这段代码在各方面都有效,但现在它并没有完全关闭,它似乎在我点击窗口上的“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);
}
});
}
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();
}
答案 0 :(得分:6)
将默认关闭操作设置为“DISPOSE_ON_CLOSE”而不是EXIT_ON_CLOSE(常规提示)
Hovercraft Full Of Eels评论非常有用。 如果您有任何未曝光的窗口或非守护程序线程,您的应用程序将不会终止
检查所有有效帧..使用Frame.getFrames()
如果处理掉所有Windows / Frame,则调试以检查仍在运行的任何非守护程序线程
一个残酷但有效的解决方案是System.exit(0)
类应该实现WindowListener
// Use this method
public void windowClosed(WindowEvent e){
System.exit(0);
}
答案 1 :(得分:2)
我打算猜测这行代码
while(true) {}
可能是你的罪魁祸首。
答案 2 :(得分:1)
务必将默认关闭操作设置为DISPOSE_ON_CLOSE而不是EXIT_ON_CLOSE
frameName.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
有关详细信息:Basic Java Swing, how to exit and dispose of your application/JFrame
答案 3 :(得分:0)
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();
}
答案 4 :(得分:0)
投机。对于Nimbus来说,拥有静态(swing)类可以保留一些东西。因为使用静态通常也不是很好看,尝试取消它们,作为JFrame应用程序的字段。
答案 5 :(得分:0)
我遇到了同样的问题,我只是在最后添加了此代码,当它变黑并没有关闭时,我只需键入1并在下次修复即可。
Scanner scan=new Scanner(System.in);
int exit=scan.nextInt();
if (exit==1) System.exit(0);