我创建了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 只是挂在那里。有没有什么办法解决这一问题?