Results 1 to 2 of 2
  1. #1
    Mastergeek666 is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Exclamation Problems with JPanels and displaying

    Hey I'm having a problem, I made one panel for my buttons and then when I made another to put a label underneath the buttons, my buttons disappeared. Can anyone help?
    /code
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TicTacToe extends JFrame implements ActionListener
    {
    private JButton btns[];
    Font f12 = new Font("Freestyle Script", 0, 42);

    public TicTacToe()
    {
    super ("Tic-Tac-Toe");
    btns = new JButton[9];
    JPanel pane = new JPanel();
    JPanel pane2 = new JPanel();
    JLabel players = new JLabel("TEST");
    Container con = getContentPane();
    con.add(pane);
    con.add(pane2);
    pane2.add(players);
    for(int i = 0; i < 9; i++)
    {
    btns[i] = new JButton(String.valueOf(i));
    btns[i].setFont(f12);
    btns[i].setText("");
    }
    for(int i = 0; i < btns.length; i++)
    {
    btns[i].addActionListener(this);
    pane.add(btns[i]);
    btns[i].setPreferredSize(new Dimension(100, 100));
    }
    setBounds(400, 250, 400, 550);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    }

    public static void main(String[] args)
    {
    TicTacToe panel = new TicTacToe();
    }
    public void actionPerformed(ActionEvent e)
    {
    for (int i=0; i<btns.length; i++)
    {
    if(e.getSource() == btns[i])
    {
    switch(i)
    {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    btns[i].setText("X");
    }
    }
    }
    }
    void Turns()
    {
    int x = 0;
    int o = 1;
    }
    void Player1(int x)
    {

    }
    void Player2(int o)
    {

    }
    }
    \code

    There's my code, right now im just doing design but I'm having problems with the panels and the label.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    Container con = getContentPane();
    System.out.println("default layout manager for JFrame = " +
                        getLayout().getClass().getName());
    // Looks like you might want the buttons on "pane" to be in the center of your
    // BorderLayout. Adding a component to this layouts container with no constraint
    // places it in the center section, by default. This is peculiar to BorderLayout.
    // So this next line is okay.
    con.add(pane);
    // Adding another component to a section/container which already contains a
    // component will cause the first component to be removed and the second
    // component to be added. This is the answer to your stated question.
    // Looks like you might want the JLabel "players" in the south/last
    // section of the BorderLayout. For this you must use a constraint  see
    // BorderLayout api Field Summary section for options. To get started try:
    con.add(pane2, BorderLayout.SOUTH);

Similar Threads

  1. displaying string
    By jamborta in forum AWT / Swing
    Replies: 6
    Last Post: 01-23-2008, 07:15 PM
  2. Can't synchronize multiple JPanels in a JFrame
    By vassil_zorev in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2007, 04:22 PM
  3. How to add Images to JPanels?
    By Soda in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 05:54 PM
  4. Displaying charts in a jsp
    By Priyanka in forum Java Servlet
    Replies: 1
    Last Post: 11-16-2007, 10:59 AM
  5. Images not displaying in JSP in IE7
    By chadscc in forum Advanced Java
    Replies: 0
    Last Post: 11-13-2007, 03:24 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
  •