【问题标题】:Adding blank space using GridBagLayout/GridBagConstraints使用 GridBagLayout/GridBagConstraints 添加空格
【发布时间】:2015-11-19 16:59:21
【问题描述】:

我正在尝试在组件之间添加空白(清晰)间距。如果你运行我的代码,你会看到它们都连接在一起。我试过使用insets,但似乎没有用。我想在x1InputdpLabel 之间添加空格,使它们显示为好像它们没有连接一样。也许我错过了一些简单的东西,但它并没有为我工作。有什么建议吗?

我的代码如下:

package problemgbc;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ProblemGBC extends JFrame implements ActionListener {

    JLabel x1Label = new JLabel("x1:");
    JTextField x1Input = new JTextField(3);
    JLabel x2Label = new JLabel("x2:");
    JTextField x2Input = new JTextField(3);
    JLabel xLabel = new JLabel("x:");
    JTextField xInput = new JTextField(3);
    JLabel dpLabel = new JLabel("Decimal Value:");
    JComboBox decimalPlace = new JComboBox();

public static void main(String[] args) {
    try {
        //Sets the GUI look and feel to the systems default GUI
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException |
            IllegalAccessException | UnsupportedLookAndFeelException e) {
    }
    ProblemGBC problemGUI = new ProblemGBC();
}

public ProblemGBC() {
    setLayout(new BorderLayout());
    setSize(650, 150);
    setResizable(false);
    setLocationRelativeTo(null);
    setTitle("Problem");
    ImageIcon iconImg = new ImageIcon("src/resources/icon.png");
    setIconImage(iconImg.getImage());
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(3, 3, 3, 3);
    gbc.weighty = 0;
    gbc.gridx = 0;
    gbc.gridy = 0;

    gbc.anchor = GridBagConstraints.LINE_END;

    JPanel top = new JPanel(new GridBagLayout());
    gbc.gridx = 0;
    gbc.gridy = 0;
    top.add(xLabel, gbc);
    gbc.gridx = 1;
    gbc.gridy = 0;
    top.add(xInput, gbc);
    gbc.gridx = 2;
    gbc.gridy = 0;
    top.add(x1Label, gbc);
    gbc.gridx = 3;
    gbc.gridy = 0;
    top.add(x1Input, gbc);
    gbc.gridx = 2;
    gbc.gridy = 1;
    top.add(x2Label, gbc);
    gbc.gridx = 3;
    gbc.gridy = 1;
    top.add(x2Input, gbc);
    gbc.gridx = 4;
    gbc.gridy = 0;
    top.add(dpLabel);
    gbc.gridx = 5;
    gbc.gridy = 0;
    decimalPlace.addItem("Full Answer");
    decimalPlace.addItem("1");
    decimalPlace.addItem("2");
    decimalPlace.addItem("3");
    decimalPlace.addItem("4");
    decimalPlace.addItem("5");
    decimalPlace.addItem("6");
    decimalPlace.addItem("7");
    decimalPlace.addItem("8");
    decimalPlace.addItem("9");
    ((JLabel) decimalPlace.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);
    top.add(decimalPlace, gbc);
    add("North", top);

    setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

}

【问题讨论】:

  • 您可以创建一个没有任何内容的标签,然后将其设置为一定的宽度并将其放在两个元素之间...
  • 不要创建一个不需要的hack标签,这样会弄乱面板中的网格。

标签: java jpanel gridbaglayout


【解决方案1】:

我建议您从 How to Use GridBagLayout 上的 Swing 教程中的演示代码开始。使用演示代码,您将拥有一个遵循 Swing 指南的结构更好的程序。例如:

  1. 应在事件调度线程上创建 GUI,
  2. 您应该先打包() 框架,然后再使其可见。
  3. 您不应该使用:add("North", top)。那就是不要使用“魔术”值,这不是指定约束的顺序。约束应该最后指定,就像您对 GridBagLayout 所做的那样。

我尝试过使用插图,但似乎没有用。

是什么让您认为它不起作用? 3像素是一个小差距。你试过更大的差距吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    相关资源
    最近更新 更多