有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中关闭帧

我想知道是否有人能向我解释为什么我的这个不能编译?当用户点击[x]按钮时,我试图用java关闭一个框架。我不确定您是否需要java中的监听器或类似的东西,但自从我一直在查找这个问题以来,似乎这就是您所需要的

import javax.swing.JFrame;

public class BallWorld
{
  public static void main( String[] args )
  {
    BallWorldFrame world = new BallWorldFrame();
    world.setDefaultCloseOperation(world.DISPOSE_ON_CLOSE);
    world.setVisible( true );

   }


  }

共 (1) 个答案

  1. # 1 楼答案

    从你所说的,听起来你的BallWorldFrame并不是从JFrame扩展而来,因为默认的关闭操作是JFrame的独有功能

    尝试一个更简单的例子:

    public static void main(String[] args) {
    
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
    
                BallWorldFrame world = new BallWorldFrame();
                // All these compile and run without issue...
                world.setDefaultCloseOperation(world.DISPOSE_ON_CLOSE);
                world.setDefaultCloseOperation(BallWorldFrame.DISPOSE_ON_CLOSE);
                world.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                world.setVisible(true);
    
            }
        });
    
    }
    
    public static class BallWorldFrame extends JFrame {
    }
    

    注意static去宽容来自这样一个事实,在我的例子中,BallWorldFrame是我的主类的一个内部类。如果BallWorldFrame存在于它自己的类文件中,它将不需要它