Java GUI为什么BoxLayout面板中没有另一个BoxLayout面板时行为奇怪
我试着做一种相互嵌套的面板。我是Java新手,请耐心等待 我所面临的问题是扮演维德的角色。它显示了它里面的所有3个面板,但是它们没有布局集。每当我添加布局时,它们都不会出现 嵌套面板应如下所示:Java GUI为什么BoxLayout面板中没有另一个BoxLayout面板时行为奇怪,java,swing,awt,layout-manager,boxlayout,Java,Swing,Awt,Layout Manager,Boxlayout,我试着做一种相互嵌套的面板。我是Java新手,请耐心等待 我所面临的问题是扮演维德的角色。它显示了它里面的所有3个面板,但是它们没有布局集。每当我添加布局时,它们都不会出现 嵌套面板应如下所示: Jframe (GridLayout) | |-> pnlLogin (BoxLayout) | |-> pnlInput (BoxLayout) |-> pnlMsg (BoxLayout) |-> pnlBu
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绘图的设计
┌─┬───────────────────────────────────────┬─┐
│ ├───────────────────────────────────────┤ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ 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();
}
}
也许你会发现更容易相处。它允许您为每个组件分配相对大小。1)以最小大小提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则具有更大的宽度和高度,以显示应如何使用额外的空间。2) 请参阅(是)。@andrewhompson检查更新
height 100
不是好兆头。我可以看出像JTextArea这样的GUI组件可能需要“4行高,50个字符宽”,但这不能很好地转化为“像素”。设置列数和行数,如果需要,设置字体(字体和大小),然后让Swing工具包整理大小!