【JAVA Swing】学生录取程序(图形界面)

学生录取程序

1. 问题描述

2.  概要设计

2.1 界面设计

2.2 菜单栏实现

2.3 菜单栏功能与按钮功能的统一实现

2.4 复选框实现性别选择

2.5 下拉列表实现加分选项

2.6 具体功能实现

2.6.1 退出

2.6.2 录取

2.6.3 统计

2.6.4 错误信息处理

 3. 程序具体实现

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();
	}
}

  • 14
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

布凯彻-劳斯基

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值