简体   繁体   English

从 Java Swing 中的另一个 class 访问 JPanel 变量

[英]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).我有两个类,JFrame class 和 class 用于单个面板(我将使用 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. 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). (稍后我将从不同的 CardLayout class 执行此操作,但现在我从 JFrame 类执行此操作)。

Here is the panel class:这是面板 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:这是框架 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 .似乎您需要将LoginPanel添加到frame

Your ECLoginPanel already has a frame (because it inherits it from ECFrame ).您的ECLoginPanel已经有一个frame (因为它是从ECFrame继承的)。

So you just need to call frame.add(LoginPanel);所以你只需要调用frame.add(LoginPanel); (assuming both your classes are on the same package) as the last line of ECLoginPanel 's constructor. (假设您的两个类都在同一个包中)作为ECLoginPanel的构造函数的最后一行。

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 .但是您将frame设置为在 ECFrame 的构造函数中可见(在ECFrame的构造函数之前ECLoginPanel ),因此您还应该在添加LoginPanel后重新验证并重新绘制框架。

So replace the code of ECLoginPanel to:所以将ECLoginPanel的代码替换为:

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. ...让我们知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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