[英]JVM won't close after JDialog has been disposed
我創建了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();
}
}
它運行良好並且做了它應該做的事情,除了jvm在使用后不會關閉。 我用它如下:
ImageDialog dlg = new ImageDialog(new JFrame(), "Important question", "How many fluffy bunnies do you see?", img);
System.out.println(dlg.getTextField());
dlg.dispose();
但是當程序完成時,JVM就會掛起。 有沒有什么辦法解決這一問題?
你需要為你的框架設置setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
或者在任何其他地方:
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
關閉對話框程序后將退出。
對於對話框,您應設置DISPOSE_ON_CLOSE
關閉操作屬性。 對話框取決於幀。 關閉相框時,程序將結束。
這就是為什么不要忘記讓你的框架可見。
編輯
而不是這個:
ImageDialog dlg = new ImageDialog(new JFrame(), "Screen captcha", "Enter the letters from the image", img);
System.out.println(dlg.getTextField());
dlg.dispose();
你應該這樣:
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
它似乎在這個SSCCE中運行得很好,它使用DISPOSE_ON_CLOSE
作為對話框和框架。
注意:當Java虛擬機(VM)中的最后一個可顯示窗口被丟棄時,VM可能會終止。
如果應用程序,這個說明很重要。 一致地使用DISPOSE_ON_CLOSE
並且VM無法終止。 它表明存在一個流浪的非守護程序線程(amok?)。 最好找到該線程的來源並采取合理的行動來優雅地終止它。
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);
編輯評論答案:
System.gc();
dialog.setVisible(false);
只要程序運行,JVM就會運行,所以我沒有看到它的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.