java 中actionPerformed的用法
下边的程序代码,哪不对啊~为什么显示if(e.getSource()==btnEqual)这句的btnEqual错误importjava.awt.*;importjava...
下边的程序代码,哪不对啊~
为什么显示if(e.getSource()==btnEqual) 这句的btnEqual错误
import java.awt.*;
import java.awt.event.*;
public class TFMath
{
public static void main(String args[])
{
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame
{
TextField num1,num2,num3;
public void launchFrame()
{
num1=new TextField(10);
num2=new TextField(10);
num3=new TextField(15);
Label lbPlus=new Label("+");
Button btnEqual=new Button("=");
//btnEqual.addActionListener(new MyMonitor(this));
//btnEqual.addActionListener(new MyMonitor
(num1,num2,num3));
btnEqual.addActionListener(new MyMonitor());
setLayout(new FlowLayout());
add(num1);
add(lbPlus);
add(num2);
add(btnEqual);
add(num3);
pack();
setVisible(true);
}
class MyMonitor implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnEqual)
{int n1=Integer.parseInt(num1.getText());
int n2=Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
}}
}
} 展开
为什么显示if(e.getSource()==btnEqual) 这句的btnEqual错误
import java.awt.*;
import java.awt.event.*;
public class TFMath
{
public static void main(String args[])
{
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame
{
TextField num1,num2,num3;
public void launchFrame()
{
num1=new TextField(10);
num2=new TextField(10);
num3=new TextField(15);
Label lbPlus=new Label("+");
Button btnEqual=new Button("=");
//btnEqual.addActionListener(new MyMonitor(this));
//btnEqual.addActionListener(new MyMonitor
(num1,num2,num3));
btnEqual.addActionListener(new MyMonitor());
setLayout(new FlowLayout());
add(num1);
add(lbPlus);
add(num2);
add(btnEqual);
add(num3);
pack();
setVisible(true);
}
class MyMonitor implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnEqual)
{int n1=Integer.parseInt(num1.getText());
int n2=Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
}}
}
} 展开
3个回答
展开全部
你的那个 btnEqual 不能够被解析,也就是说,你定义的位置,有问题。看不到这个符号,Button btnEqual=new Button("=");
往前提一下,就OK
import java.awt.*;
import java.awt.event.*;
public class Test
{
public static void main(String args[])
{
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame
{
/**
*
*/
Label lbPlus=new Label("+");
Button btnEqual=new Button("=");
private static final long serialVersionUID = 1L;
TextField num1,num2,num3;
public void launchFrame()
{
num1=new TextField(10);
num2=new TextField(10);
num3=new TextField(15);
//btnEqual.addActionListener(new MyMonitor(this));
//btnEqual.addActionListener(new MyMonitor (num1,num2,num3));
btnEqual.addActionListener(new MyMonitor());
setLayout(new FlowLayout());
add(num1);
add(lbPlus);
add(num2);
add(btnEqual);
add(num3);
pack();
setVisible(true);
}
class MyMonitor implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnEqual)
{int n1=Integer.parseInt(num1.getText());
int n2=Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
}}
}
}
往前提一下,就OK
import java.awt.*;
import java.awt.event.*;
public class Test
{
public static void main(String args[])
{
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame
{
/**
*
*/
Label lbPlus=new Label("+");
Button btnEqual=new Button("=");
private static final long serialVersionUID = 1L;
TextField num1,num2,num3;
public void launchFrame()
{
num1=new TextField(10);
num2=new TextField(10);
num3=new TextField(15);
//btnEqual.addActionListener(new MyMonitor(this));
//btnEqual.addActionListener(new MyMonitor (num1,num2,num3));
btnEqual.addActionListener(new MyMonitor());
setLayout(new FlowLayout());
add(num1);
add(lbPlus);
add(num2);
add(btnEqual);
add(num3);
pack();
setVisible(true);
}
class MyMonitor implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnEqual)
{int n1=Integer.parseInt(num1.getText());
int n2=Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
}}
}
}
展开全部
楼上正解,还有一种方法就是将if(e.getSource()==btnEqual) 这一句改成
if (e.getActionCommand() == "=")
源代码如下:
package com.lql.test;
import java.awt.*;
import java.awt.event.*;
public class TFMath
{
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
new TFFrame().launchFrame();
}
});
}
}
class TFFrame extends Frame
{
TextField num1, num2, num3;
// Button btnEqual=new Button("=");
public void launchFrame()
{
Button btnEqual = new Button("=");
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(15);
Label lbPlus = new Label("+");
// btnEqual.addActionListener(new MyMonitor(this));
// btnEqual.addActionListener(new MyMonitor
// (num1,num2,num3));
btnEqual.addActionListener(new MyMonitor());
setLayout(new FlowLayout());
add(num1);
add(lbPlus);
add(num2);
add(btnEqual);
add(num3);
pack();
setVisible(true);
}
class MyMonitor implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == "=")
// if(e.getSource()==btnEqual)
{
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText("" + (n1 + n2));
}
}
}
}
if (e.getActionCommand() == "=")
源代码如下:
package com.lql.test;
import java.awt.*;
import java.awt.event.*;
public class TFMath
{
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
new TFFrame().launchFrame();
}
});
}
}
class TFFrame extends Frame
{
TextField num1, num2, num3;
// Button btnEqual=new Button("=");
public void launchFrame()
{
Button btnEqual = new Button("=");
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(15);
Label lbPlus = new Label("+");
// btnEqual.addActionListener(new MyMonitor(this));
// btnEqual.addActionListener(new MyMonitor
// (num1,num2,num3));
btnEqual.addActionListener(new MyMonitor());
setLayout(new FlowLayout());
add(num1);
add(lbPlus);
add(num2);
add(btnEqual);
add(num3);
pack();
setVisible(true);
}
class MyMonitor implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == "=")
// if(e.getSource()==btnEqual)
{
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText("" + (n1 + n2));
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你那定义的是局部变量,在另外的类里不能被解析的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询