我正在努力确保我的Java应用程序采取合理的步骤来保持健壮,其中一部分涉及优雅地关闭.我正在阅读有关关闭钩子的事情,我实际上并没有在实践中如何使用它们.
那里有一个实际的例子吗?
Let's say I had a really simple application like this one below, which writes numbers to a file, 10 to a line, in batches of 100, and I want to make sure a given batch finishes if the program is interrupted. I get how to register a shutdown hook but I have no idea how to integrate that into my application. Any suggestions?
package com.example.test.concurrency;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class GracefulShutdownTest1 …Run Code Online (Sandbox Code Playgroud) 我很抱歉,如果这是一个的n00b问题,但我已经花路太长,这一次我创建的窗口侦听器,窗口事件,以及其他一切,我怎么指定调用什么方法?这是我的代码:
private static void mw() {
Frame frm = new Frame("Hello Java");
WindowEvent we = new WindowEvent(frm, WindowEvent.WINDOW_CLOSED);
WindowListener wl = null;
wl.windowClosed(we);
frm.addWindowListener(wl);
TextField tf = new TextField(80);
frm.add(tf);
frm.pack();
frm.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试获取一个URL,并下载它,我已经完成了其他所有工作,我只是想让窗口关闭.
我有一个JDialog作为我的应用程序中的主窗口(最初它是一个JFrame,但它在任务栏中显示我不想要).
目前我在做:
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
Run Code Online (Sandbox Code Playgroud)
当我点击退出按钮时:
frame.dispose();
Run Code Online (Sandbox Code Playgroud)
但这个过程似乎仍然在后台徘徊
JFrame JFrame.EXIT_ON_CLOSE似乎做了我想做的事.
如何正确关闭我的应用程序?
我有一个Java swing启动程序来启动另一个类(运行它的main方法).每个程序都有取消按钮退出自己.
我System.exit(0);按下这个取消按钮时使用.
启动程序执行此操作actionPerformed:
if (source==carMenuItem) {
ta.append("Car launched\n");
TestCar.main(par);
}
if (source==DialogboxMenuItem) {
ta.append("Dialogbox launched\n");
DialogBox.main(par);
}
if (source==LengthConversionMenuItem) {
ta.append("LengthConversion launched\n");
LengthConversion.main(par);
}
Run Code Online (Sandbox Code Playgroud)
当我按下程序的取消按钮时,它也会关闭我的启动程序.我该如何避免这种情况?
这段代码确实在各方面工作,但现在它并没有完全关闭,它似乎在我点击窗口上的"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();
} …Run Code Online (Sandbox Code Playgroud) 我是这样写的.
public static void main(String[] args){
Thread thread = new Thread(() -> {
while(true){
try{
// do something
Thread.sleep(10);
}catch(InterruptedException ex){
System.out.println("ABC");
break;
}
}
});
JFrame frame = new JFrame();
frame.setSize(1280,720);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosed(WindowEvent e){
thread.interrupt();
}
});
thread.start();
frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)
当我点击窗口的"关闭"按钮时,
程序花了几秒钟才结束.
什么阻止程序退出?
如何在不使用的情况下立即关闭程序JFrame.EXIT_ON_CLOSE?
我认为我的线程(while(true)线程)立即结束,
因为我点击"关闭"按钮后很快就会显示"ABC".
谢谢.
编辑
没有我的线程发生同样的事情.
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(1280,720);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)
EDIT2
public static …Run Code Online (Sandbox Code Playgroud) mainFrame.addWindowListener(new WindowListener() {
@Override
public void windowClosing(WindowEvent e) {
if (JOptionPane.showConfirmDialog(mainFrame, "Are you sure you want to quit?", "Confirm exit.", JOptionPane.OK_OPTION, 0, new ImageIcon("")) != 0) {
return;
}
System.exit(-1);
}
@Override
public void windowOpened(WindowEvent e) {}
@Override
public void windowClosed(WindowEvent e) {}
@Override
public void windowIconified(WindowEvent e) {}
@Override
public void windowDeiconified(WindowEvent e) {}
@Override
public void windowActivated(WindowEvent e) {}
@Override
public void windowDeactivated(WindowEvent e) {}
});
Run Code Online (Sandbox Code Playgroud)
有我的代码,是否可能,因为我只使用windowClosing方法删除我的情况下的所有其他方法,无用的方法,所以它占用更少的空间?
例
mainFrame.addWindowListener(new WindowListener() {
@Override
public void windowClosing(WindowEvent e) {
if …Run Code Online (Sandbox Code Playgroud)