[英]JVM won't close after JDialog has been disposed
I have created an extension of JDialog
. 我创建了
JDialog
的扩展。
public class ImageDialog extends JDialog implements ActionListener {
private JTextField textField;
public ImageDialog(JFrame parent, String title,
String message, BufferedImage bufferedImage) {
super(parent, title, true);
if (parent != null) {
Dimension parentSize = parent.getSize();
Point p = parent.getLocation();
setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
}
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setModal(true);
JPanel frame = new JPanel();
frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
frame.add(new JLabel(message), BorderLayout.PAGE_START);
JLabel lblimage = new JLabel(new ImageIcon(bufferedImage));
frame.add(lblimage, BorderLayout.CENTER);
textField = new JTextField(1);
frame.add(textField, BorderLayout.PAGE_END);
getContentPane().add(frame);
JPanel buttonPane = new JPanel();
JButton button = new JButton("OK");
buttonPane.add(button);
button.addActionListener(this);
getContentPane().add(buttonPane, BorderLayout.SOUTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
public String getTextField() {
return textField.getText();
}
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
}
}
It works well and does what's it supposed to do, except that jvm won't close after it's been used. 它运行良好并且做了它应该做的事情,除了jvm在使用后不会关闭。 I use it as follows:
我用它如下:
ImageDialog dlg = new ImageDialog(new JFrame(), "Important question", "How many fluffy bunnies do you see?", img);
System.out.println(dlg.getTextField());
dlg.dispose();
But JVM just hangs there when the program is done. 但是当程序完成时,JVM就会挂起。 Is there any way to fix this?
有没有什么办法解决这一问题?
You need to set for your frame setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
你需要为你的框架设置
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Or in any other place: 或者在任何其他地方:
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
After closing dialog program will exit. 关闭对话框程序后将退出。
For dialog you should set DISPOSE_ON_CLOSE
close operation property. 对于对话框,您应设置
DISPOSE_ON_CLOSE
关闭操作属性。 Dialog is frame dependent. 对话框取决于帧。 Your program will end when you close your frame.
关闭相框时,程序将结束。
That's why don't forget to make your frame visible. 这就是为什么不要忘记让你的框架可见。
EDIT 编辑
Instead of this: 而不是这个:
ImageDialog dlg = new ImageDialog(new JFrame(), "Screen captcha", "Enter the letters from the image", img);
System.out.println(dlg.getTextField());
dlg.dispose();
You should have sth like this: 你应该这样:
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
//ideally frame should have a button that creates the dialog and sets it to visible
// no need to dispose dialog here
It seems to work just fine in this SSCCE that uses DISPOSE_ON_CLOSE
for the dialogs as well as the frame. 它似乎在这个SSCCE中运行得很好,它使用
DISPOSE_ON_CLOSE
作为对话框和框架。
Note: When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate.
注意:当Java虚拟机(VM)中的最后一个可显示窗口被丢弃时,VM可能会终止。
That note is important if the app. 如果应用程序,这个说明很重要。 uses
DISPOSE_ON_CLOSE
consistently and the VM fails to terminate. 一致地使用
DISPOSE_ON_CLOSE
并且VM无法终止。 It indicates that there is a stray non-daemon thread running (amok?). 它表明存在一个流浪的非守护程序线程(amok?)。 Better to find the source of that thread and take reasonable action to terminate it gracefully.
最好找到该线程的来源并采取合理的行动来优雅地终止它。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class ImageDialog extends JDialog implements ActionListener {
private JTextField textField;
public static void main(String[] args) {
JFrame f = new JFrame("Image Dialog Test");
BufferedImage bi = new BufferedImage(128,50,BufferedImage.TYPE_INT_RGB);
f.setLocationByPlatform(true);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(400,100);
f.setVisible(true);
new ImageDialog(f, "Hi!", "Hello World", bi);
}
public ImageDialog(JFrame parent, String title,
String message, BufferedImage bufferedImage) {
super(parent, title, true);
if (parent != null) {
Dimension parentSize = parent.getSize();
Point p = parent.getLocation();
setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
}
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setModal(true);
JPanel frame = new JPanel();
frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
frame.add(new JLabel(message), BorderLayout.PAGE_START);
JLabel lblimage = new JLabel(new ImageIcon(bufferedImage));
frame.add(lblimage, BorderLayout.CENTER);
textField = new JTextField(1);
frame.add(textField, BorderLayout.PAGE_END);
getContentPane().add(frame);
JPanel buttonPane = new JPanel();
JButton button = new JButton("OK");
buttonPane.add(button);
button.addActionListener(this);
getContentPane().add(buttonPane, BorderLayout.SOUTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
public String getTextField() {
return textField.getText();
}
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
}
}
//Set all your:
.DISPOSE_ON_CLOSE
//to:
.EXIT_ON_CLOSE
//and if dispose(); is the last thing to happen change it to:
System.exit(0);
EDIT for comment answer: 编辑评论答案:
System.gc();
dialog.setVisible(false);
The JVM will run as long as your program is running so I don't see the issue with that. 只要程序运行,JVM就会运行,所以我没有看到它的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.