初学者急求 java小问题解决办法!!

zbwork000 2008-11-01 05:39:15
import java.awt.*;
import java.awt.event.*;

public class TFMath{
public static void main(String args[]){
new TTFrame().launchFrame();
}
}

class TFFrame extends Frame{
TextField num1,num2,num3;
public void launchFrame(){
Frame f1 = new Frame("math");
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Label lab1 = new Label("+");
Button b1 = new Button("=");
b1.addActionListener(new MyMonitor());
setLayout(new FlowLayout());
add(num1);
add(lab1);
add(num2);
add(b1);
add(num3);
pack();
f1.setVisible(true);
}

}


class MyMonitor implements ActionListener{

TFFrame tf1 = null;

public MyMonitor(TFFrame tf1){
this.tf1 = tf1;
}

public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf1.num1.getText());
int n2 = Integer.parseInt(tf1.num2.getText());
System.out.println(""+(n1+n2));
}
}

这是一个用窗口实现加法的程序。有点问题,请大家帮忙看看,谢谢了。
...全文
76 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zbwork000 2008-11-01
  • 打赏
  • 举报
回复
谢谢2楼的你的回答。我是第一次在这里发贴的,
SylvanLiu 2008-11-01
  • 打赏
  • 举报
回复
new TTFrame().launchFrame()与class TFFrame extends Frame,一看就有类名上的错误;

b1.addActionListener(new MyMonitor());与public MyMonitor(TFFrame tf1),构造函数调用不当

其他的没仔细看

cydp007 2008-11-01
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;

public class TFMath {
public static void main(String args[]) {
new TFFrame().launchFrame();
}
}

class TFFrame extends Frame {
private static final long serialVersionUID = 1L;
TextField num1, num2, num3;

public void launchFrame() {
//Frame f1 = new Frame("math");//这句话没必要..
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Label lab1 = new Label("+");
Button b1 = new Button("=");
b1.addActionListener(new MyMonitor());
setLayout(new FlowLayout());
add(num1);
add(lab1);
add(num2);
add(b1);
add(num3);
pack();
setVisible(true);
}
class MyMonitor implements ActionListener {//这里写成内部类..

public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
System.out.println("" + (n1 + n2));
num3.setText("" + (n1 + n2));
}

}

}
xwguan 2008-11-01
  • 打赏
  • 举报
回复
有点乱~

62,628

社区成员

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

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