Results 1 to 5 of 5
Thread: Creating a custom panel:
- 10-03-2010, 08:01 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
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"); } }
-
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 08:13 PM.
- 10-03-2010, 08:11 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
Sorry, I am only cross-posting because I can't figure it out. I realize that is no excuse and apologize.
-
- 10-03-2010, 08:40 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Add panel to parent panel
By LovJava in forum AWT / SwingReplies: 10Last Post: 09-02-2010, 08:43 PM -
Help Creating Custom File and Adding an Editor
By Fondor in forum Advanced JavaReplies: 6Last Post: 04-23-2010, 04:06 AM -
Adding a panel to a panel
By rclausing in forum New To JavaReplies: 7Last Post: 02-02-2010, 05:56 AM -
Creating custom objects
By coltragon in forum New To JavaReplies: 11Last Post: 12-29-2009, 10:50 PM -
Creating Custom annotations
By ajeeb in forum New To JavaReplies: 2Last Post: 04-02-2009, 08:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks