0

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

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

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

谢谢。

这是我的代码:

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();
    }
}
4

2 回答 2

4

由于一次只会触发一个 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();
    }
}
于 2013-04-13T14:03:06.183 回答
2

你最好使用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);
于 2013-04-13T14:19:54.977 回答