?? user1.java
字號:
import java.awt.*;
import java.awt.event.*;
public class User1 extends WindowAdapter
implements ActionListener,ItemListener
{
Frame f;
TextField tf1,tf2;
Checkbox cb1,cb2;
Choice c1,c2;
List ls1;
Button b1;
public static void main(String arg[])
{
(new User1()).display();
}
public void display()
{
Panel p1,p2;
CheckboxGroup cg;
f = new Frame("Input student ");
f.setSize(480,200);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
f.setLayout(new GridLayout(1,2)); //網格布局,左右分隔窗口
ls1 = new List(); //創建列表框
f.add(ls1); //占據窗口左半部分
p1 = new Panel();
p1.setLayout(new GridLayout(6,1)); //網格布局,6行1列
f.add(p1); //占據窗口右半部分
tf1 = new TextField ("1"); //創建組件
tf2 = new TextField ("Name");
cg = new CheckboxGroup(); //創建復選框組
cb1 = new Checkbox("male",cg,true); //創建單選按鈕
cb2 = new Checkbox("female",cg,false);
c1 = new Choice(); //創建選擇框
c1.addItem("江蘇省"); //添加選擇框的選項
c1.addItem("浙江省");
c1.addItemListener(this); //注冊選擇框事件監聽程序
c2 = new Choice(); //創建選擇框
c2.addItem("南京");
b1 = new Button("Add");
b1.addActionListener(this);
p1.add(tf1); //組件依次添加到面板p1上
p1.add(tf2);
p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.add(cb1);
p2.add(cb2);
p1.add(p2);
p1.add(c1);
p1.add(c2);
p1.add(b1);
f.addWindowListener(this);
f.setVisible(true);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b1) //單擊Add按鈕時
{
String str;
str = tf1.getText()+" "+tf2.getText();
if (cb1.getState()) //單選按鈕選中時,添加標簽
str = str +" "+cb1.getLabel();
if (cb2.getState())
str = str +" "+cb2.getLabel();
str = str+" "+c1.getSelectedItem(); //獲得選擇框的選中項
str = str+" "+c2.getSelectedItem();
ls1.add(str); //添加列表框選項
tf1.setText(""+(Integer.parseInt(tf1.getText())+1));
} //編號自動加1
}
public void itemStateChanged(ItemEvent e)
{
if (c1.getSelectedIndex()==0) //對選擇框c1操作時觸發
{
c2.removeAll(); //清除選擇框c2全部內容
c2.addItem("南京"); //選擇框c2添加內容
c2.addItem("蘇州");
c2.addItem("無錫");
}
if (c1.getSelectedIndex()==1)
{
c2.removeAll();
c2.addItem("杭州");
c2.addItem("寧波");
c2.addItem("溫州");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -