Welcome to Dream.In.Code
Become a Java Expert!

Join 150,375 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,537 people online right now. Registration is fast and FREE... Join Now!




Problems with JFrame

 
Reply to this topicStart new topic

Problems with JFrame, my items are not showing up!

Mastergeek666
15 Feb, 2008 - 11:08 AM
Post #1

D.I.C Head
Group Icon

Joined: 10 Aug, 2007
Posts: 120


Dream Kudos: 75
My Contributions
Hey, haven't been on in a while but the problem I'm having is that I create a JFrame method for my TicTacToe game and my intro frame is suppose to show a gif and a button that while later on make 4 other frames visible for my 3-D Tic Tac Toe! If anyone could help out that would be great. Thanks in advance!
CODE

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JFrame implements ActionListener, ItemListener
{
    JFrame Intro = new JFrame();
    JFrame Stats = new JFrame();
    JFrame Top = new JFrame();
    JFrame Mid = new JFrame();
    JFrame Bot = new JFrame();
    public TicTacToe()
    {
        Intro();
    }
    public JFrame Intro()
    {
        Intro.setTitle("IT'S TIC-TAC-TOE!");
        Intro.setBounds(250, 250, 300, 150);
        Intro.setVisible(true);
        Intro.setResizable(false);
        Intro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pane = new JPanel();
        Container con = getContentPane();
        JLabel pic = new JLabel(new ImageIcon("funny.gif"));
        JButton start = new JButton("LET'S PLAY!");
        start.addActionListener(this);
        pane.add(pic, BorderLayout.NORTH);
        pane.add(start, BorderLayout.SOUTH);
        con.add(pane);
        return Intro;
    }
    public JFrame Stats()
    {
        return Stats;
    }
    public JFrame Top()
    {
        return Top;
    }
    public JFrame Mid()
    {
        return Mid;
    }
    public JFrame Bot()
    {
        return Bot;
    }
    public void actionPerformed(ActionEvent e)
    {

    }
    public void itemStateChanged(ItemEvent e)
    {
        
    }
    public static void main(String[] args)
    {
        TicTacToe panel = new TicTacToe();
    }
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Problems With JFrame
15 Feb, 2008 - 11:35 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Be sure to specify which frame you are getting the Content Pane for.

CODE

Container con = Intro.getContentPane();


And secondly put your Intro.setVisible(true); statement towards the buttom after you have added all your controls.

CODE

public JFrame Intro()
{
        Intro.setTitle("IT'S TIC-TAC-TOE!");
        Intro.setBounds(250, 250, 300, 150);

        
        Intro.setResizable(false);
        Intro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pane = new JPanel();
        Container con = Intro.getContentPane();
        JLabel pic = new JLabel(new ImageIcon("funny.gif"));
        JButton start = new JButton("LET'S PLAY!");
        start.addActionListener(this);
        pane.add(pic, BorderLayout.NORTH);
        pane.add(start, BorderLayout.SOUTH);
        con.add(pane);

        // Put the setVisible down here so that it will show everything you added.
        Intro.setVisible(true);
        return Intro;
}


By putting the setVisible up at the top, you are showing your frame and then adding content. Your components will not be "realized". So add your content to the GUI first and then trigger the realization by using setVisible on the frame.

smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:12PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month