简体   繁体   中英

Accesing a JPanel variable from another class in Java Swing

I have two classes, the JFrame class and the class for an individual panel (I will be using CardLayout). My panel class extends my JFrame class, but I want to be able to access my panel in the JFrame class so that I can add it to the JFrame from there. (I will do this from a different CardLayout class later, but for now I am doing it from the JFrame class).

Here is the panel class:

import java.awt.Font;

import javax.swing.*;
import javax.swing.border.Border;

public class ECLoginPanel extends ECFrame {
    public JPanel LoginPanel;
    public JButton login;
    public JButton signup;
    
    public ECLoginPanel() {
        Color darkblue = new Color(24, 40, 85);
        Color lightblue = new Color(78, 159, 206);
        Border emptyBorder = BorderFactory.createEmptyBorder();
        
        login = new JButton("Login");
        signup = new JButton("Signup");
        
        LoginPanel.setBackground(darkblue);
        LoginPanel.add(login);
        LoginPanel.add(signup);
        
        login.setFont(new Font("HelveticaNeue", Font.BOLD, 20)); 
        login.setBackground(lightblue);
        login.setFocusPainted(false);
        login.setBorder(emptyBorder);
        
        signup.setFont(new Font("HelveticaNeue", Font.BOLD, 20));
        signup.setBackground(lightblue);    
        signup.setFocusPainted(false);
        signup.setBorder(emptyBorder);
    }
}

And here is the frame class:

import javax.swing.*;

public class ECFrame {
    JFrame frame;
    
    public ECFrame() {
        frame = new JFrame("EasyChat v0.01");
        frame.setSize(800,450);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);
    }
}

It seems like you need to add LoginPanel to frame .

Your ECLoginPanel already has a frame (because it inherits it from ECFrame ).

So you just need to call frame.add(LoginPanel); (assuming both your classes are on the same package) as the last line of ECLoginPanel 's constructor.

But you are setting your frame to visible in the constructor of ECFrame (which is called before ECLoginPanel 's constructor), so you should also revalidate and repaint the frame after adding the LoginPanel .

So replace the code of ECLoginPanel to:

import java.awt.Font;

import javax.swing.*;
import javax.swing.border.Border;

public class ECLoginPanel extends ECFrame {
    public JPanel LoginPanel;
    public JButton login;
    public JButton signup;
    
    public ECLoginPanel() {
        Color darkblue = new Color(24, 40, 85);
        Color lightblue = new Color(78, 159, 206);
        Border emptyBorder = BorderFactory.createEmptyBorder();
        
        login = new JButton("Login");
        signup = new JButton("Signup");
        
        LoginPanel.setBackground(darkblue);
        LoginPanel.add(login);
        LoginPanel.add(signup);
        
        login.setFont(new Font("HelveticaNeue", Font.BOLD, 20)); 
        login.setBackground(lightblue);
        login.setFocusPainted(false);
        login.setBorder(emptyBorder);
        
        signup.setFont(new Font("HelveticaNeue", Font.BOLD, 20));
        signup.setBackground(lightblue);    
        signup.setFocusPainted(false);
        signup.setBorder(emptyBorder);

        //Added the following 3 lines...
        frame.add(LoginPanel);
        frame.revalidate();
        frame.repaint();
    }
}

... and let us know if it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM