我想创建一个程序,其中有4个按钮和用户需求匹配在一起这些。Java的操作侦听器,检查两个按钮是否被按下
因此,如果有4个按钮:按钮1,按钮2,BUTTON3,将Button4和用户按压按钮1和按钮3然后将按钮改变颜色。否则他们保持不变。
我已如果动作侦听器内语句中使用的动作侦听器和一个可靠的,但我也不太清楚,以便它检查,如果这两个按钮被按下我会怎么做。
谢谢。
这里是我的代码:
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();
}
}
你去哪里在你的代码中使用了ActionListener接口? –
我用它只是一个按钮,例如。 button1.addActionListener(new ActionListener);当我按下一个按钮时,我可以让它做些事情,但是我不能让它工作,这样当按钮1和按钮3都被按下时,就做一些事情。 – Rachel
无论如何,感谢张贴无关代码50线和您问题相关的0线。 –