【问题标题】:Jframe will not close on "X"Jframe 不会在“X”处关闭
【发布时间】:2013-05-08 02:27:26
【问题描述】:

这段代码在各个方面都有效,但现在它没有完全关闭,当我点击窗口上的“X”时它似乎挂起。在我关闭后,如果我最小化屏幕然后最大化它,它就会变成黑色。我可以让它完全关闭的唯一方法是退出 Eclipse,我的 IDE。

在此之前,我有不同的外观和感觉。我偷了一些 GUI 代码,我在 netbeans 中制作它,让它看起来比我手工制作的更好。所以我认为这与“nimbus”的外观和感觉有关,但也许我没有正确关闭其他对象,现在这是一个问题?

static CLUtilCompact app = null; // this
static AuxPPanel aux = null; // JPanel
static StatusPanel stat = null; // JPanel
static UserActPanel user = null; // JPanel
static InputPanel input = null; // JPanel
static Automator auto = null;   
        //public class Automator extends Thread 
       //   implements NativeMouseInputListener, NativeKeyListener  

public CLUtilCompact() 
{
    aux = new AuxPPanel();
    stat = new StatusPanel();
    user = new UserActPanel();
    auto = new Automator();
    input = new InputPanel();
    GlobalScreen.getInstance().addNativeKeyListener(auto);
    GlobalScreen.getInstance().addNativeMouseListener(auto);
    GlobalScreen.getInstance().addNativeMouseMotionListener(auto);
    auto.start();
}

public static void main(String[] args) 
{
    // Create the App, and panels
    app = new CLUtilCompact();
    // Let the panels have access to app now
    aux.setApp(app);
    stat.setApp(app);
    user.setApp(app);
    auto.setApp(app);
    app.updateOutput("Started");
    app.updateStatus("Started");
    input.setApp(app);

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(CLUtilCompact.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(CLUtilCompact.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(CLUtilCompact.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(CLUtilCompact.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            app.setDefaultCloseOperation(EXIT_ON_CLOSE);
            app.setAlwaysOnTop(true);
            app.setVisible(true);
        }
    });
}

Class Automator 运行和关闭

public void run()
{
    try // to make the Global hook
    {
        GlobalScreen.registerNativeHook();
    }
    catch (NativeHookException ex){theApp.updateOutput("No Global Keyboard or Mouse Hook");return;}
    try // to create a robot (can simulate user input such as mouse and keyboard input)
    {
        rob = new Robot();
    } 
    catch (AWTException e1) {theApp.updateOutput("The Robot could not be created");return;}

    while(true) {}
}

public void OnClose()
{
    GlobalScreen.unregisterNativeHook();
}

编辑:日食中的小红框将其关闭,但它仍然没有关闭。

【问题讨论】:

  • 在 Eclipse 控制台中按下 X 会怎样?
  • 不知道你的意思,你的意思是当我关闭 Eclipse 时?这一切都关闭程序完全关闭。问题是当我将它作为可运行的 .jar 使用时,无法关闭它。它甚至没有出现在任务管理器中。
  • 我的意思是THISX
  • 你有一个非守护线程正在运行,阻止了 Swing 退出。你为你的全球听众使用什么图书馆?我会调查的。
  • @Eels JNativehook 来自谷歌代码,谷歌的第一个结果。` `@Imray 问题在底部编辑

标签: java swing


【解决方案1】:

将默认关闭操作设置为“DISPOSE_ON_CLOSE”而不是 EXIT_ON_CLOSE(一般提示)

Hovercraft Full Of Eels 评论非常有用。 如果您有任何未处理的窗口或非守护线程,您的应用程序将不会终止

检查所有活动帧.. 使用Frame.getFrames() 如果所有 Windows/Frames 都已处理完毕,则进行调试以检查是否有任何仍在运行的非守护线程

一个残酷但有效的解决方案是System.exit(0)

类应该实现WindowListener

// Use this method
public void windowClosed(WindowEvent e){
    System.exit(0);
}

【讨论】:

  • 我是个残忍的人,但有时也很优雅。所以从鳗鱼评论和其他评论中,它说我需要用isDaemon(true) 将某些东西标记为守护进程?还是说一旦我真的调用了我的 OnClose 方法,我的守护进程(全局键盘和鼠标监听器)就会关闭,我的程序就会死掉而不是僵尸模式?
  • 我认为你不需要关心线程,进程会被清理。如果你有打开的文件可能是刷新和关闭它们的好主意,除了 System.exit(0) 很好
【解决方案2】:

我冒昧的猜测一下这行代码

while(true) {}

可能是你的罪魁祸首。

【讨论】:

    【解决方案3】:

    请务必将默认关闭操作设置为 DISPOSE_ON_CLOSE 而不是 EXIT_ON_CLOSE

    frameName.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    

    欲了解更多信息:Basic Java Swing, how to exit and dispose of your application/JFrame

    【讨论】:

      【解决方案4】:
       http://stackoverflow.com/questions/2352727/closing-jframe-with-button-click
      
         JButton button = new JButton("exit");
          button.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
         frame.setVisible(false);
                  frame.dispose();
      
              }
      

      【讨论】:

        【解决方案5】:

        投机。对于 Nimbus 来说,拥有静态(摇摆)类可以保留一些东西。由于经常使用静态也不是很好看,请尝试将它们作为 JFrame 应用程序的字段删除。

        【讨论】:

          【解决方案6】:

          我遇到了同样的问题,我只是在最后添加了这段代码,当它出现黑色并且没有关闭时,我只需输入 1 并在下次修复它。

          Scanner scan=new Scanner(System.in);
          int exit=scan.nextInt();
          if (exit==1) System.exit(0);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-04-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-09
            相关资源
            最近更新 更多