我看到它的GridBagLayout位置是细胞中心对齐的孩子.如何左右对齐?
UPDATE
构建代码(我知道我可以重用c)
// button panel
JPanel button_panel = new JPanel();
button_panel.add(ok_button);
button_panel.add(cancel_button);
// placing controls to dialog
GridBagConstraints c;
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
add(inputSource_label, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
add(inputSource_combo, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
add(output_label, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
add(output_combo, c);
c = …Run Code Online (Sandbox Code Playgroud) 我在swing JPanel中显示一个键值对列表.密钥显示在JLabel中,值显示在JTextField中.面板上有足够的空间来显示1或2列键值对,具体取决于父JFrame的大小.除非面板太小,否则我想显示2列键值对.然后我想切换到一列.这可以在Swing中编写我自己的自定义布局管理器吗?
将每个键值对放在它自己的面板上并将面板添加到Flow Layout会做我想做的事情,除了标签不会彼此对齐并且文本字段不会彼此对齐因此看起来很糟糕.有一个更好的方法吗?
编辑:
这是它的样子.如果面板足够大,则显示两列.否则显示一列.
2列:
Some Key _______________ Key 2 ________________
Another Key _______________ Yet Another Key ________________
Key 5 _______________ Key 6 ________________
Run Code Online (Sandbox Code Playgroud)
1列
Some Key _______________
Key 2 _______________
Another Key _______________
Yet Another Key _______________
Key 5 _______________
Key 6 ________________
Run Code Online (Sandbox Code Playgroud) 在 Java 中,当使用 BorderLayout 时,是否可以在 CENTER 中有两个面板,但两者都在表单上可见。
这是我的代码:
guiFrame.add(guiFieldsPanel, BorderLayout.CENTER);
guiFrame.add(guiButtonsPanel, BorderLayout.CENTER);
guiFrame.setVisible(true);
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,两个面板都设置为中心,但我只能看到 guiButtonsPanel,因为它位于 guiFieldsPanel 的“顶部”。
我可以将两个面板组合在一起,然后将它们设置为显示在 CENTER 中吗?