实验任务4和实验任务5
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验任务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的主要内容,如果未能解决你的问题,请参考以下文章