这个程序是随机产生两个数字,求他们的乘法,要求输入答案到jtextfield,如果正确,使用paint方法显示成功,才能继续下一个题,如果输入错误,则显示失败,继续做这个题,只有成功才能继续下一题,我写的代码如下,后面的写不下去了,请大家帮帮忙
现在不知道如何对两个jbutton 如何监听,以及点击button后如何调用paint方法import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TextMath extends JApplet implements ActionListener{
 JLabel jlabel1,jlabel2,jlabel3;
 JTextField jtext1,jtext2,jtext3;
 JButton rollButton,rollButton1;
 int num,count;
  public void init(){
  Container container = getContentPane();
  container.setLayout(new FlowLayout());
  
  jlabel1 = new JLabel("First number");
  jtext1 = new JTextField(10);
  jtext1.setEditable(false);
  container.add(jlabel1);
  container.add(jtext1);
  
  jlabel2 = new JLabel("scond number");
  jtext2 = new JTextField(10);
  jtext2.setEditable(false);
  container.add(jlabel2);
  container.add(jtext2);
  
  jlabel3 = new JLabel("sum is");
  jtext3 = new JTextField(10);
  container.add(jlabel3);
  container.add(jtext3);
  
  rollButton = new JButton("next");
  rollButton.addActionListener(this);
  container.add(rollButton);
  rollButton1 = new JButton("确定");
  rollButton1.addActionListener(this);
  container.add(rollButton1);
  
  
 }
 public void actionPerformed(ActionEvent actionevent){
  
  if(actionevent.getActionCommand().equals("next")){
   num = rollDice();
   showStatus("How much is"+jtext1.getText()+"time"+jtext2.getText()+"?");
  }
  if(actionevent.getActionCommand().equals("确定")){
  } }
 public int rollDice(){
  int die1,die2,sum;
  
  die1 = 1 + (int)(Math.random()*10);
  die2 = 1 + (int)(Math.random()*10);
  sum = die1*die2;
  jtext1.setText(Integer.toString(die1));
  jtext2.setText(Integer.toString(die2));
  return sum;
 }
public void paint(Graphics g){
  num = rollDice();
  super.paint(g);
  count = Integer.parseInt(jtext3.getText().trim());
  if(count == num){
  g.drawString("Very good", 50, 50);
  }
 }

解决方案 »

  1.   

    不会用 paint,直接显示在 JLabel 上不行啊?
      

  2.   

    JApplet 没用过,不过可以回答两个东东
    1、 rollButton.addActionListener(this);
      这个其实就是增加监听器 ,只有加了这个,actionPerformed里面的相应事件才会被监听到。
    2、及点击button后如何调用paint方法,Canvas里面可以用repaint方法。。这个里面可以试试
      

  3.   

    帮你写了这个,其中添加按钮监听的方法有些特别,但是我觉得这样能使代码好看些。虽然在我的代码里你可以用Button来paint,但是1楼的方法更简单些,直接改变JLable的值然不是更好吗。供你参考吧,方法你自己选择决定。
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class TwoButtons extends JPanel{

    public static void main(String args[]) {
    JFrame frame = new JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    TwoButtons tb = new TwoButtons();
    frame.add(tb);
    frame.setPreferredSize(new Dimension(800, 600));
    frame.setBounds(0, 0, 800, 600);
    frame.validate();

    frame.setVisible(true);
    }

    public TwoButtons() {
    initComp();
    } public void initComp() {
    this.setLayout(new FlowLayout());
    this.setPreferredSize(new Dimension(800, 600));

    JButton b1 = new JButton("Button1");
    JButton b2 = new JButton("Button2");

    b1.addActionListener(new TwoButtons_button1_actionAdapter(this));
    b2.addActionListener(new TwoButtons_button2_actionAdapter(this));

    this.add(b1);
    this.add(b2);
    }

    public void paintWithoutPassingInGraphics1() {
    Graphics g = this.getGraphics();
    g.drawLine(0, 0, 300, 300);
    }

    public void paintWithoutPassingInGraphics2() {
    Graphics g = this.getGraphics();
    g.drawLine(300, 300, 500, 300);
    }

    /** Action Handlers **/
    public void b1_actionPerformed(ActionEvent e) {
    paintWithoutPassingInGraphics1();
    }

    public void b2_actionPerformed(ActionEvent e) {
    paintWithoutPassingInGraphics2();
    }
    }class TwoButtons_button1_actionAdapter implements ActionListener {

    private TwoButtons adaptee;

    TwoButtons_button1_actionAdapter(TwoButtons adaptee) {
    this.adaptee = adaptee;
    } public void actionPerformed(ActionEvent e) {
    adaptee.b1_actionPerformed(e);
    }

    }class TwoButtons_button2_actionAdapter implements ActionListener {

    private TwoButtons adaptee;

    TwoButtons_button2_actionAdapter(TwoButtons adaptee) {
    this.adaptee = adaptee;
    } public void actionPerformed(ActionEvent e) {
    adaptee.b2_actionPerformed(e);
    }

    }