对齐所有 gridbaglayout 元素
我想用 Java 用 Swing 制作一个类似 Firefox 的选项对话框。
我现在尝试将所有元素对齐窗口的北面。我用第一个元素制作了它,但下一个元素垂直填充房间并且未对齐。
为此,我使用 GridBagLayout。
这是代码:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class Background {
private Border headBorder = BorderFactory.createEmptyBorder(15, 8, 10, 8);
private GridBagConstraints headPaneConstrain;
private JPanel headPane;
public Background() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame dialog = new JFrame();
//headPane
dialog.add(headPane());
//2 elements
createSetting("test");
createSetting("test2");
//dialog settings
dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dialog.setSize(500,500);
dialog.setVisible(true);
}
});
}
private JPanel createSetting(String name) {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(name));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
headPane.add(panel,headPaneConstrain);
return panel;
}
private JPanel headPane(){
//Constrain
headPaneConstrain = new GridBagConstraints();
headPaneConstrain.fill = GridBagConstraints.HORIZONTAL;
headPaneConstrain.anchor = GridBagConstraints.NORTH;
headPaneConstrain.weightx = 1.0;
headPaneConstrain.weighty = 1.0;
headPaneConstrain.gridx=0;
//HeadPane
headPane = new JPanel();
headPane.setBorder(headBorder);
headPane.setLayout(new GridBagLayout());
return headPane;
}
public static void main(String[] args) {
new Background();
}
}
感谢帮助
I want to make a Firefox like Option Dialog in Java with Swing.
I tried now to align all my elements on the north of the Window. I made it with the first element, but the next Elements are filling the room vertically and are not aligned.
For this i'm using the GridBagLayout.
Here is the code:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class Background {
private Border headBorder = BorderFactory.createEmptyBorder(15, 8, 10, 8);
private GridBagConstraints headPaneConstrain;
private JPanel headPane;
public Background() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame dialog = new JFrame();
//headPane
dialog.add(headPane());
//2 elements
createSetting("test");
createSetting("test2");
//dialog settings
dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dialog.setSize(500,500);
dialog.setVisible(true);
}
});
}
private JPanel createSetting(String name) {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(name));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
headPane.add(panel,headPaneConstrain);
return panel;
}
private JPanel headPane(){
//Constrain
headPaneConstrain = new GridBagConstraints();
headPaneConstrain.fill = GridBagConstraints.HORIZONTAL;
headPaneConstrain.anchor = GridBagConstraints.NORTH;
headPaneConstrain.weightx = 1.0;
headPaneConstrain.weighty = 1.0;
headPaneConstrain.gridx=0;
//HeadPane
headPane = new JPanel();
headPane.setBorder(headBorder);
headPane.setLayout(new GridBagLayout());
return headPane;
}
public static void main(String[] args) {
new Background();
}
}
Thanks for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个可以帮助您
this one can help you with that
如果您不希望“内部”面板垂直增长,那么为什么要将
weighthy
设置为 1.0?您应该将其设置为 0.0(至少对于所有不应垂直增长的面板而言)。此外,请注意使用边框会对两个面板之间的组件对齐产生影响。
在表单中显示不同“部分”的一种更好方法是对整个表单仅使用一个面板,并使用
Jlabel
后跟水平JSeparator
分隔各个部分。对于这种形式,我会使用
DesignGridLayout
(而不是GridBagLayout
,它太像PITA了,无法得到你想要的东西......),但我有偏见这里。If you don't want your " inner" panels to grow vertically, then why do you set
weigthy
to 1.0? You should set it to 0.0 instead (at least for all panels that should not grow vertically).Besides, please pay attention that using borders will have an impact on alignments of components between 2 panels.
A better way to show different "sections" in a form is to use only one panel for the whole form and separate sections with a
Jlabel
followed by a horizontalJSeparator
.For this kind of form, I would use
DesignGridLayout
(rather thanGridBagLayout
which is too much of a PITA to get what you want...), but I'm biased here.