问题描述
我试图在彼此内部做一些嵌套的面板。我是 Java 新手,请耐心等待。
我面临的问题是 pnlLogin
的行为怪异。它只显示其中的所有 3 个面板,然后它们才没有 Layout 集。每当我添加布局时,它们都不会出现。
嵌套面板应如下所示:
Jframe (GridLayout)
|
|-> pnlLogin (BoxLayout)
|
|-> pnlInput (BoxLayout)
|-> pnlMsg (BoxLayout)
|-> pnlButtons (BoxLayout)
我还有以下图片来演示它应该是什么样子:
这是没有设置布局时的样子:
这是获得 BoxLayout 时的样子
我做错了什么?我该如何解决?
这是我的代码:
import java.awt.*;
import javax.swing.*;
public class AnmeldeFenster {
private JFrame jFrame = new JFrame();
private JPanel pnlLogin = new JPanel();
private JPanel pnlEingabe = new JPanel();
private JPanel pnlMelder = new JPanel();
private JPanel pnlButtons = new JPanel();
public AnmeldeFenster() {
int frameWidth = 500;
int frameHeight = 500;
this.jFrame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
this.jFrame.setSize(frameWidth,frameHeight);
this.jFrame.setLocation(400,400);
this.jFrame.setResizable(true);
this.jFrame.setTitle("Anmeldefenster");
this.jFrame.setLayout(new GridLayout());
Container contMngr = this.jFrame.getContentPane();
// Login Panel (Main)
// this.pnlLogin.setBounds(0,frameWidth,frameHeight);
this.pnlLogin.setLayout(new BoxLayout(this.pnlLogin,BoxLayout.PAGE_AXIS));
contMngr.add(this.pnlLogin);
// Eingabe Panel
this.pnlEingabe.setBackground(Color.BLACK);
this.pnlEingabe.setLayout(new BoxLayout(this.pnlEingabe,BoxLayout.PAGE_AXIS));
this.pnlEingabe.setPreferredSize(new Dimension(frameWidth,300));
this.pnlMelder.setAlignmentX(Component.CENTER_ALIGNMENT);
this.pnlLogin.add(this.pnlEingabe);
// Melder Panel
this.pnlMelder.setBackground(Color.GREEN);
this.pnlMelder.setLayout(new BoxLayout(this.pnlMelder,BoxLayout.PAGE_AXIS));
this.pnlMelder.setPreferredSize(new Dimension(frameWidth,100));
this.pnlMelder.setAlignmentX(Component.CENTER_ALIGNMENT);
this.pnlLogin.add(this.pnlMelder);
// Button's Panel
this.pnlButtons.setBackground(Color.BLUE);
this.pnlButtons.setLayout(new BoxLayout(this.pnlButtons,BoxLayout.PAGE_AXIS));
this.pnlButtons.setPreferredSize(new Dimension(frameWidth,100));
this.pnlButtons.setAlignmentX(Component.CENTER_ALIGNMENT);
this.pnlLogin.add(this.pnlButtons);
this.jFrame.setVisible(true);
}
public static void main(String[] args) {
new AnmeldeFenster();
}
}
ASCII Draw 中的设计
┌─┬───────────────────────────────────────┬─┐
│ ├───────────────────────────────────────┤ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ height 200 │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ └───────────────────────────────────────┘ │ height 500
│ empty place │
│ ┌───────────────────────────────────────┐ │
│ │ height 100 │ │
│ │ │ │
│ └───────────────────────────────────────┘ │
│ empty place │
│ ┌───────────────────────────────────────┐ │
│ │ height 100 │ │
│ │ │ │
│ ├───────────────────────────────────────┤ │
└─┴───────────────────────────────────────┴─┘
width 400
解决方法
不确定发生了什么,但问题似乎是您没有向子面板添加任何子组件。
不知何故,为面板的首选尺寸保留了空间,但没有绘制任何东西。
我修改了下面的代码以从最后两个面板中删除 BoxLayout。在第一个面板中,我添加了一个虚拟标签:
import java.awt.*;
import javax.swing.*;
public class BL {
private JFrame jFrame = new JFrame();
private JPanel pnlLogin = new JPanel();
private JPanel pnlEingabe = new JPanel();
private JPanel pnlMelder = new JPanel();
private JPanel pnlButtons = new JPanel();
public BL() {
int frameWidth = 500;
int frameHeight = 500;
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(frameWidth,frameHeight);
jFrame.setLocation(400,400);
jFrame.setResizable(true);
jFrame.setTitle("Anmeldefenster");
jFrame.setLayout(new GridLayout());
Container contMngr = jFrame.getContentPane();
// Login Panel (Main)
// pnlLogin.setBounds(0,frameWidth,frameHeight);
pnlLogin.setLayout(new BoxLayout(pnlLogin,BoxLayout.PAGE_AXIS));
contMngr.add(pnlLogin);
// Eingabe Panel
pnlEingabe.setBackground(Color.YELLOW);
pnlEingabe.setLayout(new BoxLayout(pnlEingabe,BoxLayout.PAGE_AXIS));
pnlEingabe.setPreferredSize(new Dimension(frameWidth,300));
pnlMelder.setAlignmentX(Component.CENTER_ALIGNMENT);
pnlLogin.add(pnlEingabe);
pnlEingabe.add( new JLabel( "testing" ) );
// Melder Panel
pnlMelder.setBackground(Color.GREEN);
// pnlMelder.setLayout(new BoxLayout(pnlMelder,BoxLayout.PAGE_AXIS));
pnlMelder.setPreferredSize(new Dimension(frameWidth,100));
pnlMelder.setAlignmentX(Component.CENTER_ALIGNMENT);
pnlLogin.add(pnlMelder);
// Button's Panel
pnlButtons.setBackground(Color.BLUE);
// pnlButtons.setLayout(new BoxLayout(pnlButtons,BoxLayout.PAGE_AXIS));
pnlButtons.setPreferredSize(new Dimension(frameWidth,100));
pnlButtons.setAlignmentX(Component.CENTER_ALIGNMENT);
pnlLogin.add(pnlButtons);
jFrame.setVisible(true);
}
public static void main(String[] args) {
new BL();
}
}
也许您会发现 Relative Layout 更易于使用。它允许您为每个组件分配相对大小。