标签:
数学题练习器,答对一题,就在文本区域内打印出来,答错打出错误提示,一共有十道简单数学题
//MathProblem.java主要涉及界面字体
import java.awt.*; import java.awt.event.*; import javax.swing.JTextArea; import javax.swing.JTextField; public class MathProblem extends Frame implements ActionListener { private static final long serialVersionUID = 1L; TextArea ta; JTextField tf1,tf2; Label lb1,lb2; Button bt1,bt2,bt3; Panel p1,p2,p3,p4,p5,p6; Frame f; Font fo; Color color; String tips1; String tips2; int i; Question qu=new Question(); MathProblem(){ fo=new Font("宋体",Font.BOLD,20); color=Color.BLACK; tips1="回答正确!"; tips2="回答错误!"; i=0; ta=new TextArea(); ta.setFont(fo); ta.setForeground(color); ta.setSize(480, 180); ta.setEditable(true); tf1=new JTextField(20); tf2=new JTextField(10); tf1.setHorizontalAlignment(JTextField.CENTER); tf1.setText(qu.math[i]); tf1.setEditable(true); tf2.setEditable(true); lb1=new Label("题目:"); lb2=new Label("答案:"); bt1=new Button("确定"); bt2=new Button("上一题"); bt3=new Button("下一题"); bt1.addActionListener(this); bt2.addActionListener(this); bt3.addActionListener(this); p1=new Panel(); p2=new Panel(); p3=new Panel(); p1.add(ta); p2.add(lb1); p2.add(tf1); p2.add(lb2); p2.add(tf2); p3.setLayout(new GridLayout(1,3)); p3.add(bt1); p3.add(bt2); p3.add(bt3); f=new Frame("练习题"); f.setVisible(true); f.setResizable(false); f.setSize(700, 370); f.setLocationRelativeTo(null); f.add(p1,BorderLayout.NORTH); f.add(p2,BorderLayout.CENTER); f.add(p3,BorderLayout.SOUTH); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } public void actionPerformed(ActionEvent e) { if(e.getSource().equals(bt1)){ if(tf2.getText().equals(qu.mathKey[i])){ ta.setForeground(color.GREEN); ta.setText(qu.math[i]+qu.mathKey[i]+"\n"+tips1); } else{ ta.setText(tips2); ta.setForeground(color.RED); } } if(e.getSource().equals(bt2)){ i--; ta.setForeground(color); if(i>=0){ tf1.setText(qu.math[i]); tf2.setText(null); ta.setText(null); } else{ ta.setText("已经是第一题!"); i=0; } } if(e.getSource().equals(bt3)){ i++; ta.setForeground(color); if(i<=9){ tf1.setText(qu.math[i]); tf2.setText(null); ta.setText(null); } else{ ta.setText("已经是最后一题!"); i=9; } } } public static void main(String args[]){ MathProblem mp=new MathProblem(); } }
//question.java放置题目和答案
public class Question { String[] math; String mathKey[]={"2","4","8","2","7","45","4","124","132","1"}; Question(){ math=new String[10]; math[0]="1+1="; math[1]="10-6="; math[2]="2*4="; math[3]="6÷3="; math[4]="1+2*3="; math[5]="(100-10)*2="; math[6]="(7+1)÷2="; math[7]="1+123="; math[8]="64÷2+100="; math[9]="8+1-8="; } }
标签:
原文地址:http://www.cnblogs.com/waikitcn/p/4500525.html