我做了一个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);
最佳答案
尝试使用 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/