AdderFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});put the code into Constructor Add()this means this object
when you click button BAdd, actionPerformed() is called
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});put the code into Constructor Add()this means this object
when you click button BAdd, actionPerformed() is called
解决方案 »
- jtree怎么让节点是一个panel控件,并且,panel控件被激活
- java swing设置office2003皮肤将导致JToolBar左右灰白两色,十分难看(xp系统,经典主题)
- jasper的报错有人知道为什么吗?
- 如何监听某一个文件夹下有没有文件写入而后删除了该文件?
- Thread的join()问题,是高手的请进
- 有谁能解释一下code points和code units?
- 读写Excel,POI-HSSF会破坏公式,换JXL不会,为何?(有源码)
- 这道练习题怎样做?!
- 求助:JAVA语言求助!!!急!!急!!谢谢!
- 我想利用jsp+javabean将数据库(mssql2000)中存储的image型数据,以图片型式输出到网页上,大家能否提供一些例子
- 构造函数问题????
- trim的问题.
import java.awt.event.*;
import javax.swing.*;public class Adder implements ActionListener
{
JFrame AdderFrame;
JTextField TOprand1;
JTextField TOprand2;
JLabel LAdd,LSum;
JButton BAdd;
int i=0;
public Adder()
{
AdderFrame=new JFrame("AdderFrame");
TOprand1=new JTextField("0.0");
TOprand2=new JTextField("0.0");
LAdd=new JLabel("+");
LSum=new JLabel("= ");
BAdd=new JButton("ADD!");
AdderFrame.getContentPane().setLayout(new FlowLayout());
AdderFrame.getContentPane().add(TOprand1);
AdderFrame.getContentPane().add(TOprand2);
AdderFrame.getContentPane().add(LAdd);
AdderFrame.getContentPane().add(TOprand2);
AdderFrame.getContentPane().add(LSum);
AdderFrame.getContentPane().add(BAdd);
AdderFrame.pack();
AdderFrame.setVisible(true);
AdderFrame.addWindowListener(new WindowAdapter() //此行提前
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//为命令按钮添加事件监听器,监听器的实参是Adder类的对象
BAdd.addActionListener(this); //this 指的是什么
}
//Adder类的事件服务方法
public void actionPerformed(ActionEvent event) //这个方法什么时候被执行
{
i=(i+1)%2;
if(i==1)
{
//i为奇数,求和
int sum=(int)(Double.valueOf(TOprand1.getText()).doubleValue()+Double.valueOf(TOprand2.getText()).doubleValue());
LSum.setText("="+sum);
}
else
{
TOprand1.setText("0.0");
TOprand2.setText("0.0");
LSum.setText("= ");
}
}
public static void main(String[] args)
{
Adder adder=new Adder();
}
}
当执行BAdd.addActionListener(this)语句时,this指向的就是 actionPerformed方法
要是我有两个或更多的按扭呢,是不是都called actionPerformed(),要是我想要几个按扭执行不同的方法,是不是就得另换一种方法了
to : ddbean(丁大伯)我已提前,但以出现错误:
Adder.java:35: illegal start of expression
public void actionPerformed(ActionEvent event)
^
1 error
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
URL url=webBrowserPane.back();
urlTextField.setText(url.toString());
}
}
);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;public class Adder implements ActionListener
{
JFrame AdderFrame;
JTextField TOprand1;
JTextField TOprand2;
JLabel LAdd,LSum;
JButton BAdd;
int i=0;
Graphics g=null;
public Adder()
{
AdderFrame=new JFrame("AdderFrame");
TOprand1=new JTextField("0.0");
TOprand2=new JTextField("0.0");
LAdd=new JLabel("+");
LSum=new JLabel("= ");
BAdd=new JButton("ADD!");
AdderFrame.getContentPane().setLayout(new FlowLayout());
AdderFrame.getContentPane().add(TOprand1);
AdderFrame.getContentPane().add(TOprand2);
AdderFrame.getContentPane().add(LAdd);
AdderFrame.getContentPane().add(TOprand2);
AdderFrame.getContentPane().add(LSum);
AdderFrame.getContentPane().add(BAdd);
AdderFrame.pack();
AdderFrame.setVisible(true);
AdderFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//为命令按钮添加事件监听器,监听器的实参是Adder类的对象
BAdd.addActionListener(this); //this 指的是什么
}
//Adder类的事件服务方法
public void actionPerformed(ActionEvent event) //这个方法什么时候被执行
{
i=(i+1)%2;
if(i==1)
{
//i为奇数,求和
int sum=(int)(Double.valueOf(TOprand1.getText()).doubleValue()+Double.valueOf(TOprand2.getText()).doubleValue());
LSum.setText("="+sum);
}
else
{
TOprand1.setText("0.0");
TOprand2.setText("0.0");
LSum.setText("= ");
}
}public static void main(String[] args)
{
Adder adder=new Adder();
}
}
BAdd.addActionListener(this); //this 指的是什么
}this 实际上表示一个ActionEvent, Adder类implement ActinoLisenter接口所以在Adder中要实现actionPerformed()方法,当button出触发ActionEvent时调用,actionPerformed()方法
若是有两个以上的事件源,可以用ActionEven的getSource()方法来获得事件发生的"地点“
public void actionPerformed(ActionEvent event)
^
1 error
那么为什么会出现上面的错误