Results 1 to 3 of 3
  1. #1
    Beastly is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default [SOLVED]Switching JPanels with a button inside one of the JPanels

    Hi everyone I,m new to this forum.
    Iv'e been having a really frustrating problem with swing.

    I have a program were i have a Main class that is a JFrame and some other classes that extend JPanel.

    On one panel is a button that when clicked should go to the Main class and the Main class should remove the frame and add the other.

    I'm using a null layout because I want complete control over everything.

    The problem is scopes and non-static to static methods and such.

    Here is the code for all three classes (one JFrame and two JPanel)
    I just need a way to switch panels, as you can see I haven't created an instance on Main because I don't know were I should so that it is accessible.

    Thanks in advance if anyone can help me.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Main extends javax.swing.JFrame
    {            
        public Main()
        {
            super("Game");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(1000, 650);
            setVisible(true);
            setLocation(200,50);
            setResizable(false);  
        }
        
        public static void main(String[] args)
        {
            
        }    
    }
    
    
    
    
    
    import javax.swing.*;
    import java.awt.Color;
    
    public class JoinPanel extends javax.swing.JPanel
    {
        JLabel titleLabel = new JLabel("Game");
        JLabel nameLabel = new JLabel("Name");
        JLabel ipLabel = new JLabel("IP");
        JButton joinButton = new JButton("Join");
        JTextField nameText = new JTextField("");
        JTextField ipText = new JTextField("");
        
        public JoinPanel()
        {
            setBackground(new java.awt.Color(0,0,0));
            setSize(1000, 650);
            setVisible(true);
            setLocation(0, 0);
            initComponents();
            setLayout(null);
        }
        
        public void initComponents()
        {
            titleLabel.setBackground(Color.BLACK);
            titleLabel.setForeground(Color.RED);
            titleLabel.setFont(new java.awt.Font("Arial Black", 1, 100));
            titleLabel.setLocation(330,0);
            titleLabel.setSize(400,200);
            titleLabel.setVisible(true);
            add(titleLabel);
            
            nameLabel.setBackground(Color.BLACK);
            nameLabel.setForeground(Color.WHITE);
            nameLabel.setFont(new java.awt.Font("Arial Black", 1, 36));
            nameLabel.setLocation(250,250);
            nameLabel.setSize(150,50);
            nameLabel.setVisible(true);
            add(nameLabel);
            
            ipLabel.setBackground(Color.BLACK);
            ipLabel.setForeground(Color.WHITE);
            ipLabel.setFont(new java.awt.Font("Arial Black", 1, 36));
            ipLabel.setLocation(250,350);
            ipLabel.setSize(150,50);
            ipLabel.setVisible(true);
            add(ipLabel);
            
            nameText.setBackground(Color.BLACK);
            nameText.setForeground(Color.WHITE);
            nameText.setFont(new java.awt.Font("Arial Black", 1, 20));
            nameText.setLocation(400,250);
            nameText.setSize(300,50);
            nameText.setVisible(true);
            add(nameText);
            
            ipText.setBackground(Color.BLACK);
            ipText.setForeground(Color.WHITE);
            ipText.setFont(new java.awt.Font("Arial Black", 1, 20));
            ipText.setLocation(400,350);
            ipText.setSize(300,50);
            ipText.setVisible(true);
            add(ipText);
            
            joinButton.setBackground(Color.BLACK);
            joinButton.setForeground(Color.WHITE);
            joinButton.setFont(new java.awt.Font("Arial Black", 1, 36));
            joinButton.setLocation(400,450);
            joinButton.setSize(150,50);
            joinButton.setVisible(true);
            joinButton.addActionListener(new java.awt.event.ActionListener()
                {
                    public void actionPerformed(java.awt.event.ActionEvent event)
                    {
                        joinButtonPressed();
                    }
                });
            add(joinButton);
        }
        
        public void joinButtonPressed()
        {       
           
        }
    }
    
    
    
    
    
    
    
    import java.awt.Color;
    import javax.swing.*;
    
    public class LobbyPanel extends javax.swing.JPanel
    {
        JTextArea playerArea = new JTextArea();
        JScrollPane playerScroll = new JScrollPane(playerArea);
        JTextArea infoArea = new JTextArea(); 
        
        
        public LobbyPanel()
        {
            setBackground(new java.awt.Color(0,0,0));
            setSize(1000, 650);
            setVisible(true);
            setLocation(0, 0);
            initComponents();
            setLayout(null);
        }
        
        public void initComponents()
        {
            playerScroll.setBackground(Color.BLACK);
            playerScroll.setForeground(Color.RED);
            playerScroll.setFont(new java.awt.Font("Arial Black", 1, 100));
            playerScroll.setLocation(0,0);
            playerScroll.setSize(500,650);
            playerScroll.setVisible(true);
            add(playerScroll);
        }
    }
    Last edited by Beastly; 04-26-2011 at 03:36 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    CardLayout is your friend here.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Beastly is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    I actually solved this by passing a few parameters around.

    Quote Originally Posted by KevinWorkman View Post
    CardLayout is your friend here.
    Well that doesn't solve the problem because how does the panel access the class which uses the cardlayout methods? I don't want to use cardlayout anyway as I said I want to do everything by myself.

    Thanks anyway though.

Similar Threads

  1. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM
  2. Switching JPanels inside JFrame attempt
    By frenk_castle in forum AWT / Swing
    Replies: 7
    Last Post: 03-31-2010, 08:39 AM
  3. JList and JPanels
    By JetsYanks in forum New To Java
    Replies: 8
    Last Post: 12-25-2009, 02:11 PM
  4. HELP with jPanels
    By maverik_vz in forum AWT / Swing
    Replies: 1
    Last Post: 03-12-2009, 11:46 AM
  5. Help with overlapping JPanels
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 03-09-2009, 10:19 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
  •