gpt4 book ai didi

java - JComboBox 未出现在 GridBagLayout 中

转载 作者:行者123 更新时间:2023-12-01 10:44:06 25 4
gpt4 key购买 nike

我在显示最后一个 JComboBox 时遇到问题,我正在使用 GridBaGlayout除了最后一个名为 cat1、cat2 和 cat3 的组件之外,所有内容都按我想要的方式显示

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

我也尝试更改 .gridwidth 属性,但没有成功。

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

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

附注我只是放入 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)
{}
}

最佳答案

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

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

关于java - JComboBox 未出现在 GridBagLayout 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34292632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com