package controler;
import newtest.*;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
public class Controler extends JFrame{
String op;double result;int a,b,c;
AutoJudge auto=new AutoJudge();
public void btnNew(ActionEvent e)//方法
{
a=(int)(Math.random()*9+1);
b=(int)(Math.random()*9+1);
c=(int)(Math.random()*3+1);
op="";result=0;
switch(c)
{
case 0:
op="+";result=a+b;
case 1:
op="-";result=a-b;
case 2:
op="*";result=a*b;
case 3:
op="/";result=a/b;
}
auto.jb1.setText(""+a);
auto.jb2.setText(op);
auto.jb3.setText(""+b);
auto.textField.setText("");
}
public void btnJudge(ActionEvent e)
{
String str=auto.textField.getText();
double d=Double.valueOf(str).doubleValue();
String ans=""+a+op+b+"="+d+"";
if(result==d)
ans+="right!";
else
ans+="false!";
auto.list.add(ans);
}
}
以上是controler的代码段,用来实现逻辑。
然后是图形界面。
package newtest;
import controler.*;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingConstants;
public class AutoJudge extends JFrame {
Controler control=new Controler();
private JPanel contentPane;
public JTextField textField=new JTextField(); ;
public JButton btnNew1 = new JButton("\u51FA\u9898");
public JButton btnJudge1 = new JButton("\u5224\u5206");
public JLabel jb1 = new JLabel("");//new为一个对象在堆中开辟了空间,这个空间里面有对象的方法和字段。
public JLabel jb2 = new JLabel("");//在这里jb1与jb2等都不是对象本身,而是一个对象引用,或者说是对象指针。jb1与jb2存在于栈里面。而不是堆中。
public JLabel jb3 = new JLabel("");//这里的等号意为把堆中的内存赋值给对象引用。
public List list = new List(0);
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AutoJudge frame = new AutoJudge();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AutoJudge() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
btnNew1.setBounds(64, 69, 97, 23);
contentPane.add(btnNew1);
btnJudge1.setBounds(244, 69, 97, 23);
contentPane.add(btnJudge1);
JLabel jb4 = new JLabel("=");
jb4.setHorizontalAlignment(SwingConstants.CENTER);
jb4.setFont(new Font("宋体", Font.PLAIN, 37));
jb4.setBounds(226, 8, 30, 42);
contentPane.add(jb4);
textField.setFont(new Font("宋体", Font.PLAIN, 28));
textField.setBounds(266, 8, 97, 42);
contentPane.add(textField);
textField.setColumns(10);
list.setFont(new Font("Dialog", Font.PLAIN, 18));
list.setBounds(28, 108, 385, 153);
contentPane.add(list);
jb1.setFont(new Font("宋体", Font.PLAIN, 28));
jb1.setBounds(87, 8, 43, 42);
contentPane.add(jb1);
jb2.setFont(new Font("宋体", Font.PLAIN, 28));
jb2.setBounds(131, 8, 30, 42);
contentPane.add(jb2);
jb3.setFont(new Font("宋体", Font.PLAIN, 28));
jb3.setBounds(173, 8, 43, 42);
contentPane.add(jb3);
SymAction symAction=new SymAction();
btnNew1.addActionListener( symAction );
btnJudge1.addActionListener(symAction);
}
class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(ActionEvent e) {
Object object=e.getSource();//获取事件源。
if(object==btnNew1)
{
control.btnNew(e);
}
if(object==btnJudge1)
{
control.btnJudge(e);
}
}
}
}