在java中正确使用actionListener
我是java新手,我试图用几个按钮和下拉菜单构建一个简单的GUI。我设法得到了一个工作的GUI,但真正的问题是如何正确使用actionListener方法。为了将操作分配给按钮,我将每个actionListener插入到自己的类中,代码如下:在java中正确使用actionListener,java,user-interface,Java,User Interface,我是java新手,我试图用几个按钮和下拉菜单构建一个简单的GUI。我设法得到了一个工作的GUI,但真正的问题是如何正确使用actionListener方法。为了将操作分配给按钮,我将每个actionListener插入到自己的类中,代码如下: public class GUI implements something { public static ClientGUI App; private JFrame chatWin; private JMenuBar menuba
public class GUI implements something
{
public static ClientGUI App;
private JFrame chatWin;
private JMenuBar menubar;
private JMenu x;
private JMenu y;
private JMenuItem exit;
private JMenuItem about;
public GUI()
{
/*
* some code and parameters
*/
//creating the menu bar
JMenuBar menubar = new JMenuBar();
chatWin.setJMenuBar(menubar);
JMenu x= new JMenu("menu1");
menubar.add(x);
JMenuItem exit = new JMenuItem("menu2");
x.add(exit);
JMenu y= new JMenu("Help");
menubar.add(help);
JMenuItem about = new JMenuItem("inner menu1");
y.add(about);
//action listener for the exit button
class exitaction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
exit.addActionListener(new exitaction());
//action listener for the about button
class aboutaction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//some code
}
}
}
public static void main (String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
App = new GUI();
}
});
}
}
代码是否被认为是整洁和正确的?有什么我可以改进或需要更改的吗?它可以工作,但您可以在单独的类中甚至在主类中实现,而不是为每个按钮创建嵌套类
public class GUI implements something,ActionListener
{
//...
public GUI()
{
//...
exit.setActionListener(this);
about.setActionListener(this);
//...
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == exit)
{
System.exit(0);
} else if(source == about)
{
//action
}
}
}
您还可以为项目设置操作命令,并比较字符串而不是对象
public class GUI implements something,ActionListener
{
//...
public GUI()
{
//...
exit.setActionCommand("exit");
exit.addActionListener(this);
about.setActionCommand("about");
about.addActionListener(this);
//...
}
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
if("exit".equals(action))
{
System.exit(0);
} else if("about".equals(action))
{
//action
}
}
}
向按钮添加操作侦听器的简单直接方法:
//Create the button
JButton button_save = new JButton("Save");
add(button_save);
// Create the listener
button_save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//do your stuff when button clicked
//ex: disable the button
button_save.setEnabled(false);
}
});
我遵循规则:
如果动作实现很好,我会加入一个内部类
public MyFrame extends JFrame {
// componentes creating, etc
private static class MyShortAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
// some big implementation
}
}
}
简短的时候,我加入了一个匿名类
myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
// some small code
}
});
如果它被其他组件使用,我将提取到顶级类
public class MyAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
// some small code
}
}
我想你的代码已经正确了(我的意思是它正在工作,不是吗)。 剩下的主要是品味问题(以及代码的可用性)。例如,您可能希望稍后重用代码,或者向尚未考虑的程序中添加功能和其他按钮。因此,请尝试以下经验法则:
- 避免内部类–您永远不知道何时要在其他地方实例化该类的对象。因此,每个类使用一个文件(顺便说一句,类应该以大写字母开头)
- 将ActionListener的每个实现放入自己的类中。我知道这会毁了你的项目,但是你所有的代码都会被清晰地安排好,你甚至可以重用这些功能。例如,退出按钮。也许您决定在程序中的其他地方使用第二个退出按钮,然后您可以为这两个按钮提供相同的ActionListener,从而实现相同的功能
对于编译器来说,这不会有任何区别,但对于您来说,当您的项目开始增长并变得越来越复杂时 我不认为在主类中实现侦听器是一种改进。这将向公共API公开一个内部细节,并带来一些微妙的问题,例如,如果您序列化按钮,您还将序列化GUI和超类中的字段。谢谢大家,这真的很有帮助!非常感谢Marcio,我用了你的建议,效果很好!是的,我也喜欢你的规则。奥布里加多:-感谢详细的答案!