Results 1 to 9 of 9
- 07-01-2010, 05:16 PM #1
Swing question: Making a visual tic tac toe
This is a two part question.
First, im planning on making 9 objects on the form, and the user clicks on the object and it either shows an "X" or "O" depending on which players turn it is.
Now the obvious type of object i think is best to use is a JLabel. is this correct?
And my second questions is:
If i do use JLabels, how do i set the JLabels to a certain height and width so that they resemble a box shape
note to self http://www.thestudentroom.co.uk/show....php?t=1198718Last edited by alacn; 07-02-2010 at 12:19 PM.
Teaching myself java so that i can eventually join the industry! Started in June 2010
- 07-01-2010, 05:47 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 07-01-2010, 10:43 PM #3
edit sorted
Last edited by alacn; 07-01-2010 at 10:47 PM.
Teaching myself java so that i can eventually join the industry! Started in June 2010
- 07-02-2010, 01:19 PM #4
Which type of layout manager?
ill ask the question here to safe making a new thread.
in the below picture

im assuming there is two JPanels in a frame. I can see the bottom JPanel would use the flowLayout type for the buttons, but what type of layout manager would you use the JPanel above that?Teaching myself java so that i can eventually join the industry! Started in June 2010
- 07-02-2010, 02:05 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 07-04-2010, 11:16 PM #6
k im having problems with the code below and nothing i try seems to make the JPanel appear in the top-left corner of the container.
I have my heart set on using the layout manager GridBagLayout because i want to understand it better
from this snipet of code below, u can see the code i tried using to get the gameArea JPanel in the top-left corner of the container, but it didnt work
it just keeps placing the JPanel in the middle of the screen rather than the top left where i want it.PHP Code:c.gridx =0; c.gridy=0; c.anchor = GridBagConstraints.NORTHWEST; contentPane.add(gameArea,c);
please help, thanks
entire code
PHP Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tictac; import java.awt.event.*; import java.awt.*; import javax.swing.*; public class GUI extends JFrame { //player player[] = new player[2]; FlowLayout flowlayout = new FlowLayout(); GridLayout gridlayout = new GridLayout(0,3); GridBagLayout gridbaglayout = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); BorderLayout borderLayout = new BorderLayout(); JPanel contentPane; Font ticTacFont; public GUI(){ super("Tic Tac Toe"); } public void addComponents(){ contentPane = (JPanel) getContentPane(); //main container //contentPane.setLayout(new GridLayout(2,1)); contentPane.setLayout(new GridBagLayout()); GameFrame gameFrame = new GameFrame(); // main game frame added via class StatFrame statFrame = new StatFrame(); // frame with the stats added via class gameFrame.setNewGame(); //gameArea.setPreferredSize(new Dimension(30,1000)); // container.add(gameArea,BorderLayout.WEST); } public class GameFrame{ JLabel jl[] = new JLabel[9]; // 9 labels containing 0 OR X JPanel gameArea = new JPanel(); //tic tac toe frame public GameFrame(){ gameArea.setLayout(gridlayout); c.gridx =0; c.gridy=0; c.anchor = GridBagConstraints.NORTHWEST; contentPane.add(gameArea,c); ticTacFont = new Font("Serif",Font.BOLD,48); } public void setNewGame(){ for(int i = 0;i <jl.length;i++){ jl[i] = new JLabel(" - ",JLabel.CENTER); jl[i].setFont(ticTacFont); jl[i].setOpaque(true); jl[i].setBackground(Color.WHITE); jl[i].setBorder(BorderFactory.createLineBorder(Color.BLUE)); //jl[i].setAlignmentY(CENTER_ALIGNMENT); gameArea.add(jl[i]); //add labels to the frame } } } public class StatFrame{ JPanel statsArea = new JPanel(); //stats frame public StatFrame(){ statsArea.setLayout(gridbaglayout); contentPane.add(statsArea); } public void setBuildFrame(){ } } public static void main(String[] args){ //Beginning of code here GUI form = new GUI(); form.setVisible(true); form.addComponents(); form.pack(); form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }Last edited by alacn; 07-04-2010 at 11:32 PM.
Teaching myself java so that i can eventually join the industry! Started in June 2010
- 07-04-2010, 11:47 PM #7
When I execute the code, I get a small window in the upper left corner of the screen with a 3x3 grid of white boxes with blue borders with a black dash in the middle of each.
What do you see?
- 07-05-2010, 12:47 AM #8
This one needs some Heavy GUI types. I added some debugging code and get a contradiction/overridden value shown. The gbc.anchor is set but when retrieved is still the default.
Java Code:// Following Moved here gbc.gridx =0; gbc.gridy=0; gbc.anchor = GridBagConstraints.NORTHWEST; contentPane.add(gameArea, gbc); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< HERE ((GridBagLayout)contentPane.getLayout()).getConstraints(contentPane).anchor = GridBagConstraints.NORTHWEST; System.out.println("layout=" + contentPane.getLayout() + ", cP.gbc.anchor=" +((GridBagLayout)contentPane.getLayout()).getConstraints(contentPane).anchor + ", gbc.anchor=" + gbc.anchor + ", NW=" + GridBagConstraints.NORTHWEST); //layout=java.awt.GridBagLayout, cP.gbc.anchor=10, gbc.anchor=18, NW=18 ????? 10 = CENTER // ^^
-
If you're going to use GridBagLayout, you'll need to use the other properties of the GridBagConstraints including the gridx, gridy, and weightx and weighty. For e.g.,
Java Code:import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.*; public class GUI2 { private static void createAndShowUI() { JFrame frame = new JFrame("GUI2"); frame.getContentPane().add(new Gui2Panel().getMainPanel(), BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } class Gui2Panel { private JPanel mainPanel = new JPanel(new GridBagLayout()); public Gui2Panel() { JPanel leftUpperPanel = new JPanel(); leftUpperPanel.setBackground(new Color(200, 255, 255)); leftUpperPanel.setBorder(BorderFactory.createTitledBorder("Left Upper Panel")); leftUpperPanel.setPreferredSize(new Dimension(200, 250)); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.NONE; gbc.anchor = GridBagConstraints.NORTHWEST; mainPanel.add(leftUpperPanel, gbc); } public JPanel getMainPanel() { return mainPanel; } }
Similar Threads
-
making a monogram swing
By moostico246 in forum AWT / SwingReplies: 29Last Post: 04-07-2009, 04:08 AM -
How to get back from Visual Design to Visual MIDlet?
By sknn in forum NetBeansReplies: 0Last Post: 12-08-2008, 02:30 PM -
Simple Question Making a Variable Accessable throught the code
By 2o2 in forum New To JavaReplies: 1Last Post: 10-02-2008, 03:06 AM -
code for making a java swing program a demo verson
By fakhruddin in forum AWT / SwingReplies: 1Last Post: 11-27-2007, 08:54 PM -
Making a session in swing
By sandor in forum AWT / SwingReplies: 3Last Post: 04-22-2007, 10:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks