制作用戶登錄界面(JAVA實現)


設計實現如圖所示的個人信息注冊。包含單選按鈕、多選按鈕、下拉框事件。

image

 

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);    
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM