Java 如何使图形消失?

Java 如何使图形消失?,java,graphics,applet,actionlistener,paint,Java,Graphics,Applet,Actionlistener,Paint,我想创建一个带有面部绘制游戏的小程序,该游戏带有按钮来更改面部的各个部分,但我不知道如何使用setVisible(false)来创建一个椭圆形,例如,当它在paint方法块中声明时,它会消失在动作侦听器中 //import necessary packages public class applet1 extends Applet implements ActionListener { Button b; init() { b=new Button("Oval face");

我想创建一个带有面部绘制游戏的小程序,该游戏带有按钮来更改面部的各个部分,但我不知道如何使用
setVisible(false)
来创建一个椭圆形,例如,当它在paint方法块中声明时,它会消失在动作侦听器中

//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!
}
}
  • 在进行任何自定义绘制之前,请调用
    super.paint
  • 使用状态标志更改
    paint
    实际执行的操作
  • 考虑使用Swing over AWT,将核心应用程序包装在
    JPanel
    上,并将其添加到顶级容器中
  • 也许更像

    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    public class Content extends JPanel implements ActionListener {
    
        private JButton b;
        private boolean paintOval = false;
    
        public Content() {
            b = new JButton("Oval face");
            b.addActionListener(this);
            add(b);
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
            if (paintOval) {
                g.drawOval(50, 50, 50, 50);
            }
        }
    
        public void actionPerformed(ActionEvent ae) {
            paintOval = false;
            repaint();
        }
    }
    
    然后将其添加到顶级容器中

    public class Applet1 extends JApplet {
        public void init() {
            add(new Content());
        }
    }
    

    但是如果你只是说,我会避免使用小程序,它们有自己的一系列问题,当你刚刚学习时,它们会让生活变得困难

    1)为什么要编写小程序?如果是老师指定的,请参考。2) 为什么要使用AWT?有很多很好的理由放弃AWT使用组件,转而使用Swing。是的,实际上这是一项任务,我完全同意,因为我正在学习html和JavaScript,我发现与其他PLs相比,小程序在创建基于web的应用程序方面毫无用处。无论如何,谢谢!我想这样试试。非常感谢。