我想让它在 JFileChooser 中选择一个文件后关闭我的 JFrame。我该怎么做?我尝试使用 dispose(); 函数,但它不适用于 actionPerformed 侦听器。有小费吗?
public static void createWindow() {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("JComboBox Test");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton inbutton = new JButton("Select Input File");
inbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
Test method = new Test();
File selectedFile = fileChooser.getSelectedFile();
String outFile = selectedFile.getParent() + "/baseball_out.txt";
String inFile = selectedFile.getPath();
method.baseballedit(inFile, outFile);
//ADD CLOSING ACTION HERE//
}
}
});
frame.add(inbutton);
frame.pack();
frame.setVisible(true);
}