packageexample02;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.ItemEvent;importjava.awt.event.ItemListener;importjavax.swing.ButtonGroup;importjavax.swing.DefaultComboBoxModel;importjavax.swing.JButton;importjavax.swing.JCheckBox;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JRadioButton;importjavax.swing.JTextArea;importjavax.swing.JTextField;public class Zuoye extendsJFrame{//成员变量
JPanel pnlMain;
JLabel lblUserName,lblUserPwd,lblSex,lblHobby,lblLocation;
JRadioButton rabM,rabW;
ButtonGroup btgSex;//创建是为了使rabM,rabW同组
JCheckBox chkRead,chkSwim,chkRun;
JComboBoxcmbLocation;
DefaultComboBoxModeldcmLocation;
String location="";
JTextField txtUserName,txtJob;
JButton btnEnsure,btnCancel;
JTextArea txt;//构造方法
publicZuoye() {
pnlMain=new JPanel(null);
lblUserName=new JLabel("姓名:");
txtUserName=newJTextField();
lblUserPwd=new JLabel("职业:");
txtJob=newJTextField();
lblSex=new JLabel("性别:");
rabM=new JRadioButton("男");
rabW=new JRadioButton("女");
btgSex=newButtonGroup();
lblHobby=new JLabel("兴趣爱好:");
chkRead=new JCheckBox("阅读");
chkSwim=new JCheckBox("游泳");
chkRun=new JCheckBox("跑步");
lblLocation=new JLabel("所在地:");
cmbLocation=new JComboBox();
dcmLocation=new DefaultComboBoxModel();
btnEnsure=new JButton("确认");
btnCancel=new JButton("取消");
txt=newJTextArea();
init();
}//初始化方法
private voidinit() {this.setBounds(550,200,270,400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setTitle("My first window");this.setResizable(false);//设置各个控件的位置和坐标
lblUserName.setBounds(20,20,75,22);
lblUserPwd.setBounds(20,60,75,22);
lblSex.setBounds(20,90,75,22);
lblHobby.setBounds(20, 120, 75, 22);
lblLocation.setBounds(20, 155, 75, 22);
txtUserName.setBounds(90,20,120,22);
txtJob.setBounds(90,60,120,22);
rabM.setBounds(90,90,40,22);
rabW.setBounds(140,90,40,22);
chkRead.setBounds(85, 120, 57, 22);
chkSwim.setBounds(140, 120, 57, 22);
chkRun.setBounds(195, 120, 57, 22);
cmbLocation.setBounds(100, 155, 70, 22);
setCmbLocationData();
btnEnsure.setBounds(50,200,75,22);
btnCancel.setBounds(150,200,75,22);
txt.setBounds(70,240,140,90);
txt.setVisible(false);//将所有控件压入容器中
btgSex.add(rabM);
btgSex.add(rabW);
pnlMain.add(lblUserName);
pnlMain.add(lblUserPwd);
pnlMain.add(txtUserName);
pnlMain.add(txtJob);
pnlMain.add(lblSex);
pnlMain.add(rabM);
pnlMain.add(rabW);
pnlMain.add(lblHobby);
pnlMain.add(chkRead);
pnlMain.add(chkRun);
pnlMain.add(chkSwim);
pnlMain.add(lblLocation);
pnlMain.add(cmbLocation);
pnlMain.add(btnEnsure);
pnlMain.add(btnCancel);
pnlMain.add(txt);this.add(pnlMain);this.setVisible(true);//使用itemListener匿名监听下拉框控件
cmbLocation.addItemListener(newItemListener() {
@Overridepublic voiditemStateChanged(ItemEvent e) {
location=cmbLocation.getSelectedItem().toString();
}
});//使用按钮监听
btnEnsure.addActionListener(new ZuoyeFrame_btnEnsure_ActionListener(this));//使用内部类按钮监听
btnCancel.addActionListener(newZuoyeFrame_btnQuit_ActionListener());
}//设计下拉框里的选项
public voidsetCmbLocationData() {
dcmLocation.addElement("北京");
dcmLocation.addElement("上海");
dcmLocation.addElement("深圳");
dcmLocation.addElement("广州");
dcmLocation.addElement("赣州");
cmbLocation.setModel(dcmLocation);
}//退出按钮监听内部类
class ZuoyeFrame_btnQuit_ActionListener implementsActionListener{
@Overridepublic voidactionPerformed(ActionEvent e) {
txt.setVisible(false);
}
}public static voidmain(String[] args) {newZuoye();
}
}