實驗任務4和實驗任務5
實驗任務4
設計思想:
首先定義一個類用創建一個登陸界面,界面有登錄和註冊的功能,註冊按鈕觸發的事件是調用註冊函數
創建一個註冊界面用於註冊用戶,註冊完之後的用戶名都可以用於登陸,同時還有一個函數用戶驗證碼的
更新。另外聲明一個用戶的信息的類,用於用戶的信息的設定和調用。
程序流程圖:
程序源代碼:
package task4;
import java.awt.*;
import java.util.List;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class User //定義用戶的信息的類
{
private String id;
private String code;
private String number;
public void set(String a,String b,String c)
{
id=a;
code=b;
number=c;
}
public String getid()
{
return id;
}
public String getcode()
{
return code;
}
public String getnumber()
{
return number;
}
}
class Interface1 extends Example implements ActionListener //定義一個提示窗體
{
JFrame J=new JFrame();
JPanel Interface = new JPanel(null)
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
ImageIcon img = new ImageIcon("D:\\學習文件\\圖形界面背景圖片\\8.jpg");
g.drawImage(img.getImage(),0,0,getWidth(),getHeight(),img.getImageObserver());
}
};
JButton Determine=new JButton("確定");
Interface1(int a)
{
if(a==1)
{
JLabel Prompt=new JLabel("用戶名不存在!");
Prompt.setBounds(80, 30, 100, 23);
Interface.add(Prompt);
setTitle("錯誤");
}
if(a==2)
{
JLabel Prompt=new JLabel("密碼錯誤!");
Prompt.setBounds(95, 30, 100, 23);
Interface.add(Prompt);
setTitle("錯誤");
}
if(a==3)
{
JLabel Prompt=new JLabel("驗證碼錯誤!");
Prompt.setBounds(88, 30, 100, 23);
Interface.add(Prompt);
setTitle("錯誤");
}
if(a==4)
{
JLabel Prompt=new JLabel("登陸成功!");
Prompt.setBounds(95, 30, 100, 23);
Interface.add(Prompt);
setTitle("提示");
}
if(a==5)
{
JLabel Prompt=new JLabel("註冊成功!");
Prompt.setBounds(95, 30, 100, 23);
Interface.add(Prompt);
setTitle("提示");
}
if(a==6)
{
JLabel Prompt=new JLabel("密碼不一致!");
Prompt.setBounds(88, 30, 100, 23);
Interface.add(Prompt);
setTitle("錯誤");
}
if(a==7)
{
JLabel Prompt=new JLabel("該用戶已存在!");
Prompt.setBounds(75, 30, 100, 23);
Interface.add(Prompt);
setTitle("錯誤");
}
Determine.setBounds(92,60,60,25);
Interface.add(Determine);
getContentPane().add(Interface);
setSize(270,150);
setVisible(true);
setLayout(null);
setResizable(false);
Determine.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void actionPerformed(ActionEvent e)
{
dispose();
}
}
class LoginInterface extends Example implements ActionListener //定義一個登陸界面的窗體
{
JPanel Interface = new JPanel(null) //聲明一個面板對象
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
ImageIcon img = new ImageIcon("D:\\學習文件\\圖形界面背景圖片\\2.jpg");
g.drawImage(img.getImage(),0,0,getWidth(),getHeight(),img.getImageObserver());
}
};
String A=new String(getRandom());
String a=new String();
String b=new String();
String c=new String();
String d=new String();
String f=new String();
JLabel[] Name = new JLabel [3]; //聲明3個標簽對象
JTextField[] txt = new JTextField [3]; //聲明3個文本框對象
JButton Land = new JButton("登錄");
JButton Register = new JButton("快速註冊");
JButton Code = new JButton(A); //聲明一個按鈕對象,裏面的內容是6位字符串
JButton Refresh = new JButton("刷新");
JLabel[] ReName = new JLabel [5]; //聲明5個標簽對象
JTextField[] Retxt = new JTextField [5]; //聲明5個文本框對象
JButton ReRegister = new JButton("註冊");
JButton ReCode = new JButton(A); //聲明一個按鈕對象,裏面的內容是6位字符串
JButton ReRefresh = new JButton("刷新");
JFrame F;
ArrayList<User> list = new ArrayList<User> ();
LoginInterface()
{
for(int i=0;i<3;i++)
{
Name[i]=new JLabel();
txt[i]=new JTextField();
}
Name[0]=new JLabel("用戶名:"); //148行到160行設置按鈕,文本框的位置和大小
Name[0].setBounds(50, 30, 50, 23);
txt[0].setBounds(95,30,180,23);
Name[1]=new JLabel("密碼:");
Name[1].setBounds(63, 65, 50, 23);
txt[1].setBounds(95,65,180,23);
Name[2]=new JLabel("驗證碼:");
Name[2].setBounds(50, 100, 50, 23);
txt[2].setBounds(95,100,90,23);
Land.setBounds(85,150,200,25);
Register.setBounds(85,190,200,25);
Code.setBounds(190,100,83,23);
Refresh.setBounds(278,100,60,23);
for(int i=0;i<3;i++) //161行到165行是把按鈕和文本框對象放入面板對象Interface中
{
Interface.add(Name[i]);
Interface.add(txt[i]);
}
Interface.add(Land); //166-169把組件添加到面板對象Tnterface中
Interface.add(Register);
Interface.add(Code);
Interface.add(Refresh);
getContentPane().add(Interface); //把面板對象Interface放入窗體中
Code.addActionListener(this); //Code按鈕的監聽器
Refresh.addActionListener(this); //Refresh按鈕的監聽器
Land.addActionListener(this);
Register.addActionListener(this);
setTitle("請登錄");
setSize(400,300); //設置窗體的大小
setVisible(true); //設置窗體為可見
setLayout(null); //設置為空布局
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) //觸發事件後的處理方法
{
if(e.getSource()==Code||e.getSource()==Refresh)
{
A=new String(getRandom());
Code.setText(A);
}
if(e.getSource()==ReRefresh||e.getSource()==ReCode)
{ A=new String(getRandom());
ReCode.setText(A);
}
if(e.getSource()==Land)
{
a=txt[0].getText();
b=txt[1].getText();
c=txt[2].getText();
if(c.equals(A))
{
for(int i=0;i<list.size();i++)
{
if(a.equals(list.get(i).getid()))
{
if(b.equals(list.get(i).getcode()))
new Interface1(4) ;
else
new Interface1(2);
break;
}
if(i==list.size()-1)
new Interface1(1);
}
}
else
new Interface1(3);
}
if(e.getSource()==Register)
{
addUser();
}
if(e.getSource()==ReRegister)
{
a=Retxt[0].getText();
b=Retxt[1].getText();
c=Retxt[2].getText();
d=Retxt[3].getText();
f=Retxt[4].getText();
if(f.equals(A))
{
User A=new User();
if(list.size()==0)
{
if(b.equals(c))
{
new Interface1(5);
A.set(a,b,d);
list.add(A);
F.dispose();
}
else
new Interface1(6);
}
else
{
for(int i=0;i<list.size();i++)
{
if(a.equals(list.get(i).getid()))
{
new Interface1(7);
break;
}
if(i==list.size()-1)
{
if(b.equals(c))
{
new Interface1(5);
A.set(a,b,d);
list.add(A);
dispose();
}
else
new Interface1(6);
break;
}
}
}
}
else
new Interface1(3);
}
}
public String getRandom() //生成6位字符串的方法
{
Random rand=new Random(new Date().getTime());
int cnt=62;
char[] s=new char[6];
for(int i=0;i<6;i++)
{
int v=rand.nextInt(cnt);
if(v<10)
s[i]=(char)(‘0‘+v);
else if(v<36)
s[i]=(char)(v-10+‘A‘);
else
s[i]=(char)(v-36+‘a‘);
}
return new String(s);
}
public void addUser() //生成一個註冊用戶的窗體
{
F=new JFrame();
JPanel ReInterface = new JPanel(null)
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
ImageIcon img = new ImageIcon("D:\\學習文件\\圖形界面背景圖片\\11.jpg");
g.drawImage(img.getImage(),0,0,getWidth(),getHeight(),img.getImageObserver());
}
};
for(int i=0;i<5;i++)
{
ReName[i]=new JLabel();
Retxt[i]=new JTextField();
}
ReName[0]=new JLabel("用戶名:");
ReName[0].setBounds(55, 30, 50, 23);
Retxt[0].setBounds(100,30,180,23);
ReName[1]=new JLabel("密碼:");
ReName[1].setBounds(68, 65, 50, 23);
Retxt[1].setBounds(100,65,180,23);
ReName[2]=new JLabel("確認密碼:");
ReName[2].setBounds(42, 100, 80, 23);
Retxt[2].setBounds(100,100,180,23);
ReName[3]=new JLabel("手機號:");
ReName[3].setBounds(55, 135, 50, 23);
Retxt[3].setBounds(100,135,180,23);
ReName[4]=new JLabel("驗證碼:");
ReName[4].setBounds(55, 170, 50, 23);
Retxt[4].setBounds(100,170,90,23);
ReRegister.setBounds(90,205,200,25);
ReCode.setBounds(200,170,83,23);
ReRefresh.setBounds(290,170,60,23);
for(int i=0;i<5;i++)
{
ReInterface.add(ReName[i]);
ReInterface.add(Retxt[i]);
}
ReInterface.add(ReCode);
ReInterface.add(ReRefresh);
ReInterface.add(ReRegister);
F.getContentPane().add(ReInterface);
ReCode.addActionListener(this);
ReRefresh.addActionListener(this);
ReRegister.addActionListener(this);
F.setTitle("快速註冊");
F.setSize(400,300);
F.setVisible(true);
F.setLayout(null);
F.setResizable(false);
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
F.validate();
}
}
public class Example extends JFrame
{
public static void main(String[] args)
{
new LoginInterface();
}
}
結果截圖:
實驗任務5:
設計思想:
首先創建一個遊戲的面板,同時產生一個數,之後輸入一個數之後,之後判斷是否猜對,同時給出
相應的提示,選擇繼續遊戲的話重新產生一個隨機數,重新開始,結果遊戲則程序結束,窗體關閉。
程序流程圖:
程序源代碼:
package task5;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class GuessNumber extends JFrame implements ActionListener
{
JPanel Interface = new JPanel(null)
{
int x=0,y=0;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
ImageIcon img = new ImageIcon("D:\\學習文件\\圖形界面背景圖片\\2.jpg");
g.drawImage(img.getImage(),x,y,getWidth(),getHeight(),img.getImageObserver());
}
};
JLabel Name = new JLabel("請輸入一個數字:");
JLabel Prompt = new JLabel("(範圍1~100)");
JTextField txt = new JTextField();
JButton Determine = new JButton("確定");
JButton Result = new JButton("");
JButton ResumeGame = new JButton("繼續遊戲");
JButton EndGame = new JButton("結束遊戲");
Random rand=new Random(new Date().getTime());
int Cnt=100;
int Number=rand.nextInt(Cnt);
int b;
GuessNumber()
{
Name.setBounds(65,55,180,23);
txt.setBounds(165,55,70,23);
Prompt.setBounds(78,70,180,23);
Determine.setBounds(240,55,60,23);
Result.setBounds(135,100,100,25);
ResumeGame.setBounds(85,140,200,25);
EndGame.setBounds(85,180,200,25);
Interface.add(Name);
Interface.add(txt);
Interface.add(Prompt);
Interface.add(Determine);
Interface.add(Result);
Interface.add(ResumeGame);
Interface.add(EndGame);
getContentPane().add(Interface);
txt.addActionListener(this);
Determine.addActionListener(this);
ResumeGame.addActionListener(this);
EndGame.addActionListener(this);
setSize(400,300);
setVisible(true);
setLayout(null);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==txt)
{
String a=txt.getText();
b=Integer.parseInt(a);
if(b>Number)
Result.setText("大了");
else if(b==Number)
Result.setText("猜對了");
else
Result.setText("小了");
}
else if(e.getSource()==ResumeGame)
{
Number=rand.nextInt(Cnt);
txt.setText(" ");
Result.setText(" ");
}
else if(e.getSource()==Determine)
{
String a=txt.getText();
b=Integer.parseInt(a);
if(b>Number)
Result.setText("大了");
else if(b==Number)
Result.setText("猜對了");
else
Result.setText("小了");
}
else
{
System.exit(0);
}
}
}
public class Example
{
public static void main(String[] args)
{
GuessNumber A=new GuessNumber();
A.setTitle("猜數字"); //設置窗體標題
}
}
結果截圖:
實驗任務4和實驗任務5