制作用户登录界面(JAVA实现)
设计实现如图所示的个人信息注册。包含单选按钮、多选按钮、下拉框事件。
Zuoye类:
package example02; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ButtonGroup; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextArea; import javax.swing.JTextField; public class Zuoye extends JFrame{ //成员变量 JPanel pnlMain; JLabel lblUserName,lblUserPwd,lblSex,lblHobby,lblLocation; JRadioButton rabM,rabW; ButtonGroup btgSex;//创建是为了使rabM,rabW同组 JCheckBox chkRead,chkSwim,chkRun; JComboBox<String> cmbLocation; DefaultComboBoxModel<String> dcmLocation; String location=""; JTextField txtUserName,txtJob; JButton btnEnsure,btnCancel; JTextArea txt; //构造方法 public Zuoye() { pnlMain=new JPanel(null); lblUserName=new JLabel("姓名:"); txtUserName=new JTextField(); lblUserPwd=new JLabel("职业:"); txtJob=new JTextField(); lblSex=new JLabel("性别:"); rabM=new JRadioButton("男"); rabW=new JRadioButton("女"); btgSex=new ButtonGroup(); lblHobby=new JLabel("兴趣爱好:"); chkRead=new JCheckBox("阅读"); chkSwim=new JCheckBox("游泳"); chkRun=new JCheckBox("跑步"); lblLocation=new JLabel("所在地:"); cmbLocation=new JComboBox<String>(); dcmLocation=new DefaultComboBoxModel<String>(); btnEnsure=new JButton("确认"); btnCancel=new JButton("取消"); txt=new JTextArea(); init(); } //初始化方法 private void init() { 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(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { location=cmbLocation.getSelectedItem().toString(); } }); //使用按钮监听 btnEnsure.addActionListener(new ZuoyeFrame_btnEnsure_ActionListener(this)); //使用内部类按钮监听 btnCancel.addActionListener(new ZuoyeFrame_btnQuit_ActionListener()); } //设计下拉框里的选项 public void setCmbLocationData() { dcmLocation.addElement("北京"); dcmLocation.addElement("上海"); dcmLocation.addElement("深圳"); dcmLocation.addElement("广州"); dcmLocation.addElement("赣州"); cmbLocation.setModel(dcmLocation); } //退出按钮监听内部类 class ZuoyeFrame_btnQuit_ActionListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { txt.setVisible(false); } } public static void main(String[] args) { new Zuoye(); } }
ZuoyeFrame_btnEnsure_ActionListener类
package example02; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //登陆按钮监听类 public class ZuoyeFrame_btnEnsure_ActionListener implements ActionListener{ Zuoye zy; String gender=""; String hobby=""; public ZuoyeFrame_btnEnsure_ActionListener(Zuoye zy) { this.zy = zy; } @Override public void actionPerformed(ActionEvent e) { zy.txt.setVisible(true); zy.txt.setLineWrap(true); zy.txt.setText("姓名:"+zy.txtUserName.getText()+'\n'); zy.txt.append("职业:"+zy.txtJob.getText()+'\n'); if(zy.rabM.isSelected()) { gender+="男"; }else if(zy.rabW.isSelected()) { gender+="女"; } zy.txt.append("性别:"+gender+'\n'); if(zy.chkRead.isSelected()) { hobby+="阅读"; } if(zy.chkSwim.isSelected()) { hobby+="游泳"; } if(zy.chkRun.isSelected()) { hobby+="跑步"; } zy.txt.append("兴趣爱好:"+hobby+'\n'); zy.txt.append("所在地:第"+(zy.cmbLocation.getSelectedIndex()+1)+"项 "+zy.location); } }
天晴了,起飞吧