JDialog/JFileChooser 的 "cancel" 按钮是否关闭了整个程序? - Java
Does the "cancel" button of a JDialog/JFileChooser close the whole program? - Java
如果我点击 JFileChooser
或 JDialog
的 CANCEL_OPTION
,它会自动关闭整个程序还是只关闭 window?
如果没有:我怎样才能关闭整个程序?
If I click the CANCEL_OPTION of for example a JFileChooser or a JDialog, does it close the whole program automaticly or does it only close the window?
不,默认情况下不会关闭整个程序。
If it doesn't: How can I close the whole program?
通过监听它,然后在它发生时做出响应。例如:
import java.awt.Dimension;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class JFileChooserFun {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
final JFrame myFrame = new JFrame("GUI");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 300));
// example using JFileChooser
panel.add(new JButton(new AbstractAction("Open File Chooser") {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int response = fileChooser.showOpenDialog(myFrame);
if (response == JFileChooser.CANCEL_OPTION) {
JOptionPane.showMessageDialog(myFrame, "Cancel Option Chosen, Closing Now");
System.exit(-1);
}
}
}));
// example using your own *modal* JDialog
panel.add(new JButton(new AbstractAction("Open My Modal Dialog") {
private JDialog dialog = null;
private boolean closeJVM = true; // if still true when dialog closed, then close application
@Override
public void actionPerformed(ActionEvent e) {
if (dialog == null) {
// build dialog in a "lazy" fashion
dialog = new JDialog(myFrame, "My Dialog", ModalityType.APPLICATION_MODAL);
JPanel dlgPanel = new JPanel();
dlgPanel.add(new JButton(new AbstractAction("OK -- close dialog without closing JVM") {
@Override
public void actionPerformed(ActionEvent e) {
closeJVM = false; // set this false so application continues to live
dialog.dispose(); // close the dialog and recover resources
}
}));
dialog.add(dlgPanel);
dialog.pack();
}
closeJVM = true; // set the field to close the JVM
dialog.setLocationRelativeTo(myFrame);
dialog.setVisible(true);
// boolean set to false if button pressed
if (closeJVM) {
JOptionPane.showMessageDialog(myFrame, "Dialog Canceled, Closing GUI");
System.exit(-1);
}
}
}));
myFrame.add(panel);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
});
}
}
如果我点击 JFileChooser
或 JDialog
的 CANCEL_OPTION
,它会自动关闭整个程序还是只关闭 window?
如果没有:我怎样才能关闭整个程序?
If I click the CANCEL_OPTION of for example a JFileChooser or a JDialog, does it close the whole program automaticly or does it only close the window?
不,默认情况下不会关闭整个程序。
If it doesn't: How can I close the whole program?
通过监听它,然后在它发生时做出响应。例如:
import java.awt.Dimension;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class JFileChooserFun {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
final JFrame myFrame = new JFrame("GUI");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 300));
// example using JFileChooser
panel.add(new JButton(new AbstractAction("Open File Chooser") {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int response = fileChooser.showOpenDialog(myFrame);
if (response == JFileChooser.CANCEL_OPTION) {
JOptionPane.showMessageDialog(myFrame, "Cancel Option Chosen, Closing Now");
System.exit(-1);
}
}
}));
// example using your own *modal* JDialog
panel.add(new JButton(new AbstractAction("Open My Modal Dialog") {
private JDialog dialog = null;
private boolean closeJVM = true; // if still true when dialog closed, then close application
@Override
public void actionPerformed(ActionEvent e) {
if (dialog == null) {
// build dialog in a "lazy" fashion
dialog = new JDialog(myFrame, "My Dialog", ModalityType.APPLICATION_MODAL);
JPanel dlgPanel = new JPanel();
dlgPanel.add(new JButton(new AbstractAction("OK -- close dialog without closing JVM") {
@Override
public void actionPerformed(ActionEvent e) {
closeJVM = false; // set this false so application continues to live
dialog.dispose(); // close the dialog and recover resources
}
}));
dialog.add(dlgPanel);
dialog.pack();
}
closeJVM = true; // set the field to close the JVM
dialog.setLocationRelativeTo(myFrame);
dialog.setVisible(true);
// boolean set to false if button pressed
if (closeJVM) {
JOptionPane.showMessageDialog(myFrame, "Dialog Canceled, Closing GUI");
System.exit(-1);
}
}
}));
myFrame.add(panel);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
});
}
}