和代码
package coursework;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Coursework extends JFrame {
public static void main(String[] args) {
JFrame frame=new JFrame("myframe");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(18,1));
JLabel StudentNameLabel = new JLabel("Student Name: ");
final JTextField TypeNameArea = new JTextField(" ");
JLabel StudentSurnameLabel = new JLabel("Student Surname: ");
final JTextField TypeSurnameArea = new JTextField(" ");
JLabel StudentIDLabel = new JLabel("Student ID: ");
final JTextField StudentIDArea = new JTextField("M00");
JLabel LabTestLabel = new JLabel("Lab Test: ");
final JTextField LabTestArea = new JTextField(" ");
JLabel QuizLabel = new JLabel("Quizes: ");
final JTextField QuizArea = new JTextField(" ");
JLabel ICourseworkLabel = new JLabel("Indiwidual CW: ");
final JTextField ICourseworkArea = new JTextField(" ");
JLabel GCourseworkLabel = new JLabel("Group CW: ");
final JTextField GCourseworkArea = new JTextField(" ");
panel.setBackground(Color.BLACK);
StudentSurnameLabel.setForeground(Color.YELLOW);
StudentNameLabel.setForeground(Color.YELLOW);
LabTestLabel.setForeground(Color.YELLOW);
StudentIDLabel.setForeground(Color.YELLOW);
QuizLabel.setForeground(Color.YELLOW);
ICourseworkLabel.setForeground(Color.YELLOW);
GCourseworkLabel.setForeground(Color.YELLOW);
JButton buttonSubmit = new JButton("Submit");
buttonSubmit.setBackground(Color.BLACK);
buttonSubmit.setForeground(Color.WHITE);
JButton buttonRead = new JButton("Read");
buttonRead.setBackground(Color.BLACK);
buttonRead.setForeground(Color.WHITE);
JButton buttonCancel = new JButton("Cancel");
buttonCancel.setBackground(Color.BLACK);
buttonCancel.setForeground(Color.WHITE);
JButton buttonNext = new JButton("Next");
buttonNext.setBackground(Color.BLACK);
buttonNext.setForeground(Color.WHITE);
panel.add(StudentNameLabel);
panel.add(TypeNameArea);
panel.add(StudentSurnameLabel);
panel.add(TypeSurnameArea);
panel.add(StudentIDLabel);
panel.add(StudentIDArea);
panel.add(LabTestLabel);
panel.add(LabTestArea);
panel.add(QuizLabel);
panel.add(QuizArea);
panel.add(ICourseworkLabel);
panel.add(ICourseworkArea);
panel.add(GCourseworkLabel);
panel.add(GCourseworkArea);
panel.add(buttonSubmit);
panel.add(buttonRead);
panel.add(buttonCancel);
panel.add(buttonNext);
frame.setSize(300, 800);
frame.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
String name;
String surname;
String number;
String gradeLabTest;
String gradeQuiz;
String gradeICourswork;
String gradeGCourswork;
name = TypeNameArea.getText();
surname = TypeSurnameArea.getText();
number = StudentIDArea.getText();
gradeLabTest = LabTestArea.getText();
gradeQuiz = QuizArea.getText();
gradeICourswork = ICourseworkArea.getText();
gradeGCourswork = GCourseworkArea.getText();
String line = name + " " + surname + " " + number + " " + gradeLabTest + " " + gradeQuiz + gradeICourswork + gradeGCourswork + "\r\n";
Writer output;
try {
output = new BufferedWriter(new FileWriter("C:/Hola/test.txt",true)); //clears file every time
output.append(line);
output.close();
} catch (IOException ex) {
Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
buttonRead.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
BufferedReader br ;
try {
br = new BufferedReader(new FileReader("C:/Hola/test.txt"));
for(String line; (line = br.readLine()) != null; ) {
String[] token = line.split(" ");
String name = token[0];
String surname = token[1];
String number = token[2];
double g1 = Double.parseDouble(token[3]);
double g2 = Double.parseDouble(token[4]);
double g3 = Double.parseDouble(token[5]);
double g4 = Double.parseDouble(token[6]);
double av = 0.4*g1 + 0.2*g2 + 0.2*g3 + 0.2*g4;
int grade = 0;
String gradestring;
String line_input = name + " " + surname + " " + number + " " + av + " " + grade + "\r\n";
Writer output;
try {
output = new BufferedWriter(new FileWriter("C:/Hola/test2.txt",true)); //clears file every time
output.append(line_input);
output.close();
} catch (IOException ex) {
Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
}
// line is not visible here.
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}
当出现错误时,只需遵循StackTrace并查看包含由您创建的类的第一行,在本例中:
coursework.coursework$2.ActionPerformed(coursework.java:135)
在尝试将字符串转换为数字时,看起来您有NumberFormatException。
我一直有问题运行这个程序,它编译,但不能正常运行。当我运行它并尝试执行计算时,它会吐出一堆错误。我认为它必须与可变类型。以下是程序: 这里是打印出来的错误,当尝试执行数学(抱歉,它真的很长)。 线程“AWT-EventQueue-0”java.lang.NumberFormatException中出现异常:空字符串 位于Sun.Misc.FloatingDecimal.ReadJavaFormat
我正在尝试将文件读取到中,但它提供了一些异常。我正在使用POI库。我该怎么办?请帮帮我。这是我的代码: 这是:
我在尝试从jformattedtextfield转换为Double时遇到一个问题。我正在用Java编写我的程序。这是我的最后一个学校项目,任何帮助都会得到的。 我的代码:
我试图用MySQL在java swing中开发一个stockManager应用程序。在运行代码时,我遇到以下错误: 我在下面附上代码...它有点长....................... StockManager.java items.java
我对Java编程非常陌生,我正在上一门基于Java的课程。我目前正在做这个咖啡项目,它是基于布尔和RadioButtons的。我相信我几乎完成了它,除了我得到这个错误消息在控制台。如果有任何其他错误,请让我知道,以便我可以修复它!
我有一个小问题,所以我一直在做这个程序,出于某种原因,它抛出了一个空指针异常。我已经让它工作了,但它不会显示我试图创建的JTable,只是一个空白窗口,当我包含代码时,它就会崩溃。。。。有什么想法吗?