?? test.java
字號:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Start
{
public static void main(String[] args)
{
JFrame mf=new MyFrame();
mf.setSize(500,500);
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mf.setVisible(true);
System.out.println(JFrame.EXIT_ON_CLOSE);
}
}
class MyFrame extends JFrame
{
Container c=null;
JPanel mp=null;//struct JPanel* mp=NULL;
public MyFrame()
{
c=this.getContentPane();//獲取那塊合適的幕
mp=new MyPanel();//準備一塊畫布
c.add(mp);//在合適的幕上將畫布放上去。
}
}
class MyPanel extends JPanel
{
Student[] stus=new Student[5];
int count=0;
JTextField txtStuNo=new JTextField(10);
JTextField txtStuName=new JTextField(10);
JTextField txtStuAge=new JTextField(10);
JTextField txtClass=new JTextField(10);
JLabel labStuNo=new JLabel("StuNo");
JLabel labStuName=new JLabel("StuName");
JLabel labStuAge=new JLabel("StuAge");
JLabel labClass=new JLabel("ClassName");
JButton but=new JButton("錄入");
JButton but2=new JButton("查詢全部");
public MyPanel()
{
this.add(labStuNo);
this.add(txtStuNo);
this.add(labStuName);
this.add(txtStuName);
this.add(labStuAge);
this.add(txtStuAge);
this.add(labClass);
this.add(txtClass);
this.add(but);
this.add(but2);
but.addActionListener(new MyL());
but2.addActionListener(new MyL2());
}
class MyL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{Student stu=new Student();
stu.setStuNo(txtStuNo.getText());
stu.setStuName(txtStuName.getText());
stu.setAge(Short.parseShort(txtStuAge.getText()));
stu.setClassName(txtClass.getText());
stus[count++]=stu;
JOptionPane.showMessageDialog(null,"錄入完畢");
txtStuNo.setText("");
txtStuName.setText("");
txtStuAge.setText("");
txtClass.setText("");
}catch(IllegalArgumentException i)
{
JOptionPane.showMessageDialog(null,"error");
txtStuAge.setText("");
}
// JOptionPane.showMessageDialog(null,stu.getStuNo()+" " +stu.getStuName() +" "+stu.getStuAge()+ " " +stu.getClassName());
}
}
class MyL2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<count;i++)
{
System.out.println(stus[i].getStuNo()+" " +stus[i].getStuName() +" "+stus[i].getStuAge()+ " " +stus[i].getClassName());
}
}
}
}
class Student
{
private String stuNo;
private String stuName;
private String className;
private short stuAge;
public void setStuNo(String _stuNo)
{
this.stuNo=_stuNo;
}
public String getStuNo()
{
return this.stuNo;
}
public void setStuName(String _stuName)
{
this.stuName=_stuName;
}
public String getStuName()
{
return this.stuName;
}
public String getClassName()
{
return this.className;
}
public void setClassName(String _className)
{
this.className=_className;
}
public int getStuAge()
{
return this.stuAge;
}
public void setAge(short _age)
{
try
{
if(_age>0 && _age<150)
this.stuAge=_age;
else
{throw new IllegalArgumentException();
}
}catch(java.lang.IllegalArgumentException i)
{
System.out.println("error");
throw i;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -