Java-Action Listener 检查两个按钮是否被按下

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15988488/
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-31 21:33:45  来源:igfitidea点击:

Java- Action Listener to check whether two buttons have been pressed

javaswingjbuttonactionlistener

提问by Rachel

I would like to create a program where there are 4 buttons and the user needs to match these together.

我想创建一个程序,其中有 4 个按钮,用户需要将它们匹配在一起。

So if there are 4 buttons: button1, button2, button3, button4 and the user presses button1 and button3 then the buttons change colour. else they stay the same.

因此,如果有 4 个按钮:按钮 1、按钮 2、按钮 3、按钮 4 并且用户按下按钮 1 和按钮 3,则按钮会改变颜色。否则他们保持不变。

i have tried using an action listener and an if statement within the action listener but i am not too sure how i would do it so that it checks if both buttons have been pressed.

我曾尝试在动作侦听器中使用动作侦听器和 if 语句,但我不太确定我将如何操作,以便它检查是否已按下两个按钮。

Thanks.

谢谢。

Here is my code:

这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.Color.*;
import javax.swing.Box;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.border.LineBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.BorderFactory;
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;


public class test3 extends JPanel {

    JFrame frame;
    JPanel panel;

    public test3() {

    /*Frame and panel */

    frame = new JFrame("Keyboard");
    panel = new JPanel();

    /* Buttons fot letters*/
    final JButton button1 =new JButton("button1");
    final JButton button2 =new JButton("button2");
    final JButton button3 =new JButton("button3");
    final JButton button4 =new JButton("button4");

    frame.setVisible(true);
    frame.setSize(800,600);
    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); 

    panel.setLayout(null);
    panel.setBackground(Color.WHITE);
    panel.setCursor( new Cursor(Cursor.HAND_CURSOR) ); // set the cursor to a hand 

    frame.add(panel);

    Insets insets = panel.getInsets();

    button1.setLayout(null);
    button1.setBounds(130 + insets.left, 300 + insets.top, 50,50);
    button1.setBackground(Color.WHITE);
    button1.setBorder(BorderFactory.createEmptyBorder());

    button2.setLayout(null);
    button2.setBounds(180 + insets.left, 300 + insets.top,  50,50);
    button2.setBackground(Color.WHITE);
    button2.setBorder(BorderFactory.createEmptyBorder());

    button3.setLayout(null);
    button3.setBounds(230 + insets.left, 300 + insets.top,  50,50);
    button3.setBackground(Color.WHITE);
    button3.setBorder(BorderFactory.createEmptyBorder());

    button4.setLayout(null);
    button4.setBounds(280 + insets.left, 300 + insets.top,  50,50);
    button4.setBackground(Color.WHITE);
    button4.setBorder(BorderFactory.createEmptyBorder());

    panel.add(button1);
    panel.add(button2); 
    panel.add(button3);
    panel.add(button4);
    }


    public static void main(String[] args) {
    new test3();
    }
}

回答by A human being

Since at a time, only one ActionEvent will be fired, you need to set a flag whenever button1 or button3 is pressed for first time, second time, whenever a button1/button3 is pressed, check the status of the flag, if it's true change the color of the button, else don't change. Also don't forget to set the flag to false, when you'll be done with color change.Here is the code:

因为一次只会触发一个 ActionEvent,所以你需要在第一次按下 button1 或 button3 时设置一个标志,第二次按下 button1/button3 时,检查标志的状态,如果是真的改变按钮的颜色,否则不要改变。也不要忘记将标志设置为 false,当您完成颜色更改时。这是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.Color.*;
import javax.swing.Box;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.border.LineBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.BorderFactory;
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;

public class test3 extends JPanel implements ActionListener {

    JFrame frame;
    JPanel panel;
    boolean flag;
    String buttonPressed;
    final JButton button1;
    final JButton button2;
    final JButton button3;
    final JButton button4;
    Color color1, color2;

    public test3() {

    /*Frame and panel */

    frame = new JFrame("Keyboard");
    panel = new JPanel();
    color1 = new Color(0,0,0);

    /* Buttons fot letters*/
    button1 =new JButton("button1");
    button2 =new JButton("button2");
    button3 =new JButton("button3");
    button4 =new JButton("button4");

    color2 = button1.getBackground();

    frame.setVisible(true);
    frame.setSize(800,600);
    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); 

    panel.setLayout(null);
    panel.setBackground(Color.WHITE);
    panel.setCursor( new Cursor(Cursor.HAND_CURSOR) ); // set the cursor to a hand 

    frame.add(panel);

    Insets insets = panel.getInsets();

    button1.setLayout(null);
    button1.setBounds(130 + insets.left, 300 + insets.top, 50,50);
    button1.setBackground(Color.WHITE);
    button1.setBorder(BorderFactory.createEmptyBorder());

    button2.setLayout(null);
    button2.setBounds(180 + insets.left, 300 + insets.top,  50,50);
    button2.setBackground(Color.WHITE);
    button2.setBorder(BorderFactory.createEmptyBorder());

    button3.setLayout(null);
    button3.setBounds(230 + insets.left, 300 + insets.top,  50,50);
    button3.setBackground(Color.WHITE);
    button3.setBorder(BorderFactory.createEmptyBorder());

    button4.setLayout(null);
    button4.setBounds(280 + insets.left, 300 + insets.top,  50,50);
    button4.setBackground(Color.WHITE);
    button4.setBorder(BorderFactory.createEmptyBorder());

    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);

    panel.add(button1);
    panel.add(button2); 
    panel.add(button3);
    panel.add(button4);
    flag = false;
    buttonPressed = "";
    }

    public void actionPerformed(ActionEvent ae)
    {
    JButton b = (JButton)ae.getSource();
    if( b.equals(button1) )
        {
        flag = true;
        if( buttonPressed.equals("button3") )
            {
            if( button1.getBackground().equals(color2) )
                {
                button1.setBackground(color1);
                button3.setBackground(color1);
                }
            else
                {
                button1.setBackground(color2);
                button3.setBackground(color2);
                }
            flag = false;
            }
        buttonPressed = "button1";
        }
    else if( b.equals(button3) ) 
        {
        flag = true;
        if( buttonPressed.equals("button1") )
            {
            if( button3.getBackground().equals(color2) )
                {
                button1.setBackground(color1);
                button3.setBackground(color1);
                }
            else
                {
                button1.setBackground(color2);
                button3.setBackground(color2);
                }
            flag = false;
            }
        buttonPressed = "button1";
        }
    else
        {
        flag = false;
        }

    }

    public static void main(String[] args) {
    new test3();
    }
}

回答by 0x6C38

You are better off using toogleButtons:

你最好使用toogleButtons

    final JToggleButton tb1 = new JToggleButton();
    final JToggleButton tb2 = new JToggleButton();
    final JToggleButton tb3 = new JToggleButton();
    final JToggleButton tb4 = new JToggleButton();


    ChangeListener stateChangeListener = new javax.swing.event.ChangeListener() {
        @Override
        public void stateChanged(javax.swing.event.ChangeEvent evt) {
            toggleButtonStateChanged(evt);
        }

        private void toggleButtonStateChanged(ChangeEvent evt) {
            if (tb1.isSelected() && tb3.isSelected()) {
                tb1.setBackground(Color.WHITE);
                tb2.setBackground(Color.WHITE);
                tb3.setBackground(Color.WHITE);
                tb4.setBackground(Color.WHITE);
            } else {
                //Set the color to whatever it was before
            }
        }
    };

    tb1.addChangeListener(stateChangeListener);
    tb2.addChangeListener(stateChangeListener);
    tb3.addChangeListener(stateChangeListener);
    tb4.addChangeListener(stateChangeListener);