题目:实现某一门课程的分数录入系统,界面包括学号、姓名、班号、成绩的输入域和一个录入按钮。当用户单击录入按钮时,程序检查各输入域中是否有非空的数据,如有空域则抛出一个Exception对象;同时程序检查成绩输入域,如果不是数值信息或是负的数值信息,也抛出相应的异常。在抛出异常前应提供详细的信息说明,从而使使用该程序的用户了解所出现的问题。
1.创建窗体与按钮、添加实训需要的组件
JTextField text1,text2,text3,text4;//四个输入框
JButton button ; //按钮
JTextArea area ; //文本内容
ButtonGroup group ;//创建ButtonGroup组对象
@SuppressWarnings("rawtypes")
JComboBox box;
@SuppressWarnings("unchecked")
public JTextFrame(String title){
super(title);//设置窗体标题
//创建组件
JPanel panel = new JPanel();
JLabel label_1 = new JLabel("学号:");
text1 = new JTextField(10);
JLabel label_2 = new JLabel("姓名:");
text2 = new JTextField(5);
JLabel label_3 = new JLabel("班号:");
text3 = new JTextField(5);
JLabel label_4 = new JLabel("成绩:");
text4 = new JTextField(5);
button = new JButton("录 入");
area = new JTextArea(15,20);
group = new ButtonGroup();//创建ButtonGroup组对象
String[] str = {"Java", "高等数学"};
box = new JComboBox(str);
//添加组件
panel.add(label_1);
panel.add(text1);
panel.add(label_2);
panel.add(text2);
panel.add(label_3);
panel.add(text3);
panel.add(label_4);
panel.add(text4);
panel.add(box);
panel.add(button);
DefaultMutableTreeNode classfy = new DefaultMutableTreeNode( " 学科 ",true );
DefaultMutableTreeNode family = new DefaultMutableTreeNode( " 专业 ");
DefaultMutableTreeNode schoolmate = new DefaultMutableTreeNode( " 公共 ");
family.add(new DefaultMutableTreeNode("Java"));
schoolmate.add(new DefaultMutableTreeNode("高等数学"));
classfy.add(family);
classfy.add(schoolmate);
JTree tree = new JTree(classfy);
JScrollPane sp1= new JScrollPane(tree);
this.setLayout(new BorderLayout());
this.add(sp1,BorderLayout.EAST);
//this.add(panel2,BorderLayout.WEST);
this.add(panel,BorderLayout.NORTH);
this.add(area,BorderLayout.CENTER);
this.setSize(800,500);
this.setLocation(500,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//注册动作事件监听
button.addActionListener(new ButtonListener(this));
}
2.先创建一个自定义的异常类(判断空域)
//自定义异常类
class TestException extends Exception{
public TestException() {
super(); //调用Exception无参构造方法
}
public TestException(String message) {
super(message); //调用Exception有参构造方法
}
}
3.录入按钮监听,空域/成绩为负数/成绩不是数值 在编辑器运行弹出相应异常;如果成功则在代码运行时显示录入成功(注:学号、姓名字符串还未实现匹配!)
class ButtonListener implements ActionListener{
JTextFrame jf;
public ButtonListener(JTextFrame jf) {
this.jf=jf;
}
//用JAVA正则表达式匹配字符串是不是负数
public static boolean isNumericzidai1(String str) {
Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
Matcher isNum = pattern.matcher(str);
if (isNum.matches())
return true;
else return false;
}
//用JAVA正则表达式匹配字符串是不是数字
public static boolean isNumericzidai2(String str) {
Pattern pattern = Pattern.compile("[0-9]+\\.?[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
else return true;
}
//按钮监听实行事件(判别异常测试方法)
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
test();
}catch(TestException e1) {
System.out.println(e1.getMessage());
}
//finally {
// System.out.println("出现异常!");
// }
}
public void test() throws TestException{
//获取文本内容予数组保存
String[] str=new String[4];
str[0] = jf.text1.getText();
str[1] = jf.text2.getText();
str[2] = jf.text3.getText();
str[3] = jf.text4.getText();
//输入域中存在空域,是则抛出异常
if(str[0].trim().equals(" ")||str[1].trim().equals("")||str[2].trim().equals("")||str[3].trim().equals("")) {
//抛出一个自定义Exception异常
throw (new TestException("存在空域!请重新录入!"));
}
//检查成绩输入域是不是数值信息或是负的数值信息
else if(isNumericzidai2(str[3])==true) {
jf.area.append(str[1]+"的成绩录入成功!"+"\n");
}
else {
if(isNumericzidai1(str[3])==true) {
throw(new TestException("成绩为负数!请重新录入!"));
}
else {
throw(new TestException("成绩不是数值!请重新录入!"));
}
}
}
}
全代码如下:
/*
* @author:诗&影
* 功能:简单的(判断)学科成绩录入
* 声明:如有不当之处,可以交流,加以改正。(供参考)
*
*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
//自定义异常类
class TestException extends Exception{
public TestException() {
super(); //调用Exception无参构造方法
}
public TestException(String message) {
super(message); //调用Exception有参构造方法
}
}
class ButtonListener implements ActionListener{
JTextFrame jf;
public ButtonListener(JTextFrame jf) {
this.jf=jf;
}
//用JAVA正则表达式匹配字符串是不是负数
public static boolean isNumericzidai1(String str) {
Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
Matcher isNum = pattern.matcher(str);
if (isNum.matches())
return true;
else return false;
}
//用JAVA正则表达式匹配字符串是不是数字
public static boolean isNumericzidai2(String str) {
Pattern pattern = Pattern.compile("[0-9]+\\.?[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
else return true;
}
//按钮监听实行事件(判别异常测试方法)
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
test();
}catch(TestException e1) {
System.out.println(e1.getMessage());
}
//finally {
// System.out.println("出现异常!");
// }
}
public void test() throws TestException{
//获取文本内容予数组保存
String[] str=new String[4];
str[0] = jf.text1.getText();
str[1] = jf.text2.getText();
str[2] = jf.text3.getText();
str[3] = jf.text4.getText();
//输入域中存在空域,是则抛出异常
if(str[0].trim().equals(" ")||str[1].trim().equals("")||str[2].trim().equals("")||str[3].trim().equals("")) {
//抛出一个自定义Exception异常
throw (new TestException("存在空域!请重新录入!"));
}
//检查成绩输入域是不是数值信息或是负的数值信息
else if(isNumericzidai2(str[3])==true) {
jf.area.append(str[1]+"的成绩录入成功!"+"\n");
}
else {
if(isNumericzidai1(str[3])==true) {
throw(new TestException("成绩为负数!请重新录入!"));
}
else {
throw(new TestException("成绩不是数值!请重新录入!"));
}
}
}
}
class JTextFrame extends JFrame{
JTextField text1,text3,text4;
JTextField text2;
JButton button ;
JTextArea area ;
ButtonGroup group ;//创建ButtonGroup组对象
@SuppressWarnings("rawtypes")
JComboBox box;
@SuppressWarnings("unchecked")
public JTextFrame(String title){
super(title);//设置窗体标题
//创建组件
JPanel panel = new JPanel();
JLabel label_1 = new JLabel("学号:");
text1 = new JTextField(10);
JLabel label_2 = new JLabel("姓名:");
text2 = new JTextField(5);
JLabel label_3 = new JLabel("班号:");
text3 = new JTextField(5);
JLabel label_4 = new JLabel("成绩:");
text4 = new JTextField(5);
button = new JButton("录 入");
area = new JTextArea(15,20);
group = new ButtonGroup();//创建ButtonGroup组对象
String[] str = {"Java", "高等数学"};
box = new JComboBox(str);
//添加组件
panel.add(label_1);
panel.add(text1);
panel.add(label_2);
panel.add(text2);
panel.add(label_3);
panel.add(text3);
panel.add(label_4);
panel.add(text4);
panel.add(box);
panel.add(button);
DefaultMutableTreeNode classfy = new DefaultMutableTreeNode( " 学科 ",true );
DefaultMutableTreeNode family = new DefaultMutableTreeNode( " 专业 ");
DefaultMutableTreeNode schoolmate = new DefaultMutableTreeNode( " 公共 ");
family.add(new DefaultMutableTreeNode("Java"));
schoolmate.add(new DefaultMutableTreeNode("高等数学"));
classfy.add(family);
classfy.add(schoolmate);
JTree tree = new JTree(classfy);
JScrollPane sp1= new JScrollPane(tree);
this.setLayout(new BorderLayout());
this.add(sp1,BorderLayout.EAST);
//this.add(panel2,BorderLayout.WEST);
this.add(panel,BorderLayout.NORTH);
this.add(area,BorderLayout.CENTER);
this.setSize(800,500);
this.setLocation(500,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//注册动作事件监听
button.addActionListener(new ButtonListener(this));
}
}
public class System_in {
public static void main(String args[]) {
new JTextFrame("学科成绩录入系统");
}
}
运行结果:
版权声明:本文为zc010203原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。