我想这样做,以便在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);
}
答案 0 :(得分:1)
我尝试使用dispose();函数但它不适用于actionPerformed监听器。
dispose
需要在JFrame
而不是ActionListener
上调用。点击frame
final
,然后拨打
frame.dispose();