java - 开始时无法移动组件

标签 java layout gridbaglayout

我做了一个JPane p,把它放在“EAST”一侧。然后我将 GridBagLayout() 设置为我的 Pane 布局。 当我添加按钮时,它们位于中心,但我想在左上角看到它们。

这就是我添加按钮的方式。

GridBagConstraints gbc = new GridBagConstraints();
this.b1 = new JButton("Button 1");
gbc.gridx = 0;
gbc.gridy = 0;
p1.add(b1, gbc);

enter image description here

最佳答案

尝试使用 GridBagConstraints 例如:

pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

button = new JButton("Button 1");

if (shouldWeightX) {
    c.weightx = 0.5;
}

c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);

关于java - 开始时无法移动组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34994016/

相关文章:

java - 在两个数据中心模式下测试 Consul

java - 如果更新了可绘制文件夹,共享首选项无法正确保存可绘制资源 ID

java.lang.NoClassDefFoundError : com/sun/jersey/spi/inject/Errors$Closure cause?

android - AlertDialog EditText 不显示软键盘

java - 在 GridBagLayout 中设置 columnWidth 和 rowHeight

java - GridBagLayout 对齐文本字段吗?

java - GridBagLayout 未将按钮锚定到 PAGE_END

java - 通过序列化在应用程序之间传输 Java 类

Android:将元素放在另一个元素下方并在屏幕底部对齐

python - 为什么这个小部件位于菜单栏的顶部?