Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-18-2008, 10:39 PM
Member
 
Join Date: Jan 2008
Posts: 1
Mastergeek666 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-19-2008, 01:32 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,015
hardwired is on a distinguished road
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);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
displaying string jamborta AWT / Swing 6 01-23-2008 08:15 PM
Can't synchronize multiple JPanels in a JFrame vassil_zorev AWT / Swing 0 12-30-2007 05:22 PM
How to add Images to JPanels? Soda New To Java 3 12-08-2007 06:54 PM
Displaying charts in a jsp Priyanka Java Servlet 1 11-16-2007 11:59 AM
Images not displaying in JSP in IE7 chadscc Advanced Java 0 11-13-2007 04:24 PM


All times are GMT +3. The time now is 06:12 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org