⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example0701_components.java

📁 本程序使用常见组件设计一个用于输入学生信息的用户界面
💻 JAVA
字号:
/* 本程序使用常见组件设计一个用于输入学生信息的用户界面 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;

public class Example0701_Components extends JFrame implements ActionListener
{
	JTextField name;
	JPasswordField password;
	JRadioButton male;
	JRadioButton female;
	JCheckBox party;
	JSpinner age;
	JButton selectColor;
	JPanel color;
	JSlider addition;
	JComboBox department;
	JList course;
	JButton confirm;
	JButton save;
	JTextArea result;
	JProgressBar progbar;
	JLabel time;
	
	public Example0701_Components()
	{
		setTitle("Components usage in inputting the information of students");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		buildContentPane();
		
		setExtendedState(MAXIMIZED_BOTH);
		setVisible(true);
	}
	
	public void buildContentPane()
	{
		name = new JTextField();
		
		password = new JPasswordField();
		
		male = new JRadioButton("男", true);
		female = new JRadioButton("女");
		ButtonGroup group = new ButtonGroup();
		group.add(male);
		group.add(female);
		
		party = new JCheckBox("", false);
		
		age = new JSpinner();
		age.setValue(new Integer(20));
		age.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent evt)
			{
				Integer v = (Integer)age.getValue();
				int value = v.intValue();
				
				if (value <= 15)
				{
					age.setValue(new Integer(15));
				}
				
				if (value >= 25)
				{
					age.setValue(new Integer(25));
				}
			}
		});
		
		color = new JPanel();
		color.setBorder(BorderFactory.createLineBorder(Color.BLUE));
		
		addition = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
		addition.setMajorTickSpacing(10);
		addition.setMinorTickSpacing(5);
		addition.setPaintTicks(true);
		addition.setPaintLabels(true);
		addition.setSnapToTicks(true);
		
		String[] departmentNames = {
			"计算机科学与技术系",
			"电子信息与技术系",
			"计算机工程系"
		};
		department = new JComboBox(departmentNames);
		department.setEditable(false);
		
		String[] coursesNames = {
			"数据结构",
			"操作系统",
			"网络原理",
			"Java程序设计",
			"分布式系统开发技术",
			"计算机导论",
			"密码学",
			"计算机组成原理",
			"编译原理",
			"图形学"
		};
		course = new JList(coursesNames);
		course.setVisibleRowCount(5);
		
		confirm = new JButton("确认");
		confirm.addActionListener(this);
		
		save = new JButton("保存");
		save.addActionListener(this);
		
		result = new JTextArea();
		
		time = new JLabel("计时开始...");
		
		progbar = new JProgressBar(JProgressBar.HORIZONTAL,0,100);
		progbar.setStringPainted(true);
		
		Timer timer = new Timer(1000, new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy 年 M 月 d 日    hh : mm : ss");
				time.setText(sf.format(new java.util.Date()));
				
				if (progbar.getValue() >= 100) progbar.setValue(0);
				progbar.setValue(progbar.getValue() + 5);
			}
		});
		timer.start();
		
		Box boxName = Box.createHorizontalBox();
		boxName.add(new JLabel("姓名"));
		boxName.add(name);
		
		Box boxPassword  = Box.createHorizontalBox();
		boxPassword.add(new JLabel("密码"));
		boxPassword.add(password);
		
		Box boxSexPartyAgeColor = Box.createHorizontalBox();
		boxSexPartyAgeColor.add(new JLabel("性别"));
		boxSexPartyAgeColor.add(male);
		boxSexPartyAgeColor.add(female);
		boxSexPartyAgeColor.add(Box.createHorizontalGlue());
		boxSexPartyAgeColor.add(new JLabel("党否"));
		boxSexPartyAgeColor.add(party);
		boxSexPartyAgeColor.add(Box.createHorizontalGlue());
		boxSexPartyAgeColor.add(new JLabel("年龄"));
		boxSexPartyAgeColor.add(age);
		boxSexPartyAgeColor.add(Box.createHorizontalGlue());
		selectColor = new JButton("选择颜色");
		selectColor.addActionListener(this);
		boxSexPartyAgeColor.add(selectColor);
		boxSexPartyAgeColor.add(Box.createHorizontalStrut(10));
		boxSexPartyAgeColor.add(color);
		
		Box boxAge = Box.createHorizontalBox();

		Box boxAddition = Box.createHorizontalBox();
		boxAddition.add(new JLabel("加分"));
		boxAddition.add(addition);
		
		Box box1 = Box.createVerticalBox();
		box1.add(Box.createVerticalStrut(20));
		box1.add(boxName);
		box1.add(Box.createVerticalStrut(20));
		box1.add(boxPassword);
		box1.add(Box.createVerticalStrut(20));
		box1.add(boxSexPartyAgeColor);
		box1.add(Box.createVerticalStrut(20));
		box1.add(boxAddition);
		box1.add(Box.createVerticalStrut(20));
		
		Box butt = Box.createHorizontalBox();
		butt.add(confirm);
		butt.add(Box.createHorizontalStrut(10));
		butt.add(save);
		
		Box box2 = Box.createVerticalBox();
		box2.add(new JLabel("系别"));
		box2.add(department);
		box2.add(Box.createVerticalStrut(10));
		box2.add(new JLabel("选课"));
		box2.add(new JScrollPane(course));
		box2.add(Box.createVerticalStrut(10));
		box2.add(butt);
		
		Box box = Box.createHorizontalBox();
		box.setBorder(BorderFactory.createTitledBorder("请输入学生信息"));
		box.add(box1);
		box.add(Box.createHorizontalStrut(20));
		box.add(new JSeparator(JSeparator.VERTICAL));
		box.add(Box.createHorizontalStrut(20));
		box.add(box2);
		
		JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, box, new JScrollPane(result));
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1,3));
		panel.add(new JLabel("作者:QSP  2005 年 10 月"));
		panel.add(time);
		panel.add(progbar);
		
		JLabel title = new JLabel("学生信息录入系统", new ImageIcon("mouse.gif"), JLabel.CENTER);
		title.setHorizontalTextPosition(JLabel.LEFT);
		title.setForeground(Color.BLUE);
		title.setFont(new Font("黑体", Font.BOLD, 36));

		Container con = getContentPane();
		con.add(title, "North");
		con.add(split, "Center");
		con.add(panel, "South");
	}
	
	public void actionPerformed(ActionEvent evt)
	{
		if (evt.getSource() == selectColor)
		{
			Color c = JColorChooser.showDialog(this, "请选择你所喜欢的颜色", color.getBackground());
			color.setBackground(c);
		}
		
		if (evt.getSource() == confirm)
		{
			String nameStr = name.getText();
			if (nameStr.equals(""))
			{
				JOptionPane.showMessageDialog(this, "姓名不能为空!");
				name.requestFocus();
				return;
			}
		
			int ret = JOptionPane.showConfirmDialog(this, "确认信息是正确的吗?");
			if (ret != JOptionPane.YES_OPTION) return;

			StringBuffer courses = new StringBuffer();
			Object[] selectedCourses = course.getSelectedValues();
			for (int i = 0 ; i < selectedCourses.length ; i++)
			{
				courses.append((String)selectedCourses[i]);
				if (i < selectedCourses.length - 1)
				{
					courses.append(",");
				}
			}
				
			String student =
				nameStr + "," +
				"密码(" + new String(password.getPassword()) + ")," +
				(male.isSelected() ? "男," : "女,") +
				(party.isSelected() ? "党员," : "") +
				"现年" + age.getValue() + "岁," +
				"喜欢" + color.getBackground() + "颜色," +
				"可得到加分" + addition.getValue() + "," +
				"在" + department.getSelectedItem() + "学习," +
				"选修的课程有:" + courses + "\n";
				
			result.append(student);
		}
		
		if (evt.getSource() == save)
		{
			JFileChooser fc = new JFileChooser();
			if (JFileChooser.APPROVE_OPTION == fc.showSaveDialog(this))
			{
				try
				{
					File f = fc.getSelectedFile();
					FileWriter fw = new FileWriter(f);
					
					fw.write(result.getText());
					fw.close();
				}
				catch (IOException e)
				{
					JOptionPane.showMessageDialog(this, "文件 IO 异常!");
				}
			}
		}
	}
		
	public static void main(String[] args)
	{
		new Example0701_Components();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -