问题遇到的现象和发生背景
题目要求:1)修改代码,再增加“小学生”级别,并增加测试乘、除法的功能。(已将代码粘贴在下方)
3)编写应用程序,有一个标题为“移动”的窗口,窗口布局为null,在窗口中有两个按钮,单击一个按钮让另一个按钮移动。
问题相关代码,请勿粘贴截图
package Experiment8;
import java.util.Random;
import java.awt.event.*;
import javax.swing.*;
public class Teacher implements ActionListener{
int numberOne,numberTwo; //
String operator=" ";
boolean isRight;
Random random;
int maxInteger;
JTextField textOne,textTwo,textResult;//
JLabel operatorLabel,message;
Teacher(){
random=new Random();
}
public void setMaxInteger(int n){
maxInteger=n;
}
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
if(str.equals("getProblem"))
{
numberOne=random.nextInt(maxInteger)+1;
numberTwo=random.nextInt(maxInteger)+1;
double d=Math.random();
if(d>=0.5)
{
operator="+";
}
else
{
operator="-";
}
textOne.setText(""+numberOne);
textTwo.setText(""+numberTwo);
operatorLabel.setText(operator);
message.setText("请回答");
textResult.setText(null);
}
else if(str.equals("answer"))
{
String answer=textResult.getText();
try
{
int result=Integer.parseInt(answer);
if(operator.equals("+"))
{
if(result==numberOne+numberTwo)
{
message.setText("你回答正确");
}
else
{
message.setText("你回答错误");
}
}
else if(operator.equals("-"))
{
if(result==numberOne-numberTwo)
{
message.setText("你回答正确");
}
else
{
message.setText("你回答错误");
}
}
}
catch(NumberFormatException ex)
{
message.setText("请输入数字字符");
}
}
}
public void setJTextField(JTextField...t)
{
textOne =t[0];
textTwo =t[1];
textResult =t[2];
}
public void setJLabel(JLabel...label)
{
operatorLabel=label[0];
message=label[1];
}
}
package Experiment8;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComputerFrame extends JFrame{
JMenuBar menubar;
JMenu choiceGrade;
JMenuItem grade1,grade2;//
JTextField textOne,textTwo,textResult;//
JButton getProblem,giveAnswer;
JLabel operatorLabel,message;
Teacher teacherZhang;
ComputerFrame(){
teacherZhang=new Teacher();
teacherZhang.setMaxInteger(20);
setLayout(new FlowLayout());
menubar=new JMenuBar();
choiceGrade=new JMenu("选择级别");
grade1=new JMenuItem("幼儿级别");
grade2=new JMenuItem("儿童级别");
grade1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
teacherZhang.setMaxInteger(10);
}
});
grade2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
teacherZhang.setMaxInteger(50);
}
});
choiceGrade.add(grade1);
choiceGrade.add(grade2);
menubar.add(choiceGrade);
setJMenuBar(menubar);
textOne=new JTextField(5);//【代码1】 //创建textOne,其可见字符长是5
textTwo=new JTextField(5);
textResult=new JTextField(5);
operatorLabel=new JLabel("+");
operatorLabel.setFont(new Font("Arial",Font.BOLD,20));
message=new JLabel("你还没有回答呢");
getProblem=new JButton("获取题目");
giveAnswer=new JButton("确认答案");
add(getProblem);
add(textOne);
add(operatorLabel);
add(textTwo);
add(new Label("="));
add(textResult);
add(giveAnswer);
add(message);
textResult.requestFocus();
textOne.setEditable(false);
textTwo.setEditable(false);
getProblem.setActionCommand("getProblem");
textResult.setActionCommand("answer");
giveAnswer.setActionCommand("answer");
teacherZhang.setJTextField(textOne,textTwo,textResult);
teacherZhang.setJLabel(operatorLabel,message);
getProblem.addActionListener(teacherZhang);//【代码2】//将teacherZhang注册为getProblem的ActionEvent事件监视器
giveAnswer.addActionListener(teacherZhang);//【代码3】//将teacherZhang注册为giveAnswer的ActionEvent事件监视器
textResult.addActionListener(teacherZhang);//【代码4】//将teacherZhang注册为textResult的ActionEvent事件监视器
setVisible(true);
validate();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
package Experiment8;
public class MainClass {
public static void main(String[] args) {
ComputerFrame frame;
frame=new ComputerFrame();
frame.setTitle("算术测试");
frame.setBounds(100,100,650,180);
}
}
运行结果及报错内容
无
我的解答思路和尝试过的方法
无
我想要达到的结果
完成(1)、(3)两题