Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Chess Board GUI full of buttons

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

    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);
     
     
    	}
     
     
    }


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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

Similar Threads

  1. Programming USB Relay Board
    By Infinity8 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 23rd, 2013, 03:38 AM
  2. Chess Problem
    By pmg in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2011, 12:07 PM
  3. Simple Chess program
    By x3rubiachica3x in forum What's Wrong With My Code?
    Replies: 23
    Last Post: September 22nd, 2010, 11:12 AM
  4. Help with score board!
    By vlan in forum Java Applets
    Replies: 0
    Last Post: June 2nd, 2010, 04:34 AM
  5. DOS board game
    By SageNTitled in forum Java Theory & Questions
    Replies: 5
    Last Post: March 4th, 2010, 03:53 PM