java - Java GUI为什么BoxLayout面板在其中有另一个BoxLayout面板时表现得很奇怪
问题描述
我试图在彼此内部做某种嵌套面板。我是Java新手,所以请耐心等待。
我面临的问题是pnlLogin
行为怪异。它仅在其中显示所有 3 个面板,然后它们没有布局集。每当我添加布局时,它们都不会出现。
嵌套面板应如下所示:
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, 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, 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();
}
}
也许您会发现相对布局更易于使用。它允许您为每个组件分配相对大小。
推荐阅读
- php - 未处理的异常:FormatException:意外字符(在字符 1 处)颤振
- c# - (C#) 从 TextBox WinForms 的删除字符中获取索引
- laravel - Laravel 8:将工厂属性传递给子关系
- node.js - 共享主机上的 500 内部服务器错误
- python - Pygame - 生成随机对象错误
- html - 如何将一个数据提供给 HTML 表格中的一整列?
- reactjs - 反应组件中图像 src 的意外变化
- list - C堆栈溢出HASKELL,同时处理字符串
- bootstrap-4 - Bootstrap 砌体网格损坏
- azure - 在 aks 或 aci 中部署后如何编辑 Azure ML 条目或评分脚本文件