import java.awt.*;
import java.awt.event.*;
public class TFMath {
public static void main(String[] args)
{
new TFrame().launchFrame();
}
}
class TFrame extends Frame{
TextField num1,num2,num3;
public void launchFrame(){
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(15);
Label plus = new Label("+");
Button equel = new Button("=");
equel.addActionListener(new MyMonit(this));
setLayout(new FlowLayout());
add(num1);
add(plus);
add(num2);
add(equel);
add(num3);
pack();
setVisible(true);
}
}
class MyMonit implements ActionListener { TFrame tf = null;
public MyMonit(TFrame tf){
this.tf = tf;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf.num1.getText());
int n2 = Integer.parseInt(tf.num2.getText());
tf.num3.setText(""+(n1+n2));
}}编译不错,但不知道为什么运行不起来?帮着看一下
import java.awt.event.*;
public class TFMath {
public static void main(String[] args)
{
new TFrame().launchFrame();
}
}
class TFrame extends Frame{
TextField num1,num2,num3;
public void launchFrame(){
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(15);
Label plus = new Label("+");
Button equel = new Button("=");
equel.addActionListener(new MyMonit(this));
setLayout(new FlowLayout());
add(num1);
add(plus);
add(num2);
add(equel);
add(num3);
pack();
setVisible(true);
}
}
class MyMonit implements ActionListener { TFrame tf = null;
public MyMonit(TFrame tf){
this.tf = tf;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf.num1.getText());
int n2 = Integer.parseInt(tf.num2.getText());
tf.num3.setText(""+(n1+n2));
}}编译不错,但不知道为什么运行不起来?帮着看一下
解决方案 »
- 线程共享问题,求前辈指教
- 通俗解释下什么叫低耦合高内聚
- 用纯java驱动方式访问SqlServer2005数据库,遇到一个com.microsoft.sqlserver.jdbc.SQLServerException
- Java邮件附件发送的问题
- 图形高手进!
- jtree的cellrender问题
- 同学遇到的一个免试题目
- eclipse设置src.zip问题
- 关于线程调度
- 小弟问个问题:我看了THINKING IN JAVA一多半啦(一个月),可还是做不出什么东西啊!大哥大姐们帮我想个东东,我试着做做啊!最好能说详细点啊!分不够再加啊!
- socket中client的问题
- 急!急!急!用java在网页中添加图形的问题
num2 = new TextField(10);
num3 = new TextField(15);就好了
这里已经定义了
你还在下面这样写
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(15);
覆盖了类成员变量
这个变量的定义就是乱啊