【发布时间】:2026-02-11 11:25:01
【问题描述】:
我想创建一个带有按钮的面部绘图游戏的小程序来更改面部的各个部分,但我不知道如何使用setVisible(false) 来制作例如当它在paint方法块中声明时,一个Oval会在动作监听器中消失。
//import necessary packages
public class applet1 extends Applet implements ActionListener
{
Button b;
init()
{
b=new Button("Oval face");
b.addActionListener(this);
add(b);
}
public void paint(Graphics g)
{
g.drawOval(50,50,50,50);
}
public void actionPerformed(ActionEvent ae)
{
g.setVisible(false); //I know this line cannot be executed but I jast want to show the idea!
}
}
【问题讨论】:
-
1) 为什么要编写小程序?如果是老师指定的,请参考Why CS teachers should stop teaching Java applets。 2) 为什么使用 AWT?请参阅 this answer 了解放弃 AWT 使用支持 Swing 的组件的许多充分理由。
-
是的,实际上这是一项任务,我完全同意,因为我正在学习 html 和 JavaScript,我发现与其他 PL 相比,applet 在创建基于 Web 的应用程序方面毫无用处。
-
无论如何,谢谢!
标签: java graphics applet actionlistener paint