简体   繁体   English

Swing:如何使用多个按钮?

[英]Swing: How do I use multiple buttons?

I created this little test program. 我创建了这个小测试程序。 It has 2 buttons and 2 labels. 它有2个按钮和2个标签。 I want to know how I can use 2 buttons. 我想知道如何使用2个按钮。 So when I press button-1 then I change the text for text-1 and when I press button-2 then I change text for text-2. 因此,当我按下按钮1时,我更改了文本1的文本,当我按下按钮2时,我更改了文本的文本2。 I just wanna get an idea of how I can use multiple buttons. 我只是想知道如何使用多个按钮。

My code: 我的代码:

JLabel text1, text2;
JButton button1, button2;

public Game(String title) {
    super(title);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().setLayout(new FlowLayout());

    addComponents();

    setSize(250, 250);
    setResizable(false);

}

public void addComponents() {
    text1 = new JLabel();
    getContentPane().add(text1, text2);

    text2 = new JLabel();
    getContentPane().add(text2);

    button1 = new JButton("Button");
    getContentPane().add(button1);
    button1.addActionListener(this);

    button2 = new JButton("Button 2");
    getContentPane().add(button2);
    button2.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {


}

I'm new to programming, so I would also like if someone could write some comments for the code. 我是编程的新手,所以我也想知道是否有人可以为代码写一些注释。 Just so I get an idea on how the code for multiple buttons work. 这样我就可以了解多个按钮的代码如何工作。

In your actionPerformed method you can get the source of the action actionPerformed方法中,您可以获取操作的来源

@Override
public void actionPerformed(ActionEvent e) {

if(e.getSource() == button1){
 //Do Something
}else if(e.getSource() == button2){
 //Do Something Else
}

There are various approaches to add listeners to buttons, here just a couple: 有多种方法可以将侦听器添加到按钮,这里仅提供几种方法:

Inner

If you don't have to do much actions in each button you can add inner listener in each button 如果您不必在每个按钮中执行很多操作,则可以在每个按钮中添加内部侦听器

button1.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // DO STUFF

    }
});

Common Listener 普通听众

If you have more than 2 buttons (i guess your app will be bigger) you can use your actionPerformed(ActionEvent e) and get source of the action 如果您有2个以上的按钮(我想您的应用程序会更大),则可以使用actionPerformed(ActionEvent e)并获取操作的来源

@Override
public void actionPerformed(ActionEvent e) {
    JButton source = (JButton) e.getSource();
    if(source.equals(button1)){
        // DO STUFF    
    }
}

Use actionCommand to clarify 使用actionCommand进行澄清

To clarify this approach I would reccommend to use JButton.setActionCommand(stringCommand) so after you can use a switch : 为了阐明这种方法,我建议使用JButton.setActionCommand(stringCommand)以便在使用switch

Declaring buttons: 声明按钮:

button1.setActionCommand("command1");
button2.setActionCommand("command2");

In ActionListener::actionPerformed() ActionListener::actionPerformed()

public void actionPerformed(ActionEvent e) {
    String command = ((JButton) e.getSource()).getActionCommand();

    switch (command) {
    case "command1": 
        // DO STUFF FOR BUTTON 1
    break;
    case "command2": 
        // DO STUFF FOR BUTTON 2
    break;
    }
}

You should not use setVisible(true) before the components are added. 在添加组件之前,不应使用setVisible(true)

There are a few ways to deal with more elements in an ActionEvent: 有几种方法可以处理ActionEvent中的更多元素:

  • e.getSource() returns the object on which the event occurred. e.getSource()返回发生事件的对象。 So, if button1 was pressed, e.getSource() will be the same as button1 (and e.getSource()==button1 will thus be true) 因此,如果按下button1,则e.getSource()将与button1相同(因此e.getSource()==button1将为true)
  • You can use separate classes for each ActionEvent. 您可以为每个ActionEvent使用单独的类。 If you would add the ActionListener "Button1ActionEvent" [ button1.addActionListener(new Button1ActionEvent()); 如果要添加ActionListener“ Button1ActionEvent” [ button1.addActionListener(new Button1ActionEvent()); ] you have to create this class, let it implement ActionListener and add the method actionPerformed as you had in your main class. ],您必须创建此类,使其实现ActionListener并添加方法actionPerformed,就像在主类中一样。 Also, you can create a listener inside of the addActionListener-method [ button1.addActionListener(new ActionListener() { // actionPerformed-method here }); 另外,您可以在addActionListener方法[ button1.addActionListener(new ActionListener() { // actionPerformed-method here });内部创建侦听器button1.addActionListener(new ActionListener() { // actionPerformed-method here }); ] ]

Using Java 8, its it much more concise to add ActionListeners: 使用Java 8,添加ActionListeners更为简洁:

button.addActionListener(ae -> System.out.println("foo"));

Using multiple statements: 使用多个语句:

button.addActionListener(ae -> { 
    System.out.println("foo");
    System.out.println("bar");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM