Basically im practising java and came up on a challenge that i have yet to overcome.
I have added an ActionListener but i want each button to have there own Action.
Also what i wanted is to hide the contents in the button.
Would be much appreciated to see how this is done.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyGrid {
static JButton[] btnarray;
static JFrame main;
public static void main(String[] args)
{
btnarray = new JButton[6];
main = new JFrame("Grid with Fractions");
main.setVisible(true);
main.setLayout(new GridLayout(3,3));
main.setBounds(350,250,350,350);
main.setResizable(true);
for (int i = 0; i < 6 ; i++)
{
btnarray[i] = new JButton(i/2+"");
main.add(btnarray[i]);
//Add action listener to button
btnarray[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent g)
{
//Execute when button is pressed
System.out.println("You clicked the button");
//System.out.println("Print" +btnarray[i]);
}
});
}
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Exit when the user press "X"
}}