我在加载文件后意外关闭的程序遇到问题。该程序旨在打开文件,宣布错误,并显示所有有效数字。
这是主要代码:
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
public class Project3 {
static TemperatureGUI myGUI;
static TemperatureList myList = new TemperatureList();
static Temperature[] myArray = new Temperature[100];
static int count = 0;
public static void main(String[] args) {
myGUI = new TemperatureGUI();
myGUI.setVisible(true);
}//main
public static boolean isValid(String x) {
Pattern p;
Matcher m;
String pattern = "^-?\\d{1,3}(\\.\\d{1,2})?$";
p = Pattern.compile(pattern);
m = p.matcher(x);
return m.matches();
}//isValid
public static void addMore(Temperature x) {
myList.insert(x); // add new temperature to the list
myArray[count++] = x;
SelectionSort newSorted = new SelectionSort(myArray, count);
myGUI.clear();
for (int i = 0; i < count; i++) {
myGUI.appendList(myList.getList(i).toString());
myGUI.appendArray(newSorted.get(i).toString());
}
}//AddMore
public static void handleFile(File file) {
TextFileInput in = new TextFileInput(file.getAbsolutePath());
String line = in.readLine();
while (line != null) {
//Need to Convert the String to Temperature
//Before being able to put it into the Array and List
try {
if (!isValid(line))
throw new IllegalTemperatureException(line);
float parsedTemp = Float.parseFloat(line);
Temperature temp = new Temperature(parsedTemp);
myArray[count++] = temp;
myList.insert(temp);
} catch (IllegalTemperatureException e) {
JOptionPane.showMessageDialog(null, line + " is not a valid temperature.");
} finally {
line = in.readLine();
}
}//while
SelectionSort sortedArray = new SelectionSort(myArray, count);
//Add it to GUI
for (int i = 0; i < count; i++) {
myGUI.appendArray(sortedArray.get(i).toString());
myGUI.appendList(myList.getList(i).toString());
}
for (int i = 0; i < count; i++) {
System.out.println(myList.getList(i));
}
}
}//class
FileMenuHandler.java:
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.util.regex.*;
public class FileMenuHandler implements ActionListener {
JFrame jframe;
JFileChooser chooser = new JFileChooser();
Temperature temp; //new input from "Add"
/**
*
* @param jf Which Jframe the the FileMenuHandler should be on
*/
public FileMenuHandler(JFrame jf) {
jframe = jf;
}//constructor
/**
* Checks which item was clicked and act according to to that event
*/
public void actionPerformed(ActionEvent event) {
String menuName = event.getActionCommand();
if (menuName.equals("Open")) {
int val = chooser.showOpenDialog(null);
if (val == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
Project3.handleFile(file);
} else {
JOptionPane.showMessageDialog(null, "Goodbye, forever! Don't forget the cake!");
}
} else if (menuName.equals("Add")) {
while (true) {
String input = null;
//Make sure the input value is valid.
try {
input = JOptionPane.showInputDialog(null, "Input a New Temperature.");
if (!isValid(input))
throw new IllegalTemperatureException(input);
temp = new Temperature(Float.parseFloat(input));
}
catch (IllegalTemperatureException ite) {
JOptionPane.showMessageDialog(null, input + " is an invalid temperature.");
}
//If the valid, then pass the temp value to Project 3 and call on the "addMore" method.
Project3.addMore(temp); //add more to the GUI
}//while
}//Add
else if (menuName.equals("Quit"));
System.exit(0);
}//event---
//Make sure the value is okay using Regular Expressions
public static boolean isValid(String x) {
Pattern p;
Matcher m;
String pattern = "^-?\\d{1,3}(\\.\\d{1,2})?$";
p = Pattern.compile(pattern);
m = p.matcher(x);
return m.matches();
}//isValid
}//Handler
Project3.handleFile中出现错误,错误将显示在消息中,但程序不会在窗口中显示数字。它将继续打印到控制台。 在控制台中,只有“删除引用时出现异常”。显示没有任何其他说明问题可能是什么。在我的代码中没有显示这个。为什么程序在显示错误后终止,而不是在窗口上显示数字?
修改 这是TemperatureGUI
import java.awt.GridLayout;
import javax.swing。; import java.awt。;
公共类TemperatureGUI扩展了JFrame {
JTextArea txtArray = new JTextArea();
JTextArea txtList = new JTextArea();
JMenuBar menuBar;
JMenuItem item;
public TemperatureGUI(){
//Set Up the Display
setTitle("Sorted Numbers");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 444);
setLayout(new GridLayout(1, 2));
setResizable(false);
setLocation(200,200);
createFileMenu();
txtArray.setEditable(false);
txtList.setEditable(false);
JScrollPane scrollArray = new JScrollPane(txtArray);
getContentPane().add(scrollArray);
JScrollPane scrollList = new JScrollPane(txtList);
getContentPane().add(scrollList);
//setVisible(true);
setResizable(false);
}//create GUI
public void createFileMenu(){
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
FileMenuHandler fmh = new FileMenuHandler(this);
item = new JMenuItem("Open");
item.addActionListener( fmh );
fileMenu.add( item );
fileMenu.addSeparator();
item = new JMenuItem("Add");
item.addActionListener( fmh );
fileMenu.add( item );
setJMenuBar(menuBar);
menuBar.add(fileMenu);
fileMenu.addSeparator();
item = new JMenuItem("Quit");
item.addActionListener(fmh);
fileMenu.add(item);
} //创建菜单
//Separate Methods for Adding New Values
public void appendArray(String tempArray){
txtArray.append(tempArray+"\n");
}
public void appendList(String tempList){
txtList.append(tempList +"\n");
}
//Clean out the GUI for updated list
public void clear(){
txtArray.setText(null);
txtList.setText(null);
}
}//TemperatureGUI
至于输入。我的教授为我们提供了一个TextFileInput类,用于从文本文件中逐行检索数据。 我已经尝试了诸如断点之类的东西,但它似乎没有帮助。
答案 0 :(得分:2)
在这段代码中有一个不寻常的分号:
else if (menuName.equals("Quit"));
System.exit(0);
第一个分号结束if语句,因此System.exit(0);无论之前发生了什么,下一行都会被调用。