我想创建一个程序,其中有4个按钮,用户需要将这些按钮匹配在一起。
因此,如果有4个按钮:button1,button2,button3,button4,用户按下button1和button3,则按钮会改变颜色。否则他们保持不变。
我已尝试在动作侦听器中使用动作侦听器和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();
}
}
答案 0 :(得分: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();
}
}
答案 1 :(得分: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);