Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2006

Tip: Looking for answers? Try searching our database.

switching of showing 2 panels

Thread view: 
Kova - 26 Apr 2006 02:56 GMT
Hi. My game has a menu panel with passive rendering (swing) and
animation panel with active rendering. I want to be able switch between
2 of them, so only one is shown at the time. For example, when I click
start game in menu panel then menu panel should hide and animation panel
become visible for user to play. Same thing if I want from game back to
main menu. How is this done with 2 panels?
I've tried adding both panels to JFrame container and just seting
visible one of them at the time, but dosen't work, see example code
below. Can somebody help me with this?

Here's the example code, it adds 2 panels both with JTextField to show
text that identifies what panel is displayed. Clicking on left side
should show panel2 and clicking right side should show panel1, but for
right side it dosen't work.

--------------------------------------------------------
import java.awt.event.*;
import javax.swing.*;

public class TFrame extends JFrame implements MouseListener {
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JTextField tf1 = new JTextField("Test 1");
    JTextField tf2 = new JTextField("Test 2");
   
    public TFrame() {
        super("TEST");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel1.add(tf1);
        panel2.add(tf2);
        panel1.setVisible(false);
        panel2.setVisible(false);
       
        add(panel1);
        add(panel2);
        setBounds(0,0,200,100);
        setVisible(true);
       
        addMouseListener(this);
       
    }
   
    public static void main(String[] args) {
        new TFrame();
    }

    public void mouseClicked(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {
        if (e.getX() < 99) {
            System.out.println("tf2 should appear");
            panel1.setVisible(false);
            panel2.setVisible(true);
            repaint();
        } else {
            System.out.println("tf1 should appear");
            panel2.setVisible(false);
            panel1.setVisible(true);
            repaint();
        }
    }
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}

} // end of class

--
Kova
Paul Tomblin - 26 Apr 2006 03:00 GMT
In a previous article, Kova <kova1337@REMOVEgmail.com> said:
>Hi. My game has a menu panel with passive rendering (swing) and
>animation panel with active rendering. I want to be able switch between
[quoted text clipped - 3 lines]
>main menu. How is this done with 2 panels?
>I've tried adding both panels to JFrame container and just seting

Put them both in a CardLayout.  That's what it does best.

Signature

Paul Tomblin <ptomblin@xcski.com> http://xcski.com/blogs/pt/
You know the saying, every time you develop an idiot proof system they
develop a better type of idiot... and now you know who "they" are.
             -- Matthew Malthouse

Kova - 26 Apr 2006 13:44 GMT
> In a previous article, Kova <kova1337@REMOVEgmail.com> said:
>
[quoted text clipped - 7 lines]
>
> Put them both in a CardLayout.  That's what it does best.

Thank you, works beautifully :)

--
Kova


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.