Java Swing(純個人代碼改善版):登錄界面和登錄后界面

先出純個人代碼改善版,以后有空的時候,可能會出配套的個人理解的解析版的專欄和視頻,以后可能會出人工智能,游戲制作,有功能的機(jī)械制造的專欄視頻!
寫出無bug的代碼,給代碼找錯誤,修改和理解代碼不容易,大家可以常照著我寫了好多遍的代碼打很多遍,并且看我的理解等就會寫和理解類似的代碼了,記得三連和關(guān)注?。?/strong>


package swing;
public class mains {
public static void main(String[] args) {
new swing();
}
}


package swing;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class shijian implements ActionListener, FocusListener {
static swing d = null;
/* 下面的是構(gòu)造方法,無void */
public shijian(swing dl) {
d = dl;
}
@Override
/*
* actionperformed方法是ActionListener監(jiān)聽事件中的一個重寫方法,
* 如果要求按鈕執(zhí)行一些動作,可以在這個方法中添加、修改、刪除以及判斷。
*/
public void actionPerformed(ActionEvent arg0) {
/* 當(dāng)按鈕中字符為"登錄"時,就執(zhí)行equals("登錄")中的內(nèi)容 */
if (arg0.getActionCommand().equals("已經(jīng)注冊過,可直接登錄")) {
/* trim()函數(shù)移除字符串兩側(cè)的空白字符或其他預(yù)定義字符。 */
String uname = swing.uname.getText().trim();
String pwd = swing.pwd.getText().trim();
if (uname.isEmpty() || pwd.isEmpty()) {
JOptionPane.showMessageDialog(null, "用戶名或密碼為空");
} else {
if (uname.equals(yonghu.uname) && pwd.equals(yonghu.pwd)) {
JOptionPane.showMessageDialog(null, "用戶名和密碼輸入正確,可以登錄");
new chenggongjiemian();
d.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "用戶名或密碼輸入錯誤");
}
}
}
else if (arg0.getActionCommand().equals("沒注冊過,先注冊")) {
JOptionPane.showMessageDialog(null, "沒注冊過,先注冊");
} else {
int o = JOptionPane.showConfirmDialog(null, "是否確認(rèn)關(guān)閉頁面");
if (o == 0) {
System.exit(0);
}
}
}
/*
* arg是java主函數(shù)的入口參數(shù),它是一個字符串?dāng)?shù)組變量 void focusGained(FocusEvent e):當(dāng)獲得焦點(diǎn)時發(fā)生.
* void focusLost(FocusEvent e):當(dāng)失去焦點(diǎn)時發(fā)生.
*/
@Override
public void focusGained(FocusEvent arg0) {
if (arg0.getSource() == d.uname) {
System.out.println("選中了用戶名框");
} else if (arg0.getSource() == d.pwd) {
System.out.println("選中了密碼框");
} else if (arg0.getSource() == d.pwd1) {
System.out.println("選中了確認(rèn)密碼框");
}
}
@Override
public void focusLost(FocusEvent arg0) {
if (arg0.getSource() == d.uname) {
/* getSource獲得所選 */
String uname = d.uname.getText();
if (uname.isEmpty()) {
d.lb_uname_panduan.setText("請輸入用戶名");
d.lb_uname_panduan.setForeground(Color.red);
} else {
if (uname.length() > 12 || uname.length() == 0) {
d.lb_uname_panduan.setText("用戶名格式輸入錯誤");
d.lb_uname_panduan.setForeground(Color.red);
} else {
d.lb_uname_panduan.setText("用戶名輸入正確");
d.lb_uname_panduan.setForeground(Color.green);
}
}
} else if (arg0.getSource() == d.pwd) {
String pwd = d.pwd.getText().trim();
if (pwd.length() < 6 || pwd.length() == 0) {
d.lb_pwd_panduan.setText("密碼格式輸入錯誤,不可少于6位");
d.lb_pwd_panduan.setForeground(Color.red);
} else {
d.lb_pwd_panduan.setText("輸入正確");
d.lb_pwd_panduan.setForeground(Color.green);
}
} else if (arg0.getSource() == d.pwd1) {/*做判斷兩次密碼是否一致的判斷
先獲取第一次輸入的密碼,再獲取第二次輸入的密碼,然后比較一下兩次密碼是否一致不就完啦*/
String pwd1 = d.pwd.getText().trim();// 獲取第一次的密碼
String pwd2 = d.pwd1.getText().trim();// 獲取第二次輸入的密碼.
if (pwd1.equals(pwd2)) {
d.lb_pwd_queren.setText("輸入正確");
d.lb_pwd_queren.setForeground(Color.GREEN);
} else {
d.lb_pwd_queren.setText("兩次密碼輸入不一致");
d.lb_pwd_queren.setForeground(Color.red);
}
}
}
}
public class swing extends JFrame {
static JCheckBox aihao1, aihao2, aihao3 = null;
static JComboBox com1 = null;
static JButton DengLuAnNu, QuXiaoAnNu, ZhuCeMiMaAnNu = null;
static JTextArea jt1 = null;
static JLabel lb_uname, lb_qrmima, lb_sex, lb_aihao, lb_address,
lb_jieshao = null;
static JLabel lb_uname_panduan, lb_pwd_panduan, lb_pwd,
lb_pwd_queren = null;
static JPasswordField pwd, pwd1 = null;
static JRadioButton sex_man, sex_woman = null;
static JTextField uname;
shijian sj = new shijian(this);
/* this就是指對象自己,表示自己,在那個對象的成員函數(shù)里出現(xiàn)就表示哪個對象 */
public swing() {
/* 總界面設(shè)置 */
this.setTitle("登錄界面");
this.setSize(700, 600);
this.setLocationRelativeTo(null);
this.setLayout(null);
/* 用戶名 */
lb_uname = new JLabel("用戶名");
this.add(lb_uname);
lb_uname.setBounds(100, 100, 40, 30);
/* setBounds(x,y,size_x,size_y) */
uname = new JTextField(20);
uname.setBounds(180, 100, 140, 30);
uname.addFocusListener(sj);
this.add(uname);
lb_uname_panduan = new JLabel("用戶名長度不能大于12位");
lb_uname_panduan.setForeground(Color.red);
lb_uname_panduan.setBounds(330, 100, 250, 30);
this.add(lb_uname_panduan);
/* 密 碼 */
lb_pwd = new JLabel("密 碼");
this.add(lb_pwd);
lb_pwd.setBounds(100, 140, 40, 30);
pwd = new JPasswordField();
pwd.setBounds(180, 140, 140, 30);
pwd.addFocusListener(sj);
this.add(pwd);
lb_pwd_panduan = new JLabel("請輸入密 碼,不可少與6位");
lb_pwd_panduan.setForeground(Color.red);
lb_pwd_panduan.setBounds(330, 140, 240, 30);
this.add(lb_pwd_panduan);
// lb_pwd_panduan = new JLabel("請確認(rèn)密碼");
// lb_pwd_panduan.setForeground(Color.red);
// lb_pwd_panduan.setBounds(330, 140, 150, 30);
// this.add(lb_pwd_panduan);
/* 確認(rèn)密碼 */
lb_qrmima = new JLabel("確認(rèn)密碼");
lb_qrmima.setBounds(100, 180, 70, 30);
this.add(lb_qrmima);
pwd1 = new JPasswordField(20);
pwd1.setBounds(180, 180, 140, 30);
// pwd1.addFocusListener(sj);
pwd1.addFocusListener(new shijian(this));
this.add(pwd1);
lb_pwd_queren = new JLabel("請輸入和上一 次一致的密碼");
lb_pwd_queren.setForeground(Color.red);
lb_pwd_queren.setBounds(330, 180, 180, 30);
this.add(lb_pwd_queren);
lb_pwd_queren.addFocusListener(sj);
lb_sex = new JLabel("性別");
lb_sex.setBounds(100, 210, 70, 30);
this.add(lb_sex);
sex_woman = new JRadioButton("女");
sex_man = new JRadioButton("男", true);
sex_man.setBounds(180, 210, 70, 30);
sex_woman.setBounds(260, 210, 70, 30);
this.add(sex_man);
this.add(sex_woman);
ButtonGroup bg1 = new ButtonGroup();
bg1.add(sex_man);
bg1.add(sex_woman);
lb_aihao = new JLabel("愛好");
lb_aihao.setBounds(100, 250, 70, 30);
this.add(lb_aihao);
aihao1 = new JCheckBox("唱");
aihao2 = new JCheckBox("跳");
aihao3 = new JCheckBox("rap");
aihao1.setBounds(180, 250, 70, 30);
aihao2.setBounds(250, 250, 70, 30);
aihao3.setBounds(320, 250, 70, 30);
this.add(aihao1);
this.add(aihao2);
this.add(aihao3);
lb_address = new JLabel("家庭地址");
lb_address.setBounds(100, 290, 70, 30);
this.add(lb_address);
com1 = new JComboBox();
com1.addItem("請選擇家庭地址");
com1.addItem("北京");
com1.addItem("上海");
com1.addItem("廣州");
com1.setBounds(180, 290, 140, 30);
this.add(com1);
lb_jieshao = new JLabel("個人介紹");
lb_jieshao.setBounds(100, 330, 70, 30);
this.add(lb_jieshao);
jt1 = new JTextArea();
jt1.setColumns(8);
jt1.setRows(4);
jt1.setBounds(180, 330, 140, 40);
jt1.setLineWrap(true);
this.add(jt1);
JButton btn_dl = new JButton("已經(jīng)注冊過,可直接登錄");
btn_dl.setBounds(20, 400, 180, 35);
QuXiaoAnNu = new JButton("取消");
ZhuCeMiMaAnNu = new JButton("沒注冊過,先注冊");
ZhuCeMiMaAnNu.setBounds(368, 400, 180, 35);
QuXiaoAnNu.setBounds(240, 400, 80, 35);
btn_dl.addActionListener(sj);
ZhuCeMiMaAnNu.addActionListener(sj);
QuXiaoAnNu.addActionListener(sj);
this.add(btn_dl);
this.add(ZhuCeMiMaAnNu);
this.add(QuXiaoAnNu);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class yonghu {
static String pwd = "pwdpwd";
static String uname = "1";
}


package swing;
import javax.swing.JFrame;
public class chenggongjiemian extends JFrame {
public chenggongjiemian() {
this.setTitle("登錄后的界面");
this.setLayout(null);
this.setSize(800, 800);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}

完整效果展示:

出錯時要有提醒:




