Results 1 to 9 of 9
  1. #1
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default 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=1198718
    Last 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

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by alacn View Post
    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
    Use a GridLayout (3x3) and three types of labels: an empty label, a label with an X and one with an O. If you don't want the user to fiddle with the size of the board make the containg JFrame not resizable.

    kind regards,

    Jos

  3. #3
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    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

  4. #4
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Post 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

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by alacn View Post
    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?
    A BorderLayout?

    kind regards,

    Jos

  6. #6
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    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
    PHP Code:
    c.gridx =0; c.gridy=0;
    c.anchor = GridBagConstraints.NORTHWEST;
                    
    contentPane.add(gameArea,c);
    it just keeps placing the JPanel in the middle of the screen rather than the top left where i want it.

    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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    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?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    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
                    //                                             ^^

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

    Default

    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

  1. making a monogram swing
    By moostico246 in forum AWT / Swing
    Replies: 29
    Last Post: 04-07-2009, 04:08 AM
  2. Replies: 0
    Last Post: 12-08-2008, 02:30 PM
  3. Replies: 1
    Last Post: 10-02-2008, 03:06 AM
  4. code for making a java swing program a demo verson
    By fakhruddin in forum AWT / Swing
    Replies: 1
    Last Post: 11-27-2007, 08:54 PM
  5. Making a session in swing
    By sandor in forum AWT / Swing
    Replies: 3
    Last Post: 04-22-2007, 10:58 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
  •