多个按钮和多个监听器执行各种操作 Java Swing

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9913425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:52:41  来源:igfitidea点击:

Multiple buttons and multiple listeners doing various operations Java Swing

javaswingevent-listener

提问by jibbajava

How can I have multiple buttons and multiple listeners doing various operations in java swing. Here is an example of what I have, I can redirect to the AddStudent class but the button to redirect to the AddAdult class wont redirect to the AddAdult class.

我怎样才能让多个按钮和多个侦听器在 java swing 中执行各种操作。这是我所拥有的示例,我可以重定向到 AddStudent 类,但重定向到 AddAdult 类的按钮不会重定向到 AddAdult 类。

private class ButtonHandler implements ActionListener {
  // handle button event
  public void actionPerformed( ActionEvent Student ) {
    if ( Student.getSource() == button1 ){
      try {
        AddStudent newmember = new AddStudent();
        newmember.setVisible( true );
      } catch ( Exception e1 ) {
        e1.printStackTrace();
      }
  }
}


public void actionPerformed( ActionEvent Adult ) {
  if ( Adult.getSource() == button2 ){
    try {
      AddAdult newmember = new AddAdult();
      newmember.setVisible( true );
    } catch ( Exception e1 ) {
      e1.printStackTrace();
    }
  }
}

Thanks for any help in advance.

提前感谢您的任何帮助。

回答by Robin

You can attach an ActionListenerto each JButton, as explained in the Swing tutorial for buttons

您可以将 附加ActionListener到每个JButton,如按钮Swing 教程中所述

So you will end up with something like

所以你最终会得到类似的东西

JButton firstButton = ...;
firstButton.addActionListener( myFirstButtonActionListener );

JButton secondButton = ...;
secondButton.addActionListener( mySecondActionListener );

//add them to a UI
contentPane.add( firstButton );
contentPane.add( secondButton );

For more specific questions about your program and your button you will need to provide us with more code then is currently available in your question (in other words, post an SSCCE)

有关您的程序和按钮的更具体问题,您需要向我们提供比您的问题中当前可用的更多代码(换句话说,发布SSCCE

回答by paulhudson

If you want to have multiple handlers, you can define multiple classes each implementing ActionListener interface with appropriate logic implemented and attach it to appropriate buttons. If you want to use single handler for all buttons, you can use getActionCommand() (more clear than using getSource()) method of ActionEvent to check for the button text and implement your handling logic accordingly using if else.

如果您想要多个处理程序,您可以定义多个类,每个类实现 ActionListener 接口并实现适当的逻辑并将其附加到适当的按钮。如果您想对所有按钮使用单个处理程序,您可以使用 ActionEvent 的 getActionCommand()(比使用 getSource() 更清晰)方法来检查按钮文本并使用 if else 相应地实现您的处理逻辑。

回答by mKorbel

you have three another ways

你还有另外三种方式

1) use Swing Action

1) 使用摇摆动作

       myPanel.add(new JButton(new AbstractAction("some narrative") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                //some stuff
            }
        }));

2) use inner ActionListener

2)使用内部ActionListener

code

代码

myButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent ae) {
        //some stuff
    }
});

3) use EventHandler

3) 使用事件处理器