JComboBox没有出现在GridBagLayout中

时间:2015-12-15 15:08:32

标签: java swing jcombobox gridbaglayout

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

的组件外,所有内容都显示为我想要的内容

我尝试在最后添加JLabel并将其.weightx属性设置为.REMAINDER,但即使该标签也未显示。

我尝试更改.gridwidth属性但无效。

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

我对GridBaGLayout()还有点新意,我仍然无法弄清楚我的问题, 我尝试在线查找其他代码和答案,但我找不到我的错误! 你可以帮帮我吗?

P.S。我只是为你提供了能够测试的main方法,这是从另一个类调用的,所以它可能有一些启动错误(希望不是);

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)
    {}
}

3 个答案:

答案 0 :(得分:1)

经过多次尝试,我设法解决了这个问题。

我删除了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;

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

Screenshot example

代码:

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