I was just starting to learn java swing, and I was trying to create a simple game. The game is like a minesweeper. Window with a matrix of bottles with one shaft. When I press a button, if itโs not mine, I will turn off the button and show green, and if itโs mine, I will turn off the button and show red. I displayed the color by setting the button background to the desired color. I made the implementation so far just fine. Then I added a reset button, by clicking on which I will change all the buttons using: setEnabled (true).
But for some reason, the button is not activated. I have confirmed that the program stream reaches the code to enable the button, but I cannot find the reason why it does not work.
Here is a test program that I wrote using the reset button and 1. The same question. Can someone point out what I'm possibly doing wrong?
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class Test implements ActionListener{ JFrame frame = new JFrame("Mine"); JButton buttons = new JButton(); JButton reset = new JButton("Reset"); Container grid = new Container(); public Test(){ frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setLayout(new BorderLayout()); frame.add(reset, BorderLayout.NORTH); reset.addActionListener(this); buttons = new JButton(); buttons.addActionListener(this); frame.add(buttons, BorderLayout.CENTER); } public static void main(String[] args){ new Test(); } @Override public void actionPerformed(ActionEvent event) { if(event.getSource().equals(reset)) buttons.setEnabled(true); else{ if(event.getSource()==buttons){ buttons.setBackground(Color.RED); buttons.setEnabled(false); } } } }
source share