我正在尝试使用此GUI来让人浏览txt文件,然后阅读它。当用户单击“读取文件”按钮时,应提示他们选择文件,然后它将读取文件并设置各种值。我遇到了一些问题。首先,我使用扫描仪尝试设置值,但我不确定这是最好的方法。其次,我不确定文件是否被读取。我可以打开浏览文件对话框,但随后冻结,我必须重新启动eclipse。如何使用JFileChooser读取文件并通过读取来设置值?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Display extends JFrame implements ActionListener {
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 350;
private static final int FRAME_X_ORIGIN = 100;
private static final int FRAME_Y_ORIGIN = 75;
private JFrame mainFrame;
private JCheckBox avgHSCheckBox;
private JCheckBox avgTSCheckBox;
private JCheckBox homeworkSDCheckBox;
private JCheckBox testSDCheckBox;
private JRadioButton firstClass;
private JRadioButton secondClass;
private JRadioButton thirdClass;
private JRadioButton fourthClass;
private JButton readFileButton;
private JButton exitButton;
private JButton statsButton;
private JButton clearButton;
static int numberOfClasses = 3;
static int numberOfAssignments = 6;
static int numberOfStudents;
static int numberOfLabs = 15;
static int totalHomeworkScore = 0;
static int totalTestScore = 0;
static String classYear;
static String semester;
public static void main(String[] args) {
Display frame = new Display();
frame.setVisible(true);
}
public Display() {
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setTitle("CSCE155A Course Offering Viewer");
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// header
JPanel header = new JPanel();
header.setLayout(new GridLayout(2, 1));
header.setSize(350, 75);
header.setBorder(BorderFactory.createLineBorder(Color.BLACK));
header.add(new JLabel("CSCE155A Course Offering Viewer"));
header.add(new JLabel("First Last"));
add(header, BorderLayout.NORTH);
// select stats
JPanel statsSelect = new JPanel();
statsSelect.setLayout(new GridLayout(5, 1));
statsSelect.setSize(100, 100);
statsSelect.setBorder(BorderFactory.createLineBorder(Color.BLACK));
statsSelect.add(new JLabel("Please Select: "));
avgHSCheckBox = new JCheckBox("View Average Homework Score");
statsSelect.add(avgHSCheckBox);
avgTSCheckBox = new JCheckBox("View Average Test Score");
statsSelect.add(avgTSCheckBox);
homeworkSDCheckBox = new JCheckBox("View SD for Homework Scores");
statsSelect.add(homeworkSDCheckBox);
testSDCheckBox = new JCheckBox("View SD for Test Scores");
statsSelect.add(testSDCheckBox);
add(statsSelect, BorderLayout.WEST);
// Course offerings
JPanel courseOfferings = new JPanel();
courseOfferings.setLayout(new GridLayout(5, 1));
courseOfferings.setSize(100, 100);
courseOfferings.setBorder(BorderFactory.createLineBorder(Color.BLACK));
courseOfferings.add(new JLabel("Please Select: "));
firstClass = new JRadioButton(semester + classYear);
courseOfferings.add(firstClass);
secondClass = new JRadioButton(semester + classYear);
courseOfferings.add(secondClass);
thirdClass = new JRadioButton(semester + classYear);
courseOfferings.add(thirdClass);
fourthClass = new JRadioButton(semester + classYear);
courseOfferings.add(fourthClass);
add(courseOfferings, BorderLayout.EAST);
// statistics
JPanel statistics = new JPanel();
statistics.setLayout(new GridLayout(5, 1));
statistics.setSize(200, 150);
statistics.setBorder(BorderFactory.createLineBorder(Color.BLACK));
statistics.add(new JLabel("Statistics:"));
add(statistics, BorderLayout.CENTER);
// buttons
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout());
buttons.setSize(200, 150);
buttons.setBorder(BorderFactory.createLineBorder(Color.BLACK));
readFileButton = new JButton("Read File");
buttons.add(readFileButton);
exitButton = new JButton("Exit");
buttons.add(exitButton);
statsButton = new JButton("Stats");
buttons.add(statsButton);
clearButton = new JButton("Clear");
buttons.add(clearButton);
add(buttons, BorderLayout.SOUTH);
avgHSCheckBox.addActionListener(this);
avgTSCheckBox.addActionListener(this);
homeworkSDCheckBox.addActionListener(this);
testSDCheckBox.addActionListener(this);
firstClass.addActionListener(this);
secondClass.addActionListener(this);
thirdClass.addActionListener(this);
fourthClass.addActionListener(this);
readFileButton.addActionListener(this);
exitButton.addActionListener(this);
statsButton.addActionListener(this);
clearButton.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event) {
Scanner scanner = new Scanner(System.in);
CourseOffering myCourseOffering = new CourseOffering();
ComputeStatistics myComputeStatistics = new ComputeStatistics();
JButton clickedButton = (JButton) event.getSource();
String buttonText = clickedButton.getText();
if (buttonText.equals("Read File")) {
// Create a file chooser
String filename = File.separator;
JFileChooser fc = new JFileChooser(new File(filename));
fc.showOpenDialog(this);
File selFile = fc.getSelectedFile();
numberOfClasses = scanner.nextInt();
for (int i = 0; i < numberOfClasses; i++) {
myCourseOffering.setDescription(scanner.next()); // CSCE
myCourseOffering.setDescription(scanner.next()); // 155A
myCourseOffering.setDescription(scanner.next()); // -
myCourseOffering.setDescription(scanner.next()); // Semester
semester = myCourseOffering.getDescription();
myCourseOffering.setDescription(scanner.next()); // Year
classYear = myCourseOffering.getDescription();
numberOfStudents = scanner.nextInt(); // Number Of Students
myCourseOffering.students = new Student[numberOfStudents];
for (int j = 0; j < numberOfStudents; j++) {
myCourseOffering.students[j] = new Student();
totalTestScore = 0;
totalHomeworkScore = 0;
// Sets first and last name
myCourseOffering.students[j].setFirstName(scanner.next());
myCourseOffering.students[j].setLastName(scanner.next());
// call assignscores array
int[] assignScores = new int[numberOfAssignments];
for (int k = 0; k < numberOfAssignments; k++) {
assignScores[k] = scanner.nextInt();
totalHomeworkScore += assignScores[k];
}
myCourseOffering.students[j]
.setAssignmentScores(assignScores);
int[] labScores = new int[numberOfLabs];
for (int l = 0; l < numberOfLabs; l++) {
labScores[l] = scanner.nextInt();
totalHomeworkScore += labScores[l];
}
myCourseOffering.students[j].setLabScores(labScores);
myCourseOffering.students[j].setMidTerm1(scanner.nextInt());
totalTestScore += myCourseOffering.students[j]
.getMidterm1();
myCourseOffering.students[j].setMidterm2(scanner.nextInt());
totalTestScore += myCourseOffering.students[j]
.getMidterm2();
myCourseOffering.students[j]
.setFinalExam(scanner.nextInt());
totalTestScore += myCourseOffering.students[j]
.getFinalExam();
myCourseOffering.students[j]
.setQuizScore(scanner.nextInt());
totalTestScore += myCourseOffering.students[j]
.getQuizScore();
myCourseOffering.students[j].setAttendanceScore(scanner
.nextInt());
totalHomeworkScore += myCourseOffering.students[j]
.getAttendanceScore();
myCourseOffering.students[j].setPatScore(scanner.nextInt());
totalTestScore += myCourseOffering.students[j]
.getPatScore();
myCourseOffering.students[j].setZyanteScore(scanner
.nextInt());
totalTestScore += myCourseOffering.students[j]
.getZyanteScore();
myCourseOffering.students[j]
.setTotalHomeworkScore(totalHomeworkScore);
myCourseOffering.students[j]
.setTotalTestScore(totalTestScore);
}
}
}
}
}
答案 0 :(得分:1)
您创建的扫描程序使用System.in
。您需要创建一个对文件进行操作的扫描程序:Scanner scanner = new Scanner(selFile);
。
另外,仅仅是FYI,这是做ActionListeners的一种恶劣方式。我知道有很多例子可以做到这一点,包括来自Sun / Oracle的例子,但它很糟糕。为每个按钮附加单独的监听器,这样你就不会创建一个负责所有事情的巨型监听器。