展开全部
字符抛出异常可以理解,真心不明白 -80 大于100这样的为啥要用try catch ,不过还是给你写32313133353236313431303231363533e4b893e5b19e31333332623337了。package zhidao;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;
import javax.swing.JPanel;
public class StudentScore extends JFrame implements ActionListener{
private JTextField stuId;
private JTextField stuScore;
private JTextArea allStudent;
private JButton button;
private final JPanel panel = new JPanel();
public StudentScore() {
setBounds(200, 100, 244, 630);
getContentPane().setLayout(null);
panel.setBounds(0, 0, 228, 592);
getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("\u8F93\u5165\u5B66\u751F\u6210\u7EE9");
lblNewLabel.setBounds(20, 33, 85, 15);
panel.add(lblNewLabel);
JLabel label = new JLabel("\u8F93\u5165\u5B66\u751F\u7F16\u53F7");
label.setBounds(20, 8, 85, 15);
panel.add(label);
stuId = new JTextField();
stuId.setBounds(113, 5, 105, 21);
panel.add(stuId);
stuId.setColumns(10);
stuScore = new JTextField();
stuScore.setBounds(113, 33, 105, 21);
panel.add(stuScore);
stuScore.setColumns(10);
button = new JButton("\u5F55\u5165");
button.setBounds(84, 64, 80, 23);
panel.add(button);
allStudent = new JTextArea();
JScrollPane scrollPane = new JScrollPane(allStudent);
scrollPane.setBounds(20, 97, 198, 485);
panel.add(scrollPane);
button.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
//监听,try catch在这里
String id = stuId.getText();
String score = stuScore.getText();
try{
Double sco = Double.valueOf(score);
if(sco == -1){
allStudent.append("录入结束");
button.setEnabled(false);
return;
}
if(sco<0 || sco>100){
throw new Exception();
}
allStudent.append("编号:"+id+",成绩:"+score);
allStudent.append("\n");
}catch(Exception e){
allStudent.append("编号为"+id+"的学生成绩输入异常");
allStudent.append("\n");
}
}
public static void main(String[] args) {
new StudentScore().setVisible(true);
}
}