Java 加载包含另一个JPanel的JPanel
我正在尝试加载一个扩展JPanel的类,该类包含来自另一个类的组件,该类也将JPanel扩展到另一个类中。 这就是我需要实现的目标:Java 加载包含另一个JPanel的JPanel,java,swing,jpanel,containers,Java,Swing,Jpanel,Containers,我正在尝试加载一个扩展JPanel的类,该类包含来自另一个类的组件,该类也将JPanel扩展到另一个类中。 这就是我需要实现的目标: First.java的示例 public class First extends JPanel{ JPanel cont = new JPanel(); public First(){ cont.setBackground(Color.YELLOW); } } pu
First.java的示例
public class First extends JPanel{
JPanel cont = new JPanel();
public First(){
cont.setBackground(Color.YELLOW);
}
}
public class Second extends JPanel{
JPanel cont = new JPanel();
First first_panel = new First();
public Second(){
cont.setBackground(Color.RED);
cont.add(first_panel);
}
}
public class Container extends JFrame{
JFrame frame = new JFrame();
JPanel cont = new JPanel();
Second second_panel = new Second();
public Container(){
cont.setBackground(Color.GREEN);
cont.add(second_panel);
frame.add(cont);
frame.setVisible(true);
}
}
Second.java的示例
public class First extends JPanel{
JPanel cont = new JPanel();
public First(){
cont.setBackground(Color.YELLOW);
}
}
public class Second extends JPanel{
JPanel cont = new JPanel();
First first_panel = new First();
public Second(){
cont.setBackground(Color.RED);
cont.add(first_panel);
}
}
public class Container extends JFrame{
JFrame frame = new JFrame();
JPanel cont = new JPanel();
Second second_panel = new Second();
public Container(){
cont.setBackground(Color.GREEN);
cont.add(second_panel);
frame.add(cont);
frame.setVisible(true);
}
}
Container.java的示例
public class First extends JPanel{
JPanel cont = new JPanel();
public First(){
cont.setBackground(Color.YELLOW);
}
}
public class Second extends JPanel{
JPanel cont = new JPanel();
First first_panel = new First();
public Second(){
cont.setBackground(Color.RED);
cont.add(first_panel);
}
}
public class Container extends JFrame{
JFrame frame = new JFrame();
JPanel cont = new JPanel();
Second second_panel = new Second();
public Container(){
cont.setBackground(Color.GREEN);
cont.add(second_panel);
frame.add(cont);
frame.setVisible(true);
}
}
我可以逐个加载类,但当我尝试加载包含另一个面板的面板时,GUI不会显示它。逻辑有什么问题?问题出在哪里?显示的代码中有两个基本问题
每个类都进行了扩展,并且都有它所处理的组件的实例
两个面板都没有任何内容可以赋予它非零大小,也没有覆盖getPreferredSize
方法,因此它们是0x0 px
看看这个MCVE的效果
import java.awt.Color;
import javax.swing.*;
public class Container {
JFrame frame = new JFrame();
JPanel cont = new JPanel();
Second second_panel = new Second();
public Container() {
cont.setBackground(Color.GREEN);
cont.add(second_panel.getPanel());
frame.add(cont);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new Container();
}
};
SwingUtilities.invokeLater(r);
}
}
class Second {
JPanel cont = new JPanel();
First first_panel = new First();
public Second() {
cont.setBackground(Color.RED);
cont.add(new JLabel("Second"));
cont.add(first_panel.getPanel());
}
public JComponent getPanel() {
return cont;
}
}
class First {
JPanel cont = new JPanel();
public First() {
cont.setBackground(Color.YELLOW);
cont.add(new JLabel("First"));
}
public JComponent getPanel() {
return cont;
}
}
为了更快地获得更好的帮助,可以发布一个(最小完整的可验证示例)或(简短、自包含、正确的示例)。