繁体   English   中英

Jframe不会关闭“X”

[英]Jframe will not close on “X”

这段代码确实在各方面工作,但现在它并没有完全关闭,它似乎在我点击窗口上的“X”时挂起。 在我靠近后,如果我最小化屏幕,然后最大化它,它只是黑色。 我可以让它完全关闭的唯一方法是退出我的IDE Eclipse。

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

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);
        }
    });
}

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中的小红框关闭了它,但仍然靠它自己没有。

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

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

检查所有活动帧..使用Frame.getFrames()如果所有Windows /帧都被丢弃,则调试以检查仍在运行的任何非守护程序线程

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

类应该实现WindowListener

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

我打算猜测这行代码

while(true) {}

可能是你的罪魁祸首。

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

frameName.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

有关更多信息: 基本Java Swing,如何退出和处理您的应用程序/ JFrame

 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();

        }

投机。 对于Nimbus来说,拥有静态(swing)类可以保留一些东西。 因为使用静态通常也不是很好看,尝试取消它们,作为JFrame应用程序的字段。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM