中的牛
2021-08-24 18:00:40 菏泽汽车网
关于ActionListener的响应问题,就我的理解可以有两种方法。第一种就是你放到一个新的类里面,实现ActionListener接口,然后写好public void actionPerformed(ActionEvent e)的方法。这种当继承自JFrame还是蛮有用的,但是如果是一个在public static void main(String[] args)中建立一个JFrame,然后对里面的(比如按钮)实现监听,那么去实现ActionListener接口就不那么合适了(哎,很多都是当你做过后才知道什么是合适的),不过Java提供了另一种解决方案:
import t.*;
import ing.*;
import ent.*;
public class ActionListenerTest ...{
public static void main(String[] args) ...{
JFrame frame = new JFrame(\"Button Test\");
tDefaultCloseOperation(IT_ON_CLOSE);
final JButton jbClose = new JButton(\"Close the Frame\");
dActionListener(new ActionListener () ...{
public void actionPerformed(ActionEvent e) ...{
if (tSource().equals(jbClose)) ...{
it(0);
}
}
}
);
d(jbClose);
亚马逊发言人尚未就此消息置评。目前 ck();
tVisible(true);
}
}
也就是在addActionListener的参数中新定义到一个ActionListenner并重写它的actionPerformed。不过要注意的是,这个actionPerformed一定要是public的,不然权限不够。还有就是里面用到的组件在外部必须声明为final的,这点也许会造成些许使用的限制。
另一种其实是很常用的那种,前面也用到过,不过这里再写一遍好了,翻来翻去很麻烦的。
import t.*;
import ing.*;
import ent.*;
public class ButtonFrame extends JFrame implements ActionListener ...{
JButton jbClose = null;
public ButtonFrame() ...{
super(\"ButtonFrame Test\");
jbClose = new JButton (\"Close the Frame in ButtonFrame\");
dActionListener(this);
tDefaultCloseOperation(IT_ON_CLOSE);
d(jbClose);
ck();
tVisible(true);
}
public void actionPerformed(ActionEvent e) ...{
if (tSource().equals(jbClose)) ...{
it(0);
}
}
public static void main(String[] args) ...{
ButtonFrame bf = new ButtonFrame();
}
}
两个程序的效果是一样的,都是点击了按钮后就结束程序。 查看本文来源
天津治疗白癜风去哪里九江哪里治白癜风最好
骨关节炎怎么缓解
- 上一篇:纵览覆盖
- 下一篇:中设置模态窗体和启动位置呢