学生录取程序
1. 问题描述
题目:学生录取程序
要求:主界面有菜单,分别有(录取,统计,退出3个菜单项),“录取”及“统计”菜单功能和同名按钮功能一样。“退出”菜单项,关闭窗口并退出程序。主界面中允许用户输入学生信息(学号,性别,总成绩,加分选择项,如体育特长生加10分,省级优秀三好学生加10分)。输入后,点击界面“录取”按钮,如符合录取条件(总分大于520),则弹出对话框提示“学号XX,姓名XX已录取”。如未录取,则弹出对话框提示“该生分数不足,未录取”。
点击“统计”菜单项,则弹出对话框显示已录取的所有学生信息(包括学号,姓名,分数,是否有加分项等)可以采用文本区显示。
2. 概要设计
2.1 界面设计
分析题目后,我选择用BorderLayout布局来实现窗口,以便实现菜单,输入,录取,统计,显示等组件的显示。
页面北部放置菜单栏,方便用户的使用。
页面中部则用三个水平Box布局分别放置“学号”,“姓名”,“性别”,“总成绩”“加分选项”;“录取”“统计”;统计页面显示,显示界面只有在点击“统计”按钮后才可显示。再将这三个容器置入垂直Box布局中。
2.2 菜单栏实现
menubar = new JMenuBar();
menu = new JMenu("菜单");
admitItem = new JMenuItem("录取");
countItem = new JMenuItem("统计");
exitItem = new JMenuItem("退出");
menu.add(admitItem);
menu.add(countItem);
menu.add(exitItem);
menubar.add(menu);
frame.add(menubar, BorderLayout.NORTH);
2.3 菜单栏功能与按钮功能的统一实现
因为其名字相同,故可用getActionCommand().equals()方法来统一实现,减少代码量。
2.4 复选框实现性别选择
group = new ButtonGroup();
boy = new JRadioButton("男生");
girl = new JRadioButton("女生");
group.add(boy);
group.add(girl);
box1.add(boy);
box1.add(girl);
2.5 下拉列表实现加分选项
box1.add(new JLabel("总成绩:"));
scoreField = new JTextField(10);
box1.add(scoreField);
box1.add(new JLabel("加分选项:"));
extraPoint = new JComboBox<String>();
extraPoint.addItem("无");
extraPoint.addItem("体育特长生");
extraPoint.addItem("省级优秀三好学生");
box1.add(extraPoint);
2.6 具体功能实现
2.6.1 退出
if (e.getActionCommand().equals("退出")) {
frame.setVisible(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2.6.2 录取
先从下拉列表中获取选项来判断是否加分,加分则分数加10分,否则将页面显示是否有加分项choice置为“否”。
然后判断分数是否大于520,大于520,则将信息追加到文本域中,但并不显示,弹出消息对话框告知用户结果。
小于520,则直接弹出消息对话框告知用户结果。
else if (e.getActionCommand().equals("录取")) {
String id = idField.getText();
String name = nameField.getText();
double score = Double.parseDouble(scoreField.getText());
String extra = extraPoint.getSelectedItem().toString();
String choice = new String("是");
if (score < 0) {
JOptionPane.showMessageDialog(frame, "请输入有效的成绩", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (extra.equals("体育特长生")) {
score += 10;
} else if (extra.equals("省级优秀三好学生")) {
score += 10;
} else if (extra.equals("无")) {
choice = "否";
}
if (score > 520) {
JOptionPane.showMessageDialog(frame, "学号"+id+",姓名"+name+"已录取", "查询结果", JOptionPane.INFORMATION_MESSAGE);
textShow.append(id+"\t"+name+"\t"+score+"\t"+choice+'\n');
} else {
JOptionPane.showMessageDialog(frame, "该生分数不足,未录取", "查询结果", JOptionPane.INFORMATION_MESSAGE);
}
}
2.6.3 统计
直接将文本域显示,文本域附加滚动条。
else if (e.getActionCommand().equals("统计")) {
textShow.setVisible(true);
}
菜单栏按钮具有相同效果。
2.6.4 错误信息处理
若用户在输入成绩时输入值无效(数字小于0或非数字),则不做操作并弹出消息对话框提示用户。
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "请输入有效的成绩", "错误", JOptionPane.ERROR_MESSAGE);
}
3. 程序具体实现
package mypackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StudentAdmissionSystem implements ActionListener{
JFrame frame;
JTextField idField, nameField, scoreField;
JButton admit, count;
JRadioButton boy, girl;
ButtonGroup group;
JComboBox<String> extraPoint;
JMenuBar menubar;
JMenu menu;
JMenuItem admitItem, countItem, exitItem;
Box box1, box2, box3, basebox;
JTextArea textShow;
StudentAdmissionSystem(){
frame = new JFrame("学生录取程序");
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menubar = new JMenuBar();
menu = new JMenu("菜单");
admitItem = new JMenuItem("录取");
countItem = new JMenuItem("统计");
exitItem = new JMenuItem("退出");
menu.add(admitItem);
menu.add(countItem);
menu.add(exitItem);
menubar.add(menu);
frame.add(menubar, BorderLayout.NORTH);
box1 = Box.createHorizontalBox();
box2 = Box.createHorizontalBox();
box3 = Box.createHorizontalBox();
basebox = Box.createVerticalBox();
box1.add(new JLabel("学号:"));
idField = new JTextField(10);
box1.add(idField);
box1.add(new JLabel("姓名:"));
nameField = new JTextField(10);
box1.add(nameField);
group = new ButtonGroup();
boy = new JRadioButton("男生");
girl = new JRadioButton("女生");
group.add(boy);
group.add(girl);
box1.add(boy);
box1.add(girl);
box1.add(new JLabel("总成绩:"));
scoreField = new JTextField(10);
box1.add(scoreField);
box1.add(new JLabel("加分选项:"));
extraPoint = new JComboBox<String>();
extraPoint.addItem("无");
extraPoint.addItem("体育特长生");
extraPoint.addItem("省级优秀三好学生");
box1.add(extraPoint);
admit = new JButton("录取");
box2.add(admit);
count = new JButton("统计");
box2.add(count);
textShow = new JTextArea(10, 20);
textShow.setText("已录取学生名单:\n\n学号\t姓名\t分数\t是否有加分项\n");
box3.add(new JScrollPane(textShow));
textShow.setVisible(false);
basebox.add(box1);
basebox.add(box2);
basebox.add(box3);
frame.add(basebox, BorderLayout.CENTER);
admitItem.addActionListener(this);
countItem.addActionListener(this);
exitItem.addActionListener(this);
admit.addActionListener(this);
count.addActionListener(this);
extraPoint.addActionListener(this);
frame.pack();
frame.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
textShow.setVisible(false);
try {
if (e.getActionCommand().equals("退出")) {
frame.setVisible(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} else if (e.getActionCommand().equals("录取")) {
String id = idField.getText();
String name = nameField.getText();
double score = Double.parseDouble(scoreField.getText());
String extra = extraPoint.getSelectedItem().toString();
String choice = new String("是");
if (score < 0) {
JOptionPane.showMessageDialog(frame, "请输入有效的成绩", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (extra.equals("体育特长生")) {
score += 10;
} else if (extra.equals("省级优秀三好学生")) {
score += 10;
} else if (extra.equals("无")) {
choice = "否";
}
if (score > 520) {
JOptionPane.showMessageDialog(frame, "学号"+id+",姓名"+name+"已录取", "查询结果", JOptionPane.INFORMATION_MESSAGE);
textShow.append(id+"\t"+name+"\t"+score+"\t"+choice+'\n');
} else {
JOptionPane.showMessageDialog(frame, "该生分数不足,未录取", "查询结果", JOptionPane.INFORMATION_MESSAGE);
}
} else if (e.getActionCommand().equals("统计")) {
textShow.setVisible(true);
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "请输入有效的成绩", "错误", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new StudentAdmissionSystem();
}
}