Java Swing GridBagLayout - 添加没有空格的按钮

时间:2015-06-04 11:11:37

标签: java swing layout-manager gridbaglayout

如何删除gridBagLayout引起的间距并使它们粘在一起?
这是我的代码只需添加3个按钮。 我读了这个问题,但没有完整的解决方案How to fix gap in GridBagLayout;
我只想将所有按钮放在JFrame的顶部。

action = {
  [1] = function (x)
    print(1)
  end,

  [2] = function (x)
    z = 5
  end,

  ["nop"] = function (x)
    print(math.random())
  end,

  ["my name"] = function (x)
    print("fred")
  end,
}

我的按钮如何显示:
enter image description here
我希望按钮看起来如何: enter image description here

3 个答案:

答案 0 :(得分:4)

布局会生成一列按钮,所以..

变化:

        gc.fill = GridBagConstraints.HORIZONTAL;

要:

        gc.fill = GridBagConstraints.BOTH;

编辑1

  

我想让按钮保持与图片中所描述的相同的高度,并将它们放在顶部。

使用组合布局将它们限制在顶部相当容易。在这种情况下,我们可能会将带有按钮的面板添加到PAGE_START面板的BorderLayout中。边框布局的这一部分将拉伸子组件的宽度(我们的面板带有GridLayout)以填充可用空间,但它将尊重高度无论是什么内容 - 只给予组件所需的垂直空间。

编辑2

这是一个实现上述想法的MCVE。外面板(带有青色边框)用于约束按钮面板的高度(带橙色边框)。有关其工作原理的更多详细信息,请参阅源代码中的注释。

enter image description here enter image description here enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class MyProblem {

    private JFrame frame = new JFrame();

    public static void main(String[] args) {
        new MyProblem();
    }

    public MyProblem() {
        frame.setLayout(new BorderLayout()); // actually the default

        // we will use this panel to constrain the panel with buttons
        JPanel ui = new JPanel(new BorderLayout());
        frame.add(ui);
        ui.setBorder(new LineBorder(Color.CYAN, 3));

        // the panel (with GridLayout) for the buttons
        JPanel toolPanel = new JPanel(new GridLayout(0,1,0,4)); // added some gap
        toolPanel.setBorder(new LineBorder(Color.ORANGE, 3));
        // toolPanel will appear at the top of the ui panel
        ui.add(toolPanel, BorderLayout.PAGE_START); 
        for (int i = 0; i < 3; i++) {
            toolPanel.add(new JButton("Button " + i));
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setSize(500, 500); // instead..
        frame.pack(); // pack will make it as small as it can be.
        frame.setMinimumSize(frame.getSize()); // nice tweak..
        frame.setVisible(true);
    }
}

答案 1 :(得分:3)

此代码可以完成这项工作:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MyProblem {

    private JFrame frame = new JFrame();

    public static void main(String[] args) {
        new MyProblem();
    }

    public MyProblem() {
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.weightx = 1;
        gc.weighty = 0;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.ipadx = 0;
        gc.ipady = 0;
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.anchor = GridBagConstraints.NORTH;
        for (int i = 0; i < 3; i++) {
            JButton button = new JButton("Button " + i);
            frame.add(button, gc);
            gc.gridy++;
        }
        gc.weighty = 1;
        frame.add(Box.createGlue(), gc); //Adding a component to feel the area.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

}

答案 2 :(得分:0)

运行此代码我想你会从中获得帮助

import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Think {

public static void addComponentsToPane(Container pane) {

    JButton jbnButton;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gBC = new GridBagConstraints();
    gBC.fill = GridBagConstraints.HORIZONTAL;

    jbnButton = new JButton("Button 1");
    gBC.weightx = 0.5;
    gBC.gridx = 0;
    gBC.gridy = 0;
    pane.add(jbnButton, gBC);

    jbnButton = new JButton("Button 3");
    gBC.gridx = 2;
    gBC.gridy = 0;
    pane.add(jbnButton, gBC);

    jbnButton = new JButton("Button 4");
    gBC.ipady = 40;     //This component has more breadth compared to other buttons
    gBC.weightx = 0.0;
    gBC.gridwidth = 3;
    gBC.gridx = 0;
    gBC.gridy = 1;
    pane.add(jbnButton, gBC);

}

private static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("GridBagLayout Source Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    addComponentsToPane(frame.getContentPane());

    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
 }
}