程序写不下去了,求高人帮忙

caoliang3621 2009-09-15 09:41:15
这个程序是随机产生两个数字,求他们的乘法,要求输入答案到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);
}
}
}
...全文
98 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
daisycool 2009-09-16
  • 打赏
  • 举报
回复
帮你写了这个,其中添加按钮监听的方法有些特别,但是我觉得这样能使代码好看些。虽然在我的代码里你可以用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);
}

}
都市巴巴 2009-09-16
  • 打赏
  • 举报
回复
JApplet 没用过,不过可以回答两个东东
1、 rollButton.addActionListener(this);
这个其实就是增加监听器 ,只有加了这个,actionPerformed里面的相应事件才会被监听到。
2、及点击button后如何调用paint方法,Canvas里面可以用repaint方法。。这个里面可以试试
捏造的信仰 2009-09-16
  • 打赏
  • 举报
回复
不会用 paint,直接显示在 JLabel 上不行啊?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧