Results 1 to 5 of 5
  1. #1
    xterradaniel is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default Creating a custom panel:

    I need to create a custom panel that resembles a tic tac toe board. I need it to have x's and 0'x and blank spaces put randomly in the grid (3 x 3). Here is what I have so far but I can't seem to finish it. I have only gotten this far due to some help on another forum, but now I am lost!

    What I have thus far:
    Java Code:
    import java.awt.GridLayout;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    
    public class Test2 extends JFrame {
    	
    	// Use a Constructor...
    	public Test2() {
    	       // Set up all of the basics.
    	       setSize(200, 200);
    	       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	       setLocationRelativeTo(null);
    	       setTitle("Tic Tac Toe");
    	       
    	       // Add the panel to the JFrame:
    	       add(new TicTacToeBoard());
    	       
    	       // Now, make the JFrame visible
    	       setVisible(true);
    	}
    
        public static void main(String[] args) {
        	// Make the main really small and simply call the constructor.
        	new Test2();
           
        }
    }
    
    // Pull the class out to here.
    class TicTacToeBoard extends JPanel {
        //store the text in an array
        String[] text = {"X", "O", " "};
        Random rand = new Random();
        // use a Random object to randomly select text
        String randomText = text[rand.nextInt(text.length)];
    
        private JLabel[][] label;
    
        TicTacToeBoard() {
            super(new GridLayout(3, 3));
            label = new JLabel[3][3];
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    label[i][j] = new JLabel();
                    label[i][j].setHorizontalAlignment(SwingConstants.CENTER);
                    add(label[i][j]);
                    
                    // Give them some text. Just for you to see...
                    label[i][j].setText(i + " " + j);
                }
            }
        }
    
        void setXat(int x, int y) {
            label[x][y].setText("X");
        }
    
        void setOat(int x, int y) {
            label[x][y].setText("O");
        }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you are going to cross-post, then we ask that you provide links to all cross-posts so we don't duplicate effort done elsewhere. For information on why this is important to us, please check out this link: BeForthrightWhenCrossPostingToOtherSites

    Thanks for your cooperation and welcome to the Java-Forums.

    edit: if you reply with all of your links, I will assume that you want to continue with your discussion here in this forum.

    edit 2: you'll also want to provide all these links in your cross-posts for the benefit of the helpers in the other forums. Again, my link above (BeForthrightWhenCrossPostingToOtherSites) will explain why this is important.

    Luck.
    Last edited by Fubarable; 10-03-2010 at 09:13 PM.

  3. #3
    xterradaniel is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Sorry, I am only cross-posting because I can't figure it out. I realize that is no excuse and apologize.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by xterradaniel View Post
    Sorry, I am only cross-posting because I can't figure it out. I realize that is no excuse and apologize.
    Don't be so hasty to apologize. Cross-posting is OK as long as you're upfront about with all parties involved. Again, please read the link.

  5. #5
    xterradaniel is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    got ya. I almost have it working. If I need any more help I will ask, again thanks.

Similar Threads

  1. Add panel to parent panel
    By LovJava in forum AWT / Swing
    Replies: 10
    Last Post: 09-02-2010, 09:43 PM
  2. Help Creating Custom File and Adding an Editor
    By Fondor in forum Advanced Java
    Replies: 6
    Last Post: 04-23-2010, 05:06 AM
  3. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 06:56 AM
  4. Creating custom objects
    By coltragon in forum New To Java
    Replies: 11
    Last Post: 12-29-2009, 11:50 PM
  5. Creating Custom annotations
    By ajeeb in forum New To Java
    Replies: 2
    Last Post: 04-02-2009, 09:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •