JComboBox 没有出现在 GridBagLayout 中

JComboBox not appearing in GridBagLayout

我在显示最后一个 JComboBox 时遇到问题,我正在使用 GridBaGlayout 除了名称为 cat1、cat2 和 cat3

的最后一个组件外,一切都如我所愿

我尝试在末尾添加一个 JLabel 并将其 .weightx 属性 设置为 .REMAINDER 但即使是该标签也不会显示。

我也尝试更改 .gridwidth 属性 但无济于事。

我也尝试添加其他组合框,但即使它们出现在第一次出现时它们仍然没有出现,所以我猜这是某种列计数错误,但我没有设置任何那个甚至不知道那是怎么设置的。

我对 GridBaGLayout() 还是有点陌生​​,我仍然无法弄清楚我的问题, 我试着在网上查找其他代码和答案,但我就是找不到我的错误! 你能帮帮我吗?

P.S。我只是把 main 方法放进去让你能够测试,这是从另一个 class 调用的,所以它可能有一些启动错误(希望没有);

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class selectioins implements ActionListener 
{
    JFrame f;
    JPanel p;
    JButton searchB, viewB, newB;
    JTextField search1F,search2F,search3F;
    JComboBox logic1,logic2,logic3, cart1,cart2,cart3;

    String[] carterias = {"First name:","Last name"};
    String[] logic = {"Disabled","AND","OR","NOT"};

    char Type = 'c';
    public static void main(String[] args)
    {
      new selectioins('c');
    }
    public selectioins(char type) 
    {
        //##### Initialization #####
        String head = "";
        if(type=='c'){ head = "Customer"; Type = 'c';}
        if(type=='e'){ head = "Employee"; Type = 'e';}
        f = new JFrame(head +" Search");
        p = new JPanel();

        search1F = new JTextField();
        search2F = new JTextField();
        search3F = new JTextField();

        searchB = new JButton("Search");
        newB = new JButton("New entry");

        logic1 = new JComboBox(logic);
        logic2 = new JComboBox(logic);
        logic3 = new JComboBox(logic);
        cart1 = new JComboBox(carterias);
        cart2 = new JComboBox(carterias);
        cart3 = new JComboBox(carterias);

        searchB.addActionListener(this);
        newB.addActionListener(this);

        //##### layout settings #####
        p.setLayout(new GridBagLayout());

        GridBagConstraints gbc4f = new GridBagConstraints();
        gbc4f.fill = GridBagConstraints.BOTH;
        gbc4f.anchor = GridBagConstraints.NORTHWEST;
        gbc4f.gridwidth = GridBagConstraints.REMAINDER;
        gbc4f.weightx = 0.75;
        gbc4f.insets = new Insets(1, 1, 1, 1);

        GridBagConstraints gbc4l =  (GridBagConstraints) gbc4f.clone();
        gbc4l.weightx = 0.1;
        gbc4l.gridwidth = 1;


        GridBagConstraints gbc4cmb = (GridBagConstraints) gbc4f.clone(); //ComboBox 
        gbc4cmb.anchor = GridBagConstraints.NORTHWEST;
        gbc4cmb.insets = new Insets(1, 1, 1, 1);
        gbc4cmb.gridwidth = GridBagConstraints.RELATIVE;


        //##### adding components #####
        p.add(new JLabel("Search for:"),gbc4l); p.add(search1F,gbc4cmb);
        p.add(new JLabel("in:"),gbc4l);         p.add(cart2,gbc4cmb);
        p.add(new JLabel(" 1"),gbc4f);          p.add(logic1,gbc4f);

        p.add(new JLabel("That has:"),gbc4l);   p.add(search2F,gbc4cmb);
        p.add(new JLabel("in:"),gbc4l);         p.add(cart2,gbc4cmb);
        p.add(new JLabel(" 11 "),gbc4f);            p.add(logic2,gbc4f);

        p.add(new JLabel("Also has:"),gbc4l);   p.add(search3F,gbc4cmb);
        p.add(new JLabel("in:"),gbc4l);         p.add(cart3,gbc4cmb);
        p.add(new JLabel("3 "),gbc4f);

        p.add(searchB,gbc4cmb);
        p.add(newB,gbc4cmb);


        //##### Containers finalizing #####
        f.add(p,BorderLayout.NORTH);
        //f.setResizable(false);
        f.setBounds(600,400,400,300);
        f.setVisible(true);
    }




    public void actionPerformed(ActionEvent e)
    {}
}

您不太了解 GridBagLayout 的工作原理。它需要对每个组件进行约束。这意味着每个组件都有不同的约束。在您的示例中,您使用相同的 GridBagConstraint 来添加不同的组件(例如,gbc4cmb 使用了大约 6 次!!)。

一般情况下,不应多次使用约束,否则组件会相互重叠(因为它们具有相同的 x,y)。

您还可以考虑在 GUI 中为每一行使用 BoxLayout 和面板,其中每个面板使用 FlowLayout。截图:

代码:

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

public class SelectionsAlternative {
    private static final String[] CRITERIA = {"First name", "Last name"};
    private static final String[] OPERATORS = {"Disabled", "AND", "OR", "NOT"};

    public static void main(String[] arguments) {
        SwingUtilities.invokeLater(() -> new SelectionsAlternative().createAndShowGui());
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame("Stack Overflow");
        int width = 800;
        int height = 600;
        frame.setBounds(100, 100, width, height);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));

        addCriterion(mainPanel);
        addOperator(mainPanel);
        addCriterion(mainPanel);
        addOperator(mainPanel);
        addCriterion(mainPanel);

        Dimension minimumSize = new Dimension(width, 10);
        Dimension preferredSize = new Dimension(width, height);
        mainPanel.add(new Box.Filler(minimumSize, preferredSize, preferredSize));

        frame.getContentPane().add(mainPanel);
        frame.setVisible(true);
    }

    private void addCriterion(JPanel mainPanel) {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        panel.add(new JLabel("Search for:"));
        panel.add(new JTextField(28));
        panel.add(new JLabel("in:"));
        panel.add(new JComboBox<>(CRITERIA));
        mainPanel.add(panel);
    }

    private void addOperator(JPanel mainPanel) {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        panel.add(new JComboBox<>(OPERATORS));
        mainPanel.add(panel);
    }
}

经过多次尝试,我设法修复了它。

我删除了 GridBagLayout.RELATIVE 属性并在 gbc4cmb 中将其更改为 1 并且有效!

GridBagConstraints gbc4cmb = (GridBagConstraints) gbc4f.clone(); //ComboBox 
        gbc4cmb.anchor = GridBagConstraints.NORTHWEST;
        gbc4cmb.insets = new Insets(1, 1, 1, 1);
        gbc4cmb.gridwidth = 1;