JFileChooser 混乱

JFileChooser confusion

我必须编写一个基本上可以制作成绩单的程序,但是直到它通过命令行参数或用户从浏览器中选择一个文件读取文件后才能这样做,这意味着我需要使用 JFileChooser。我有 JFileChooser 的 GUI 设置,但这就是我能弄清楚的。当我单击打开时,对话框 window 打开,但在选择一个文件后,我创建的 window (GUI) 没有关闭。此外,在加载文件之前,该程序还会运行我所有其他方法,从而导致其他问题。我尝试使用 do-while 循环,但它只是在我打开文件之前运行了整个循环。

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MP1 extends JFrame implements java.awt.event.ActionListener{
static StudentAssignments geen165 = new StudentAssignments();
static boolean fileReady = false;
/**
 * @param args the command line arguments
 */

public static void main(String [] args) {

    do{
        if(args.length == 0 || args[0].isEmpty()){//reads in input from file
                                                  //select
           MP1 doIt = new MP1();
           doIt.setVisible(true);
        }
        else{
         geen165.readGradeFile(args[0]);//reads in input file from command 
                                        //argument 
        }
    }while(!fileReady);
    //test methods 
        JOptionPane.showMessageDialog(null, geen165.getGradeReport());

        geen165.addAssignments(3, 98, 100);
        geen165.saveGradeFile("NewGrades.txt");

        JOptionPane.showMessageDialog(null, geen165.getGradeReport());

        geen165.removeAssignment(0, 2);
        JOptionPane.showMessageDialog(null, geen165.getGradeReport());
}


//JFile Chooser GUI
public MP1(){
    prepareGui();
}

private void prepareGui(){
    setSize(500,500);

    Container window = getContentPane();
    window.setLayout(new FlowLayout());


    JButton open = new JButton("Open");
    JButton cancel = new JButton("Cancel");
    JLabel status = new JLabel("You've selected: ");

    //sets file when open is pressed
    open.addActionListener((ActionEvent e) -> {
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog(window);
        if(returnVal == JFileChooser.APPROVE_OPTION){
            File fileName = chooser.getSelectedFile();
            status.setText("You've selected: " + fileName.getName());
            geen165.readGradeFile(fileName.getName());
            fileReady=true;
        }
    });
    //exits program if cancel is pressed
    cancel.addActionListener((ActionEvent e) -> {
        System.exit(1); 
    });
    window.add(open);
    window.add(cancel);
    window.add(status);
    setDefaultCloseOperation(HIDE_ON_CLOSE); 
}

@Override
public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change 
                        //body of generated methods, choose Tools | Templates.
}
}

有什么建议吗?

我认为您需要了解 JFileChooser 的工作原理。

JFileChooser fc = new JFileChooser();

int optionSelected = fc.showOpenDialog(YourClassName.this);

if (optionSelected == JFileChooser.APPROVE_OPTION) {
     File file = fc.getSelectedFile(); 
     ... // Do what you want with the file
}

JFileChooser

你把一个简单的问题复杂化了。你不用建一个window,一切只用JFileChooser。一个简单有效的解决方案是

import javax.swing.*;

public class MP1 extends JFrame  {

    public static void main(String[] args) {
        String myFile="";

        if (args.length == 0 || args[0].isEmpty()) {//reads in input from file
            //select
            JFileChooser chooser = new JFileChooser();
            int returnVal = chooser.showOpenDialog(null);
            if (returnVal!=JOptionPane.CANCEL_OPTION) {
                myFile = chooser.getSelectedFile().getPath();
            } else {
                JOptionPane.showMessageDialog(null, "Thanks for playing!");
                System.exit(0);
            }
        } else {
            myFile = args[0];
        }
        JOptionPane.showMessageDialog(null, "You have selected "+myFile+". Go play!");
    }

}

注意我检查是否有参数。如果没有,我立即去执行 JFileChooser。不需要开销。

我删除了你所有的 class activity,因为我没有这些文件。

顺便说一句,我还没有测试过,但我相信你的问题来自于你的新框架不是模态的。因此,在您可以执行任何操作之前更改了布尔变量。但这只是一个想法。