首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JFileChooser混淆

JFileChooser混淆
EN

Stack Overflow用户
提问于 2015-09-21 16:42:21
回答 2查看 248关注 0票数 0

我必须编写一个程序,该程序本质上是制作成绩单的,但直到它通过命令行参数或用户从浏览器中选择一个文件才能这样做,这意味着我需要使用JFileChooser。我已经为JFileChooser设置了图形用户界面,但这是我所能知道的全部。当我单击“打开”时,对话框窗口将打开,但在选择文件后,我创建的窗口(GUI)不会关闭。而且,在加载文件之前,该程序会运行我所有的其他方法,从而导致其他问题。我试过使用do-while循环,但在我打开文件之前,它只是在循环中运行。

代码语言:javascript
复制
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.
}
}

有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-21 17:27:47

你把一个简单的问题复杂化了。您不需要构建一个窗口,任何东西都只需要使用JFileChooser。一个简单的解决方案是

代码语言:javascript
复制
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。没有必要设置间接费用。

我删除了您所有的类活动,因为我没有这些文件。

顺便说一句,我还没有测试过它,但是我相信你的问题来自于你的新框架,而不是模式。因此,布尔变量在您可以做任何事情之前都会被更改。但这只是个主意。

票数 0
EN

Stack Overflow用户

发布于 2015-09-21 17:11:18

我想你需要知道JFileChooser是如何工作的。

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32700432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档