【发布时间】:2021-08-15 20:45:52
【问题描述】:
我试图在彼此内部做某种嵌套面板。我是 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
【问题讨论】:
-
1) 以最小尺寸提供 ASCII 艺术或 GUI 的 预期 布局的简单绘图,如果可调整大小,则具有更大的宽度和高度 - 以显示额外的应该使用空间。 2)见Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是的。)
-
@AndrewThompson 检查更新
-
height 100不是好兆头。我可以看到像JTextArea这样的 GUI 组件可能需要“4 行高和 50 个字符宽”,但这并不能很好地转换为“像素”。设置列数和行数,如果需要设置一个Font(面和大小),然后让Swing工具包整理大小!
标签: java swing awt layout-manager boxlayout