在GridBagLayout中给组件之间添加间距

3

我有一个包含两个JToggleButtonsJPanel。该面板具有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.9c.widthx = 0.1之间的距离没有区别。我做错了什么?

2
  1. 可能需要设置 GridBagConstraints.insetsipadxipady
  2. 为了更快地获得帮助,请发布一个 [MCVE] 或 Short, Self Contained, Correct Example
  3. 提供 ASCII 艺术或 GUI 的简单绘图,至少包括最小尺寸,并且如果可调整大小,则包括更多的宽度和高度。
- Andrew Thompson
@Andrew Thompson,我编辑了帖子,现在应该可以运行了。 - parsecer
1个回答

7

尝试创建一个Insets变量,其中包含每次单击按钮时增加的int。以下是代码:

public class UserInterfacePanel extends JPanel {
    private JToggleButton startButton;
    private JToggleButton stopButton;
    private int top = 3, left = 3, bottom = 3, right = 3;
    private Insets i = new Insets(top, left, bottom, right);

    public UserInterfacePanel()  {
        setup();
    }

    private void setup()  {
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        setupButtons();
        setupButtonsActions();

        c.insets = i;
        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);

        JButton b1 = new JButton("+1");
        b1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                i = new Insets(top+1, left+1, bottom+1, right+1);
                c.insets = i;
                repaint();
            }
        });
        add(b1, BorderLayout.SOUTH); 

        //...
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接