Java中以GUI编程基础为工具,完成一个四则运算的测试程序,详细要求如下:1、根据需要设计一个用户登陆注册界面,要求用户信息用一个文本文件保存,当该文件不存在,则提示用户注册,将用户的信息写入文件,如果文件存在则进行密码核对,如果密码正确则进入系统。2、根据需要设计使用随机函数生成四则运算的出题界面,用户可以对出题给出答案程序能够自动判断对错,先期设定运算题型,数据大小,题目数量,最后给出成绩
首先是登录界面设置
import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.ObjectOutputStream;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPasswordField;import javax.swing.JTextField;public class LoginForm extends JFrame {
JLabel jlb1=new JLabel("用户名:"); JLabel jlb2=new JLabel("密码:"); JTextField jtf1=new JTextField(); JPasswordField jpf1=new JPasswordField();
JButton jb1=new JButton("登录"); JButton jb2=new JButton("注册"); JButton jb3=new JButton("退出"); MyButtonListener mb1=new MyButtonListener(1);
MyButtonListener mb2=new MyButtonListener(2);
MyButtonListener mb3=new MyButtonListener(3);
public void login(){ //窗体布局 this.setTitle("登录界面"); this.setSize(500, 400); this.setLocationRelativeTo(null); //窗体居中显示,必须在设置大小的后面 this.setAlwaysOnTop(true); //窗体置顶 this.setResizable(false);//禁止改变窗体大小 this.getContentPane().setBackground(Color.pink); //设置背景颜色 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); //仅关闭当前窗体 this.getContentPane().setLayout(null); //窗体布局管理器关闭,针对后面的组件添加
//组件的设置
jlb1.setSize(100, 20); jlb1.setLocation(130, 50); jlb1.setForeground(Color.white); jtf1.setSize(100, 20); jtf1.setLocation(200,50); jlb2.setSize(100, 20); jlb2.setLocation(130, 100); jlb2.setForeground(Color.white); jpf1.setSize(100,20); jpf1.setLocation(200, 100); jb1.setSize(80, 30); jb1.setLocation(110, 200); jb1.addActionListener(mb1); jb2.setSize(80, 30); jb2.setLocation(250, 200); jb2.addActionListener(mb2); //按钮监听 jb3.setSize(80, 30); jb3.setLocation(410, 320); jb3.setBackground(Color.pink); jb3.setBorder(null); //无边框 jb3.addActionListener(mb3); //按钮监听 //组件添加进登录窗体
this.add(jlb1);
this.add(jtf1);
this.add(jlb2);
this.add(jpf1);
this.add(jb1);
this.add(jb2);
this.add(jb3);
//登录界面显示显示
this.setVisible(true); jtf1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
jpf1.grabFocus(); //在输入组件jtf1监听键盘,如果键入回车键,光标定到下一个按钮组件jpf1 } }); }
class MyButtonListener implements ActionListener{ //对三个按钮实现监听 int count; public MyButtonListener(int i){ //1是对登录按钮进行监听,2是对注册按钮进行监听,3是对退出按钮进行监听 this.count=i; } public void actionPerformed(ActionEvent e) {
switch(count){
case 1: if(jtf1.getText().isEmpty()==true || jpf1.getText().isEmpty()==true){ //如果有未输入的空则提示,否则进入账号密码验证的操作 JOptionPane.showMessageDialog(null,"用户名或密码不能为空!");
}
else{
File file=new File("D:\\User.txt"); //如果电脑中不存在该文件,创建文件 if(file.exists()==false){ try { file.createNewFile();
} catch (IOException e1) { e1.printStackTrace(); }
}
String str=jtf1.getText()+" "+jpf1.getText(); //将密码与用户名已字符串形式拼接,再用string类的equals方法进行验证操作
try {
BufferedReader br=new BufferedReader(new FileReader(file)); //以缓冲字符输入流读文件
String line;
boolean bool=false; while((line=br.readLine())!=null){ if(str.equals(line)==true){ //逐行匹配,输入内容与文本文件内容匹配,一致则登录成功,否则登录失败
bool=true;
}
} if(bool){ JOptionPane.showMessageDialog(null,"登录成功!");
dispose();//关闭登录出题
new ArithmeticQuiz(10,1,1,5);//调用四则运算出题 }else{ JOptionPane.showMessageDialog(null,"用户名或密码错误!"); }
br.close();
} catch (IOException e1) { e1.printStackTrace(); }
} break;
case 2: dispose(); //登录窗口消失 new RegisterdForm().registerd();//打开注册界面 break;
case 3: System.exit(0);//虚拟机关闭
break;
}
} }
接下来是注册界面
import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPasswordField;import javax.swing.JTextField;public class RegisterdForm extends JFrame { //创建组件对象
JLabel jlb1=new JLabel("用户名:");
JLabel jlb2=new JLabel("密码:");
JLabel jlb3=new JLabel("确认密码:"); JTextField jtf1=new JTextField(); JPasswordField jpf1=new JPasswordField();
JPasswordField jpf2=new JPasswordField();
JButton jb1=new JButton("确定");
JButton jb2=new JButton("取消"); MyButtonListener1 mb1=new MyButtonListener1(1);
MyButtonListener1 mb2=new MyButtonListener1(2);
public void registerd(){ //窗体布局 this.setTitle("注册界面");
this.setSize(500, 400); this.setLocationRelativeTo(null);
//窗体居中显示
this.setAlwaysOnTop(true); //窗体置顶 this.setResizable(false);//禁止改变窗体大小 this.getContentPane().setBackground(Color.pink); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); //仅关闭当前注册界面 this.getContentPane().setLayout(null); //窗体面板布局管理器关闭 //组件布局 jlb1.setSize(100, 20);
jlb1.setLocation(130, 50); jlb1.setForeground(Color.white); jtf1.setSize(100, 20); jtf1.setLocation(200,50);
jlb2.setSize(100, 20);
jlb2.setLocation(130, 100); jlb2.setForeground(Color.white);
jpf1.setSize(100,20);
jpf1.setLocation(200, 100);
jlb3.setSize(100, 20);
jlb3.setLocation(130, 150); jlb3.setForeground(Color.white);
jpf2.setSize(100,20);
jpf2.setLocation(200, 150);
jb1.setSize(80, 30);
jb1.setLocation(110, 220); jb1.addActionListener(mb1); //按钮监听 jb2.setSize(80, 30);
jb2.setLocation(250, 220); jb2.addActionListener(mb2); //按钮监听
//组件添加进注册窗体
this.add(jlb1);
this.add(jtf1);
this.add(jlb2);
this.add(jpf1);
this.add(jpf2);
this.add(jlb3);
this.add(jb1);
this.add(jb2);
//注册界面显示
this.setVisible(true);
jtf1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
jpf1.grabFocus(); //在输入组件jtf1监听键盘,如果键入回车键,光标定到下一个按钮组件jpf1 } });
jpf1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { jpf2.grabFocus(); //在输入组件jpf1监听键盘,如果键入回车键,光标定到下一个按钮组件jpf2 } }); } //按钮监听
class MyButtonListener1 implements ActionListener{
int count;
public MyButtonListener1(int i){ this.count=i;
} public void actionPerformed(ActionEvent e) {
switch(count){ //1是对确定按钮进行监听,2是对取消按钮进行监听
case 1:
if(jtf1.getText().isEmpty()==true || jpf1.getText().isEmpty()==true || jpf2.getText().isEmpty()==true){//三个文本框输入内容不能为空 JOptionPane.showMessageDialog(null,"用户名或密码不能为空!");
}else if(jpf1.getText().equals(jpf2.getText())==false){ //密码与再次输入密码应一致 JOptionPane.showMessageDialog(null,"再次输入的密码不一致!");
} else{ File file=new File("D:\\User.txt"); if(file.exists()==false){ //如果电脑中不存在该文件,就创建文件
try { file.createNewFile(); } catch (IOException e1) {
e1.printStackTrace(); } }
String str=jtf1.getText()+" "+jpf1.getText(); 将密码与用户名已字符串形式拼接,再用string类的equals方法进行验证操作
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file,true));//以缓冲字符输出流创建文本文件 BufferedReader br=new BufferedReader(new FileReader(file)); //以缓冲字符输入流读文件
String line; boolean bool=false; while((line=br.readLine())!=null){ //输入内容与文本内容逐行匹配,一致,说明文本文件已存在该用户信息,否则将键盘输入的用户信息写入文本文件
if(str.equals(line)==true){
bool=true;} }
if(bool){ JOptionPane.showMessageDialog(null,"用户已存在!");
}else{ JOptionPane.showMessageDialog(null,"注册成功!");
bw.write(str);
bw.newLine();//换行
bw.flush();//刷新
bw.close();//关闭流,
br.close(); }
} catch (IOException e1) { e1.printStackTrace(); }
} break;
case 2: dispose(); //注册窗口消失
new LoginForm().login();
break;
}
最后实习现简单四则运算界面
import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Random;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;public class ArithmeticQuiz extends JFrame {
JLabel jlb=new JLabel();
JTextField jtf1=new JTextField();
JButton jb1=new JButton("下一题"); JButton jb2=new JButton("批改");
JButton jb3=new JButton("退出"); MyButtonListener2 mb1=new MyButtonListener2(1);
MyButtonListener2 mb2=new MyButtonListener2(2);
MyButtonListener2 mb3=new MyButtonListener2(3);
int max;//允许数据的最大值
int min;//允许数据的最小值
int answer;//答案
int score;//计分
int question_type;//题型
int maxQuestions;//题目数量
int counter;//计数器
long startTime;//开始答题时间
long endTime;//答题结束时间
public ArithmeticQuiz(int max,int min,int question_type,int maxQuestions){//有参构造,用来控制数据的最大值和最小值,出题数量,题型
this.max=max;
this.min=min; this.question_type=question_type; this.maxQuestions=maxQuestions; startTime=System.currentTimeMillis();//记录程序开始时间
init();
showquestion();
}
public int getRandNumber(){ //产生随机数 Random rand=new Random(); return rand.nextInt(max-min+1)+min;
}
//出题
public void showquestion(){ if(counter==maxQuestions){//当题目数量等于自己的设定的数量时,出题结束,程序关闭 endTime=System.currentTimeMillis(); JOptionPane.showMessageDialog(null, "答题结束,用时:"+(startTime-endTime)/1000+"秒,共得"+score+"分"); System.exit(0); }
int i=getRandNumber();//产生随机数 int j=getRandNumber();
counter++;//记录出题的数量 switch(question_type){//根据传入的参数值,执行不同的题型
case 1:
answer=i+j; jlb.setText(i+"+"+j+"=");
break;
case 2:
answer=i-j;
jlb.setText(i+"-"+j+"=");
break;
case 3:
answer=i*j; jlb.setText(i+"*"+j+"=");
break;
case 4:
if(j==0){
j=getRandNumber(); }
answer=i/j; jlb.setText(i+"+"+j+"=");
break; }
}
public void init(){
this.setTitle("四则运算练习"); this.setSize(500,400); this.setLocationRelativeTo(null); this.setAlwaysOnTop(true); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.getContentPane().setLayout(null); jlb.setSize(200,40); jlb.setLocation(100, 80); jlb.setFont(new Font("宋体",Font.BOLD,32));
this.add(jlb);
jtf1.setSize(40, 30); jtf1.setLocation(200,85); this.add(jtf1);
jb1.setSize(80, 30); jb1.setLocation(80, 200); jb1.addActionListener(mb1); this.add(jb1);
jb2.setSize(80,30); jb2.setLocation(200,200); jb2.addActionListener(mb2); this.add(jb2); jb3.setSize(80,30); jb3.setLocation(320,200); jb3.addActionListener(mb3); this.add(jb3);
this.setVisible(true);
}
class MyButtonListener2 implements ActionListener{ //对三个按钮实现监听 int number;
public MyButtonListener2(int i){ //1是对下一题按钮进行监听,2是对批改按钮进行监听,3是对退出按钮进行监听 this.number=i; }
public void actionPerformed(ActionEvent e) {
switch(number){
case 1: if(jtf1.getText().equals(String.valueOf(answer))){//用户输入的值与答案匹配,分数加1;并跳转下一个题目
score++;}
showquestion();
jtf1.setText("");//清空输入文本框 break;
case 2: if(jtf1.getText().equals(String.valueOf(answer))){
score++; JOptionPane.showMessageDialog(null, "回答正确");
}else{ JOptionPane.showMessageDialog(null, "回答错误,正确答案是:"+answer); }
break;
case 3: endTime=System.currentTimeMillis();//记录答题结束的时间 JOptionPane.showMessageDialog(null, "答题结束,用时:"+(startTime-endTime)/1000+"秒,共得"+score+"分"); System.exit(0);//虚拟机关闭 } } }