这段代码以前是完全正常的,但现在它无法完全关闭,似乎会在我点击窗口上的“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);
}
});
}
自动化程序的运行和关闭
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中,小红框关闭了它,但是仍然无法自行关闭。
JNativeHook
与Swing
混合使用的一般规则 -- 链接 **在此**。 - Extreme Coders