I have 3 buttons in 3 lines: green, yellow and red. All of them are in their own array.
When I press the green button, the other 2 buttons on the same line should be disabled. But I'm not sure how to handle this using arrays.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener {
View view = new View();
JButton bGreen[] = new JButton[3];
JButton bYellow[] = new JButton[3];
JButton bRed[] = new JButton[3];
public Test() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(300, 100, 500, 400);
this.setVisible(true);
this.setLayout(new GridLayout(3, 3));
makeButtons();
}
public void makeButtons() {
for (int i = 0; i < 3; i++) {
bGreen[i] = new JButton("Green");
bYellow[i] = new JButton("Yellow");
bRed[i] = new JButton("Red");
bGreen[i].setBackground(Color.green);
bYellow[i].setBackground(Color.yellow);
bRed[i].setBackground(Color.red);
bGreen[i].addActionListener(this);
bYellow[i].addActionListener(this);
bRed[i].addActionListener(this);
this.add(bGreen[i]);
this.add(bYellow[i]);
this.add(bRed[i]);
}
}
@Override
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source == bGreen)
{
}
if (source == bYellow)
{
}
}
public static void main(String[] args) {
Test test = new Test();
}
}
source
share