簡體   English   中英

在JFileChooser中選擇文件后關閉Jframe

[英]Closing Jframe after a file is selected in 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);

}

我嘗試使用dispose(); 函數,但不適用於actionPerformed偵聽器。

dispose需要在JFrame而不是ActionListener上調用。 frame final frame ,然后可以調用

frame.dispose();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM