?? land.java
字號:
package com.exam;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Land implements ActionListener
{
private ManageUser newUser;
private JFrame frame;
private Container contentPane;
//Land界面元素
private JButton clearBtn,newBtn,okBtn,exitBtn;
private JTextField userText;
private JPasswordField pwdText;//顯示密碼
//NewUser界面
private JButton clearBtn1,okBtn1,exitBtn1;
private JTextField userText1;
private JPasswordField pwdText1,pwdText2;//顯示密碼
//—————————————————————————————————
public Land()
{
frame=new JFrame("用戶登陸");
frame.setBounds(300,170,300,170);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//設置大小可不可以改變
frame.setResizable(false);
contentPane=frame.getContentPane();
initGUI();
}
//********************************************************
public void initGUI()
{
contentPane.setLayout(new CardLayout());
contentPane.add(getPanel(1),"");
}
//————————————————————————————————
public void selectPanel(int id)
//圖片管理及加載(到contentPane)
{
contentPane.add(getPanel(id),"");
CardLayout c=(CardLayout)contentPane.getLayout();
c.next(contentPane);
}
//————————————————————————————————
public JPanel getPanel(int id)
//專門用于按照id生產圖片的工廠
{
JPanel pAll=null;
switch(id)
{
//@@@@@
//產生Land界面
case 1:
{
pAll=new JPanel(new BorderLayout());
//用戶名
userText=new JTextField(10);
JPanel p1=new JPanel(new FlowLayout());
p1.add(new JLabel("用戶名:"));
p1.add(userText);
//密碼
pwdText=new JPasswordField(10);
JPanel p2=new JPanel(new FlowLayout());
p2.add(new JLabel("密 碼 :"));
p2.add(pwdText);
//按鈕
clearBtn=new JButton("清除");
newBtn=new JButton("注冊");
okBtn=new JButton("確定");
exitBtn=new JButton("退出");
JPanel p3=new JPanel(new FlowLayout());
p3.add(clearBtn);
p3.add(newBtn);
p3.add(okBtn);
p3.add(exitBtn);
JPanel p=new JPanel(new GridLayout(2,1));
p.add(p1);
p.add(p2);
pAll.add(p,BorderLayout.CENTER);
pAll.add(p3,BorderLayout.SOUTH);
clearBtn.addActionListener(this);
newBtn.addActionListener(this);
okBtn.addActionListener(this);
exitBtn.addActionListener(this);
userText.addActionListener(this);
pwdText.addActionListener(this);
}break;
//————————————————————————————————————————
//@@@@@
//產生NewUser界面
case 2:
{
pAll=new JPanel(new BorderLayout());
//用戶名
userText1=new JTextField(10);
JPanel p1=new JPanel(new FlowLayout());
p1.add(new JLabel("用 戶 名 :"));
p1.add(userText1);
//密碼
pwdText1=new JPasswordField(10);
JPanel p2=new JPanel(new FlowLayout());
p2.add(new JLabel(" 密 碼 :"));
p2.add(pwdText1);
//密碼驗證
pwdText2=new JPasswordField(10);
JPanel p3=new JPanel(new FlowLayout());
p3.add(new JLabel("密碼驗證 :"));
p3.add(pwdText2);
//按鈕
clearBtn1=new JButton("清除");
okBtn1=new JButton("確定");
exitBtn1=new JButton("返回");
JPanel p4=new JPanel(new FlowLayout());
p4.add(clearBtn1);
p4.add(okBtn1);
p4.add(exitBtn1);
JPanel p=new JPanel(new GridLayout(3,1));
p.add(p1);
p.add(p2);
p.add(p3);
pAll.add(p,BorderLayout.CENTER);
pAll.add(p4,BorderLayout.SOUTH);
clearBtn1.addActionListener(this);
okBtn1.addActionListener(this);
exitBtn1.addActionListener(this);
userText1.addActionListener(this);
pwdText1.addActionListener(this);
pwdText2.addActionListener(this);
}break;
}
return pAll;
}
//————————————————————————————————————————
//事件處理類
public void actionPerformed(ActionEvent e)
{
//對文本框進行監聽,一回車進填寫密碼項
if(e.getSource()==userText1)
{
if(!checkName())
return;
}
//當一回車時自動登陸
if(e.getSource()==pwdText||e.getSource()==okBtn)
{
//如果登陸用戶名為空的話直接返回
if(!checkName())
return;
//若密碼為空的話提示密碼不能為空
if(pwdText.getText().trim().length()==0)
{
(new InformFrame(1,"密碼名不能為空")).go();
pwdText.setText("");
pwdText.grabFocus();
}
//若密碼擁護名都不為空的話進行驗證
else
{
//用戶名和密碼的驗證
String name=userText.getText().trim();
String pwd=pwdText.getText().trim();
//若次用戶名和密碼存在的話就進入答題系統
if(ManageUser.isFind(name,pwd))
{
//彈出question
(new Question()).go();
}
//若不存在就提示輸入不正確
else
{
//錯誤信息提示!
(new InformFrame(1,"用戶名或密碼不正確!")).go();
}
}
}
//對按鈕進行監聽
if(e.getSource()==clearBtn)
{
userText.setText("");
pwdText.setText("");
userText.grabFocus();//獲得焦點,讓光標返回到用戶上
}
if(e.getSource()==newBtn)
{
selectPanel(2);
userText1.grabFocus();
}
if(e.getSource()==exitBtn)
{
(new InformFrame(2,"真的要退出嗎?")).go();
}
//__________________________________________________________________________________________________
//__________________________________________________________________________________________________
if(e.getSource()==pwdText1||e.getSource()==pwdText2||e.getSource()==okBtn1)
{
//用戶名沒有檢查???????????????????????????
//密碼和驗證密碼不能為空
if(pwdText1.getText().trim().length()==0||pwdText1.getText().trim().length()==0)
{
(new InformFrame(1,"密碼名不能為空")).go();
pwdText1.setText("");
pwdText1.grabFocus();
}
//在都不為空的前提下判斷兩次輸入的秘密是否一致
else
{ if(!(pwdText1.getText().trim()).equals(pwdText2.getText().trim()))
{
(new InformFrame(1,"兩次密碼輸入不一致,請重新輸入")).go();
pwdText1.setText("");
pwdText2.setText("");
pwdText1.grabFocus();
}
//若兩次輸入的一致判斷是否注冊的新用戶為已經存在的用戶
else
{
//用戶名和密碼的驗證
String name=userText1.getText().trim();
String pwd=pwdText1.getText().trim();
//如果確實為新用戶的話就創建一個新用戶
if(!ManageUser.isFind(name,pwd))
{
//創建新用戶
newUser.add(name,pwd);
(new InformFrame(1,"創建成功")).go();
(new Question()).go();
}
//如果新注冊的用戶已經存在就提示出錯并且重新注冊
else
{
//錯誤信息提示!
frame.dispose();
(new InformFrame(1,"用戶名或密碼已存在!")).go();
frame.dispose();
userText1.setText("");
pwdText1.setText("");
pwdText2.setText("");
pwdText1.grabFocus();
}
}
}
}
if(e.getSource()==exitBtn1)
{
selectPanel(1);
userText.grabFocus();
}
}
//***********************************************************
public boolean checkName()
{
if(userText.getText().trim().length()==0)//判斷用戶名是否為空
{
(new InformFrame(1,"用戶名不能為空")).go();
userText.setText(""); userText.grabFocus();//若為空,則用戶名重新輸入
return false;
}
else
pwdText.grabFocus();
return true;
}
public void go()
{
frame.setVisible(true);
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^上
//專門用于生產用戶提示的各種信息框
class InformFrame
{
private JFrame frame;
private Container contentPane;
private JLabel lbl;
private JButton okBtn,exitBtn;
private int id;
private String informText;
public InformFrame(int id,String informText)
{
frame=new JFrame("信息提示!");
frame.setBounds(200,500,300,170);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//設置大小可不可以改變
frame.setResizable(false);
contentPane=frame.getContentPane();
this.id=id;
this.informText=informText;
initGUI();
}
public void initGUI()
{
contentPane.setLayout(new BorderLayout());
lbl=new JLabel(informText);
contentPane.add(lbl,BorderLayout.CENTER);
contentPane.add(getPanel(id),BorderLayout.SOUTH);
}
//生產加載到contentPane中SOUTH區域的JPanel
public JPanel getPanel(int id)
{
JPanel p=null;
switch(id)
{
case 1:{
okBtn=new JButton("確定");
p=new JPanel(new FlowLayout());
p.add(okBtn);
okBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
});
}break;
case 2:{
okBtn=new JButton("是");
exitBtn=new JButton("否");
p=new JPanel(new FlowLayout());
p.add(okBtn);
p.add(exitBtn);
okBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
exitBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
});
}break;
}
return p;
}
public void go()
{
frame.setVisible(true);
}
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^下
public static void main(String args[])
{
(new Land()).go();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -