//第一个类:登陆界面
import java.awt.*
;
import java.awt.event.*
;
import javax.swing.*
;
import javax.swing.border.*
;
public class Login
extends JFrame
implements ActionListener{
JTextField f1;
JTextField f2;
JButton b1;
JButton b2;
JPanel p5;
Image img = Toolkit.getDefaultToolkit().getImage("1.jpg"
);
Login(){
this.setTitle("四则运算的系统登录"
);
this.setSize(360,250
);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
//截取屏幕大小
this.setLocation(screensize.width/2-400/2, screensize.height/2-400/2);
//居中计算
Container cp=
getContentPane();
Label l1=
new Label("用户:"
);
Label l2=
new Label("密码:"
);
JPanel p1=
new JPanel();
JPanel p2=
new JPanel();
JPanel p3=
new JPanel();
JPanel p4=
new JPanel();
p5=
new JPanel(){
//将图片添加到面板5
protected void paintChildren(Graphics g) {
int imgwidth=img.getWidth(
this);
//获得图片的宽高
int imgheight=img.getHeight(
this);
int fwidth=getWidth();
//获得窗口的大小
int fheight=
getHeight();
int x=(fwidth-imgwidth)/2
;
int y=(fheight-imgheight)/2
;
g.drawImage(img,x,y,this);
super.paintChildren(g);
}
};
f1=
new JTextField(15
);
f2=
new JPasswordField(15
);
b1=
new JButton("登录"
);
b2=
new JButton("重置"
);
p1.setBackground(Color.orange);
p2.add(l1);
p2.add(f1);
p2.setBorder(new MatteBorder(0,0,0,0,Color.black));
//创建具有指定 insets和颜色的衬边边框
p2.setBackground(Color.ORANGE);
p3.add(l2);
p3.add(f2);
p3.setBackground(Color.ORANGE);
p3.setBorder(new MatteBorder(0,0,0,0
,Color.black));
p4.add(b1);
p4.add(b2);
p4.setBorder(new MatteBorder(0,0,0,0
,Color.black));
p4.setBackground(Color.ORANGE);
p5.setLayout(new FlowLayout(FlowLayout.CENTER,20,20
));
p5.add(p2);
p5.add(p3);
p5.add(p4);
cp.add(p5,BorderLayout.CENTER);
b1.addActionListener(this);
b2.addActionListener(this);
this.setResizable(
false);
//设置不可以改变大小
this.setVisible(
true);
//设置窗口可见
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
//当获取的资源是按钮1时
{
//当输入的用户名为”詹泽娟“并且密码为”123456“,则切换到四则运算的界面
if((f1.getText()).equals("詹泽娟")&&(f2.getText()).equals("123456"
))
{
new MyWindow();
this.setVisible(
false);
}
else
JOptionPane.showMessageDialog(null, "用户名或者密码不正确!"
);
}
if(e.getSource()==
b2)
{
f1.setText(""
);
f2.setText(""
);
}
}
public static void main(String[] args){
new Login();
}
}
//第二个类:四则运算界面
import java.awt.*
;
import javax.swing.*
;
import java.awt.event.*
;
import java.math.*
;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.math.*
;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.JOptionPane;
//消息提示框
import javax.swing.UIManager;
class MyWindow
extends JFrame
implements ActionListener{
JTextField text;//提示输入的个数
JTextField t;
//随机题目
JTextField at;
//用于输入答案
JTextField st;
//判断对错
JTextField tt;
//显示时间
JTextField lt;
//设置时间
JMenuBar menuBar;
JMenu menu;
JRadioButtonMenuItem item;
private Timer tmr;
JLabel L1;
JLabel L2;
JLabel L3;
JLabel L4;
JLabel L5;
JLabel L6;
JButton B1,B2;
JPanel p1,p2,p3;
String parten="0.00";
//设置格式为“0.00”
DecimalFormat decimal=
new DecimalFormat(parten);
String str;// str=decimal.format(sum);
double seconds;
//时间“秒”
long startTime,endTime;
char[]ch={'+','-','*','÷'};
//存放加减乘除的符号数组
float sum=0;
//程序判断的答案
int timushu;
//用户输入的题目
int count=0;
//已经 出的题目
int right=0;
//计算答对的题目数
int wrong=0;
//计算答错的题目数
MyWindow()
{
super("四则运算"
);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,250
);
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
//截取屏幕大小
this.setLocation(screensize.width/2-400/2, screensize.height/2-400/2);
//居中计算
L1=
new JLabel("选择题数(不能超过5个)"
);
L2=
new JLabel("时间:"
);
L3=
new JLabel("设置时间(时间不超过120秒):"
);
L4=
new JLabel("随机题目:"
);
L5=
new JLabel("输入答案:"
);
L6=
new JLabel("判断对错:"
);
text=
new JTextField(15
);
B1=
new JButton("开始"
);
t=
new JTextField(23
);
at=
new JTextField(23
);
at.addActionListener(this);
st=
new JTextField(23
);
tt=
new JTextField(15
);
tt.addActionListener(this);
lt=
new JTextField(5
);
lt.addActionListener(this);
Timer tmr =
new Timer(1000,
this);
//间隔1秒
this.tmr = tmr;
//添加时间
menuBar=
new JMenuBar();
setJMenuBar(menuBar);
menu=
new JMenu("外观选择"
);
UIManager.LookAndFeelInfo[] infos=UIManager.getInstalledLookAndFeels();
//获取本地观感,置入数组
for(UIManager.LookAndFeelInfo info:infos){
makeitem(info.getName(),info.getClassName());}
add(menu);
B2=
new JButton("下一题");B2.addActionListener(
this);
text.addActionListener(this);B1.addActionListener(
this);
p1=
new JPanel();p2=
new JPanel();p3=
new JPanel();
getContentPane().add(p1,"North");getContentPane().add(p2,"Center");getContentPane().add(p3,"South"
);
p1.add(L3);p1.add(lt);p2.add(L1);p2.add(text);p2.add(B1);
p2.add(L4);p2.add(t);p2.add(L5);p2.add(at);p2.add(L6);p2.add(st);
p3.add(L2);p3.add(tt);p3.add(B2);
menuBar.add(menu);
this.setResizable(
false);
//设置不可以改变大小
this.setVisible(
true);
//设置窗口可见
}
void makeitem(String name,
final String MyWindow)
{
JRadioButtonMenuItem item=
new JRadioButtonMenuItem(name);
menu.add(item);
item.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
//使用内部类的实例
{
try
{
UIManager.setLookAndFeel(MyWindow);//设置观感
SwingUtilities.updateComponentTreeUI(MyWindow.
this);
//更新当前观感
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==B1)
//当获取的资源是按钮1时
{
try
{
tt.setText(String.valueOf(lt.getText()));//在显示时间框里显示以获取的时间
String reg="^[0-9]+$"
;
String str1=
lt.getText();
String str2=
text.getText();
if(str1.matches(reg))
{
tt.setText(String.valueOf(lt.getText()));
}
else
{
this.setVisible(
false);
JOptionPane.showMessageDialog(null, "输入非法错误!"
);
}
if(str2.matches(reg))
{
text.setText(String.valueOf(text.getText()));
}
else
{
this.setVisible(
false);
JOptionPane.showMessageDialog(null, "输入非法错误!"
);
}
int n1=120,n2=5
;
if(n1<
(Integer.parseInt(tt.getText())))
{//当设置时间超过120秒,弹出对话框
JOptionPane.showMessageDialog(
null, "时间不能超过120秒!"
);
this.setVisible(
false);
}
if(n2<
(Integer.parseInt(text.getText())))
{//当选择题目个数超过5个,弹出对话框
JOptionPane.showMessageDialog(
null, "选择题数不能超过五个!"
);
this.setVisible(
false);
}
tmr.start();//开始计时
}
catch(NullPointerException o)
{
}
suijishu();//调用产生随机数题目
// startTime= System.currentTimeMillis(); //获取开始时间
tt.setText(String.valueOf(lt.getText()));
}
else{
int m =
Integer.parseInt(tt.getText());
m--
;
tt.setText("" +
m);
if (m <= 0
)
{
tmr.stop();
JOptionPane.showMessageDialog(null, "超出答题时间!"
);
this.setVisible(
false);
}
if(e.getSource()==at)
//当获取的资源是输入答案时
{
str=
decimal.format(sum);
if(Float.parseFloat(str)==Float.parseFloat(at.getText()))
//判断输入的答案对错
{
right++
;
st.setText("输入的答案正确!"
);
st.setForeground(Color.red);
}
else
{
wrong++
;
st.setText("输入的答案不正确!正确答案为"+
Float.parseFloat(str));
st.setForeground(Color.red);//当判断答案对错时,字体显示红色
}
}
else if(e.getSource()==B2)
//当获取的资源是按钮2
{
count++;
//当未超过题目数时,继续出随机题
if(count!=
Integer.parseInt(text.getText()))
{
t.setText(null);
at.setText(null);
st.setText(null);
suijishu();
}
else
{//当超过输入的题目数时,弹出结束的消息框
tmr.stop();
//double endTime= System.currentTimeMillis() ;//获取结束时间
seconds=Double.parseDouble(lt.getText())-Double.parseDouble(tt.getText());
//计算时间
//tt.setText(String.valueOf(seconds)+"s");//输出时间
JOptionPane.showMessageDialog(
null, "答题结束!一共答对"+right+"道题目,答错"+wrong+"道题目!"+"答题时间为:"+seconds+"秒"
);
this.setVisible(
false);
}
}
}
}
private void suijishu()
{
int x=(
int)(Math.random()*4
);
switch(x)
{
case 0
:
fun0();//一个运算符,整数,整数
break;
case 1
:
fun1();//一个运算符,整数,分数
break;
case 2
:
fun2();//一个运算符,分数,分数
break;
case 3
:
fun3();//两个运算符
break;
case 4
:
fun4();//求阶乘
break;
case 5
:
fun5();//题目出现负数
break;
//case 4:
//、 fun1();
//、case 5:
//、 fun1();
//、case 6:
// fun1();
}
}
private void fun0()
{
int a=(
int)(Math.random()*10+1
);
int b=(
int)(Math.random()*10+1
);
int x1=(
int)(Math.random()*4);
//表示加减乘除
switch(x1)
{
case 0
:
{
t.setText(a+" "+String.valueOf(ch[0])+" "+
b);
sum=a+
b;
}
break;
case 1
:
{
t.setText(a+" "+String.valueOf(ch[1])+" "+
b);
sum=a-
b;
}
break;
case 2
:
{
t.setText(a+" "+String.valueOf(ch[2])+" "+
b);
sum=a*
b;
}
break;
case 3
:
{
t.setText(a+" "+String.valueOf(ch[3])+" "+
b);
sum=(
float)a/(
float)b;
}
break;
}
}
private void fun1()
{
int a=(
int)(Math.random()*10+1
);
int b=(
int)(Math.random()*10+1
);
int c=(
int)(Math.random()*10+1
);
int x1=(
int)(Math.random()*4);
//表示加减乘除
switch(x1)
{
case 0
:
{
t.setText(a+" "+String.valueOf(ch[0])+" "+b+"/"+
c);
sum=a+((
float)b/(
float)c);
}break;
case 1
:
{
t.setText(a+" "+String.valueOf(ch[1])+" "+b+"/"+
c);
sum=a-((
float)b/(
float)c);
}break;
case 2
:
{
t.setText(a+" "+String.valueOf(ch[2])+" "+b+"/"+
c);
sum=a*((
float)b/(
float)c);
}break;
case 3
:
{
t.setText(a+" "+String.valueOf(ch[3])+" "+b+"/"+
c);
sum=(
float)a/(
float)b/(
float)c;
}break;
}
}
private void fun2()
{
int a=(
int)(Math.random()*10+1
);
int b=(
int)(Math.random()*10+1
);
int c=(
int)(Math.random()*10+1
);
int d=(
int)(Math.random()*10+1
);
int x1=(
int)(Math.random()*4);
//表示加减乘除
switch(x1)
{
case 0
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[0])+" "+b+"/"+
c);
sum=((
float)a/(
float)d)+((
float)b/(
float)c);
}break;
case 1
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[1])+" "+b+"/"+
c);
sum=((
float)a/(
float)d)-((
float)b/(
float)c);
}break;
case 2
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[2])+" "+b+"/"+
c);
sum=((
float)a/(
float)d)*((
float)b/(
float)c);
}break;
case 3
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[3])+" "+b+"/"+
c);
sum=((
float)a/(
float)d)/((
float)b/(
float)c);
}break;
}
}
private void fun3()
{
int a=(
int)(Math.random()*10+1
);
int b=(
int)(Math.random()*10+1
);
int c=(
int)(Math.random()*10+1
);
int x1=(
int)(Math.random()*3);
//表示加减乘除
switch(x1)
{
case 0
:
fun31();//两个运算符,三个整数
break;
case 1
:
fun32();//两个运算符,两个整数,一个分数
break;
case 2
:
fun33();//两个运算符,一个整数,两个分数
break;
// case 3:
// fun34();
// break;
}
}
private void fun31()
{
int a=(
int)(Math.random()*10+1
);
int b=(
int)(Math.random()*10+1
);
int c=(
int)(Math.random()*10+1
);
int x1=(
int)(Math.random()*11);
//表示加减乘除
switch(x1)
{
case 0
:
{
t.setText(a+" "+String.valueOf(ch[0])+" "+b+" "+String.valueOf(ch[0])+" "+
c);
sum=a+b+
c;
}break;
case 1
:
{
t.setText(a+" "+String.valueOf(ch[0])+" "+b+" "+String.valueOf(ch[1])+" "+
c);
sum=a+b-
c;
}break;
case 2
:
{
t.setText(a+" "+String.valueOf(ch[1])+" "+b+" "+String.valueOf(ch[1])+" "+
c);
sum=a-b-
c;
}break;
case 3
:
{
t.setText(a+" "+String.valueOf(ch[2])+" "+b+" "+String.valueOf(ch[0])+" "+
c);
sum=a*b+
c;
}break;
case 4
:
{
t.setText(a+" "+String.valueOf(ch[2])+" "+b+" "+String.valueOf(ch[1])+" "+
c);
sum=a*b-
c;
}break;
case 5
:
{
t.setText(a+" "+String.valueOf(ch[2])+" "+b+" "+String.valueOf(ch[2])+" "+
c);
sum=a*b*
c;
}break;
case 6
:
{
t.setText(a+" "+String.valueOf(ch[0])+" "+b+" "+String.valueOf(ch[1])+" "+
c);
sum=a+b-
c;
}break;
case 7
:
{
t.setText(a+" "+String.valueOf(ch[3])+" "+b+" "+String.valueOf(ch[0])+" "+
c);
sum=((
float)a/(
float)b)+
c;
}break;
case 8
:
{
t.setText(a+" "+String.valueOf(ch[3])+" "+b+" "+String.valueOf(ch[1])+" "+
c);
sum=((
float)a/(
float)b)-
c;
}break;
case 9
:
{
t.setText(a+" "+String.valueOf(ch[3])+" "+b+" "+String.valueOf(ch[3])+" "+
c);
sum=((
float)a/(
float)b)/(
float)c;
}break;
}
}
private void fun32()
{
int a=(
int)(Math.random()*10+1
);
int b=(
int)(Math.random()*10+1
);
int c=(
int)(Math.random()*10+1
);
int d=(
int)(Math.random()*10+1
);
int x1=(
int)(Math.random()*4);
//表示加减乘除
switch(x1)
{
case 0
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[0])+b+" "+String.valueOf(ch[0])+" "+
c);
sum=((
float)a/(
float)d)+b+
c;
}break;
case 1
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[0])+b+" "+String.valueOf(ch[1])+" "+
c);
sum=((
float)a/(
float)d)+b-
c;
}break;
case 2
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[1])+b+" "+String.valueOf(ch[1])+" "+
c);
sum=((
float)a/(
float)d)-b-
c;
}break;
case 3
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[2])+b+" "+String.valueOf(ch[0])+" "+
c);
sum=((
float)a/(
float)d)*b+
c;
}break;
case 4
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[2])+b+" "+String.valueOf(ch[1])+" "+
c);
sum=((
float)a/(
float)d)*b-
c;
}break;
case 5
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[2])+b+" "+String.valueOf(ch[2])+" "+
c);
sum=((
float)a/(
float)d)*b*
c;
}break;
case 6
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[2])+b+" "+String.valueOf(ch[3])+" "+
c);
sum=((
float)a/(
float)d)*b/(
float)c;
}
case 7
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[3])+b+" "+String.valueOf(ch[0])+" "+
c);
sum=((
float)a/(
float)d)/(
float)b+
c;
}break;
case 8
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[3])+b+" "+String.valueOf(ch[1])+" "+
c);
sum=((
float)a/(
float)d)/(
float)b-
c;
}break;
case 9
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[3])+b+" "+String.valueOf(ch[3])+" "+
c);
sum=((
float)a/(
float)d)/(
float)b/(
float)c;
}break;
}
}
private void fun33()
//一个整数,两个分数
{
int a=(
int)(Math.random()*10+1
);
int b=(
int)(Math.random()*10+1
);
int c=(
int)(Math.random()*10+1
);
int d=(
int)(Math.random()*10+1
);
int e=(
int)(Math.random()*10+1
);
int x1=(
int)(Math.random()*4);
//表示加减乘除
switch(x1)
{
case 0
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[0])+" "+b+"/"+c+" "+String.valueOf(ch[0])+" "+
e);
sum=((
float)a/(
float)d)+((
float)b/(
float)c)+
e;
}break;
case 1
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[0])+" "+b+"/"+c+" "+String.valueOf(ch[1])+" "+
e);
sum=((
float)a/(
float)d)+((
float)b/(
float)c)-
e;
}break;
case 2
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[1])+" "+b+"/"+c+" "+String.valueOf(ch[1])+" "+
e);
sum=((
float)a/(
float)d)-((
float)b/(
float)c)-
e;
}break;
case 3
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[2])+" "+b+"/"+c+" "+String.valueOf(ch[0])+" "+
e);
sum=((
float)a/(
float)d)*((
float)b/(
float)c)+
e;
}break;
case 4
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[2])+" "+b+"/"+c+" "+String.valueOf(ch[1])+" "+
e);
sum=((
float)a/(
float)d)*((
float)b/(
float)c)-
e;
}break;
case 5
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[2])+" "+b+"/"+c+" "+String.valueOf(ch[2])+" "+
e);
sum=((
float)a/(
float)d)*((
float)b/(
float)c)*
e;
}break;
case 6
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[2])+" "+b+"/"+c+" "+String.valueOf(ch[3])+" "+
e);
sum=((
float)a/(
float)d)*((
float)b/(
float)c)/(
float)e;
}
case 7
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[3])+" "+b+"/"+c+" "+String.valueOf(ch[0])+" "+
e);
sum=((
float)a/(
float)d)/((
float)b/(
float)c)+
e;
}break;
case 8
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[3])+" "+b+"/"+c+" "+String.valueOf(ch[1])+" "+
e);
sum=((
float)a/(
float)d)/((
float)b/(
float)c)-
e;
}break;
case 9
:
{
t.setText(a+"/"+d+" "+String.valueOf(ch[3])+" "+b+"/"+c+" "+String.valueOf(ch[3])+" "+
e);
sum=((
float)a/(
float)d)/((
float)b/(
float)c)/(
float)e;
}break;
}
}
private void fun4()
{
int a=(
int)(Math.random()*10+1);
//求1~10的阶乘
t.setText(a+"!"
);
for(
int i=1;i<=a;i++
)
{
sum=1
;
sum=sum*
i;
}
}
private void fun5()
{
int a=(
int)(Math.random()*99+1)*-1;
//随机生成-1~-99的负整数
int b=(
int)(Math.random()*10+1
);
int x1=(
int)(Math.random()*4
);
switch(x1)
{
case 0
:
{
t.setText("("+a+") "+String.valueOf(ch[0])+" "+
b);
sum=a+
b;
}
break;
case 1
:
{
t.setText("("+a+") "+String.valueOf(ch[1])+" "+
b);
sum=a-
b;
}
break;
case 2
:
{
t.setText("("+a+") "+String.valueOf(ch[2])+" "+
b);
sum=a*
b;
}
break;
case 3
:
{
t.setText("("+a+") "+String.valueOf(ch[3])+" "+
b);
sum=(
float)a/(
float)b;
}
break;
}
}
}
一开始显示的登录界面
当用户名或密码不正确时:
当输入用户和密码都正确时,切换到四则运算界面:
当输入时间不符合要求时,弹出下框:
当输入选择题数不符合要求时,弹出下框:
当都输入正确时,则自动出题:
当输入答案后,回车,系统判断正确与否:
当答题结束时:
当答题时间超过设置时:
这次在原来的基础上,将所有功能都完善了。在实现更换皮肤时,出了点错,发现不了出错在哪里。在设置用户登录界面时,为了美观,添加了一个图片当背景,运行时发现图片总是没出现,后来仔细检查,发现参数带入有误。由于显示的界面老是在屏幕左上角出现,于是设置窗口都在屏幕上正中间显现。还存在的缺点: 1:更换皮肤界面时,前两个皮肤更换没问题,可是后三个更换皮肤时,窗口里的布局盖面
转载于:https://www.cnblogs.com/ZeJuan/p/4422980.html
相关资源:JAVA上百实例源码以及开源项目