在JFileChooser中选择文件后关闭Jframe

时间:2013-05-26 22:13:55

标签: java swing jframe actionlistener jfilechooser

我想这样做,以便在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);

}

1 个答案:

答案 0 :(得分:1)

  

我尝试使用dispose();函数但它不适用于actionPerformed监听器。

dispose需要在JFrame而不是ActionListener上调用。点击frame final,然后拨打

frame.dispose();
相关问题