//MY Problem is that i'm trying to make a lottery program, the issue i have is when i click a button, i want it to be so that after i click 6 buttons, lets say ,1,2,3,4,5,6, the program will move onto the next stage( i have yet to build that)
Howver it allows me click as many buttons as i want and never ends...
Can i put a loop on action listener somehow? I did that but all it did was loop 6 times through the one button, so essentially print out 1 six times and so on..
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.util.Random;
import javax.swing.JOptionPane;
public class DisplayButtonMessage
{
public DisplayButtonMessage()//Main constructor
{
//method for the everything that happens
JFrame frame = new JFrame("Lottery App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(11, 4));//create gridlayput to hold buttons
ActionListener al = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
//display action command of jbutton
String num = ((JButton) event.getSource()).getActionCommand();
System.out.println(num);
}
};
for (int i = 0; i < 44; i++)
{
JButton b = new JButton("No: " + String.valueOf((i + 1)));
//Creates a button each iteration
//It labels each number the value of counter+1
b.setActionCommand(String.valueOf((i + 1)));
//This gets the value of the button, counter + 1 also
b.addActionListener(al);//Adds action listener for above action Performed
panel.add(b);//Adds "b" to panel
}
frame.add(panel);//Adds the panel to frame
frame.pack();
frame.setVisible(true);//Sets the frame visible
}}