我一直在研究一个程序,而我的主类有大量的代码,它有超过20种不同的“addActionListener”方法。我怎样才能在单独的类中创建这个actionListener,itemStateChanged等等,但仍然像现在这样执行。任何提示将是最受欢迎的,因为我已经在这个类运行超过4000行代码:(谢谢如何在java中添加一个actionListener作为单独的类
0
A
回答
1
class Mylistener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == someButton){
// do something
} else if (e.getSource() == someOtherButton){
// do something
}
// add more else if statements for other components
// e.getSource() is the component that fires the event e.g. someButton
}
}
说你有两个按钮
JButton someButton = new JButton("SOME BUTTON");
JButton someOtherButton = new JButtton("SOME OTHER BUTTON");
ActionListener listener = new MyListener();
someButton.addActionListener(listener);
someOtherButton.addActionListener(listener);
编辑:
public MyClass extends JFrame {
JButton someButton = new JButton("SOME BUTTON");
JButton someOtherButton = new JButtton("SOME OTHER BUTTON");
public MyClass(){
ActionListener listener = new MyListener();
someButton.addActionListener(listener);
someOtherButton.addActionListener(listener);
}
class Mylistener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == someButton){
// do something
} else if (e.getSource() == someOtherButton){
// do something
}
// add more else if statements for other components
// e.getSource() is the component that fires the event e.g. someButton
}
}
3
public class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent evt) {
// actionPerformed here...
}
}
你会用它喜欢!
JButton button = new JButton();
button.addActionListener(new MyActionListener());
// OR
MyActionListener listener = new MyActionListener();
JButton button = new JButton();
button.addActionListener(listener);
+0
好吧,大家感谢大家的反馈意见。我会研究你提供的所有方法。谢谢 – Iron
1
你想编写一个实现ActionListener的类。 我可以给你一些代码,这里有很少的解释,但我认为我最好指出你在这里的文档:http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
这个链接会给你一些例子,它会详细解释它是如何加工。 我希望这可以帮助。
相关问题
- 1. ActionListener在一个单独的类
- 2. 如何一个ActionListener添加到按钮的边框在Java中
- 3. 在另一个类中的Java ActionListener
- 4. 如何在另一个类的ActionListener中调用Java类
- 5. 在Java Swing GUI中将ActionListener类添加到多个JButton中?
- 6. 如何一个ActionListener添加到这个类实现了JTextField
- 7. 如何在ActionListener中实现一个类?
- 8. AsyncTask作为一个单独的类
- 9. 尝试添加一个ActionListener
- 10. 一个单独的类中使用Java
- 11. Java,使用一个ActionListener作为多个单选按钮
- 12. Java - ActionListener类的变量一致性..为什么这个工作?
- 13. 如何在android的galleryview中为物品添加两个单独的动作?
- 14. 将actionlistener添加到java中的jcheckbox中
- 15. 如何在Java中设计ActionListener类?
- 16. 将vector添加为data.table作为单独值的单独新列
- 17. 如何为单独的div添加javascript?
- 18. 如何使用ActionListener在同一个Applet中添加新面板
- 19. 如何在ActionListener中添加Swing组件?
- 20. 添加一个ActionListener到一个JList
- 21. 添加一个JPanel到一个ActionListener
- 22. 从一个单独的类中添加到数组中
- 23. 如何在Access VBA中添加表单类作为库类
- 24. 如何为Java Swing按钮制作ActionListener
- 25. 为什么在一个单独的类
- 26. 如何将值作为cummulative值添加到单独列中?
- 27. 如何将actionlistener添加到整个类中,而无需单独将其分配给组件?
- 28. 如何将actionlistener添加到Java中的文本字段中
- 29. 从另一个类添加一个actionListener到JButton
- 30. 添加另一个单独的div类时删除类
那么,我们应该提供的代码与您当前的代码的功能相同,不知道当前的代码?你的代码在哪里?你有什么尝试?面临的问题是什么? –