Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在GridBagLayout中放置组件之间的空间_Java_Swing_Layout Manager_Gridbaglayout - Fatal编程技术网

Java 在GridBagLayout中放置组件之间的空间

Java 在GridBagLayout中放置组件之间的空间,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我有一个JPanel,它包含两个JToggleButtons。该面板有一个GridBagLayout,有1行2列。按钮位于同一行,但列不同 class UserInterfacePanel extends JPanel { private JToggleButton startButton; private JToggleButton stopButton; public UserInterfacePanel() {

我有一个
JPanel
,它包含两个
JToggleButtons
。该面板有一个
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);
        }
}

我希望能够控制按钮之间的间距,但changin
c.gridwidth
c.weightx
无法给出结果。将
c.wightx
设置为0.0会导致按钮彼此太近,而非零则会导致按钮太远,且
c.wightx=0.9
c.wightx=0.1
之间的距离没有差异。我做错了什么?

尝试创建一个
Insets
变量,每次单击按钮时
int
s都会增加。代码如下:

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); 

        //...
    }

1) 它可能是,或者需要设置。2) 为了更快地获得更好的帮助,请发布一个or。3) 以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则具有更大的宽度和高度。@Andrew Thompson,我编辑了这篇文章,现在应该可以运行了。