回复 #6 fantasyray 的帖子
添加JButton点击事件的方法是:
void javax.swing.AbstractButton.addActionListener(ActionListener l)
Adds an ActionListener to the button.
参数:
l the ActionListener to be added
这样使用:
JButton button = new JButton();
button.addActionListener(new ButtonListener());
你可以新建一个class ButtonListener implements ActionListener
在这个类中实现ActionListener 接口的:
void ButtonListener.actionPerformed(ActionEvent e)
Invoked when an action occurs.
方法
把你的操作放在这个方法中。注意,通常这样你只能做一种操作,比如做了压缩,就不能做解压缩。如果要点一个按钮做不同操作,那么就需要对点击来源进行判断,这就是参数ActionEvent e的作用了,通常可以这样 :
String buttonString = e.getActionCommand();
if (buttonString.equals("压缩")) {// 压缩的程序}
else if (...) {...}
这些String就是在不同按钮上你写的按钮名字。
这样明白了吗?