我有一个包含两个JToggleButtons
的JPanel
。该面板具有GridBagLayout
,有1行和2列。按钮位于同一行,但不同列。
class UserInterfacePanel extends JPanel {
private JToggleButton startButton;
private JToggleButton stopButton;
public UserInterfacePanel() {
setup();
}
private void setup() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
setupButtons();
//setupButtonsActions();
c.insets=new Insets(3,3,3,3);
c.weightx=0.5; //c.weightx=0.0;
c.weighty=0.5; //c.weighty=0.5;
c.gridx=0;
c.gridy=0;
add(startButton, c);
c.gridx=1;
c.gridy=0;
add(stopButton, c);
}
private void setupButtons() {
startButton=new JToggleButton(iconStartButton);
stopButton=new JToggleButton(iconStopButton);
}
public class UserInterface extends JFrame {
public static void main(String[] args) {
run();
}
public UserInterface() {
setup();
}
private void setup() {
width=800;
height=600;
panel=new UserInterfacePanel();
getContentPane().add(panel);
setSize(width, height);
}
public static void run() {
UserInterface gui=new UserInterface();
gui.setVisible(true);
}
}
我想要控制按钮之间的间距,但更改 c.gridwidth 或
c.weightx
并没有给我结果。将c.weightx
设置为0.0会导致按钮之间太近,而任何非零值则会导致它们之间太远,c.widthx = 0.9
和c.widthx = 0.1
之间的距离没有区别。我做错了什么?
GridBagConstraints.insets
、ipadx
或ipady
。