?? example0701_components.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 + -