Chess Board GUI full of buttons
I have to make a program that inputs the user's count of columns and rows to use in the chess board. When the board is created it has to be full of buttons fitting the amount of columns and rows specified by the user. I figured I'd have to make the buttons an array from the input because it is impossible to figure out how many buttons nor am i going to hard code 60+ new buttons. This is what I have so far.. It prompts the user to import the number of columns and rows and sets them to the int numRows,numCols. Where I am confused is the button array, we have not covered it in class and was just wondering how to set up the array with the buttons. The rest of the program is using ActionListener and actionPerformed so when the user clicks the button it will say "Button 1 clicked" etc, any help would be appreciated thanks!
Code java:
import javax.swing.*;
import java.awt.event.*;
import java.awt.GridLayout;
public class ButtonBoard {
public static void main(String[] args){
String rows = JOptionPane.showInputDialog("Enter number of rows");
String columns = JOptionPane.showInputDialog("Enter number of columns");
int numRows = Integer.parseInt(rows);
int numCol = Integer.parseInt(columns);
JFrame frame = new JFrame();
JButton button1 = new JButton("Button1");
JButton button2 = new JButton("Button2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout((new GridLayout(numRows,numCol)));
frame.add(button1);
frame.add(button2);
frame.setSize(300,300);
frame.setVisible(true);
}
}
Re: Chess Board GUI full of buttons
Alright I got the array working with creating the buttons, and should be able to figure out the actionListener with the buttons. I was just wondering how to add the background color to the buttons? I need the buttons white and black alternating in pattern. Figured I would need an if statement but cannot get them to change in the array.
Re: Chess Board GUI full of buttons
What method calls have you tried to set the color?
Or is the problem with the logic: alternating the colors