Java 我的所有组件都放置在彼此上方,i';我已经想了好一阵子了

Java 我的所有组件都放置在彼此上方,i';我已经想了好一阵子了,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,有人能告诉我我做错了什么吗?我先试着铺设ot面板,但结果相同,然后我又重新使用组件,结果相同,所有组件都互不重叠,我尝试了各种各样的事情,结果相同,它们互不重叠,中间也互不重叠 public class FnaComponents extends JPanel { public FnaComponents() { gridbag = new GridBagLayout(); setLayout(gridbag); setPreferredSize(new Dimens

有人能告诉我我做错了什么吗?我先试着铺设ot面板,但结果相同,然后我又重新使用组件,结果相同,所有组件都互不重叠,我尝试了各种各样的事情,结果相同,它们互不重叠,中间也互不重叠

public class FnaComponents extends JPanel 
{

public FnaComponents()
{
    gridbag = new GridBagLayout();
    setLayout(gridbag);
    setPreferredSize(new Dimension(600,600));
    //setBackground(Color.lightGray);

    mainPanel = new JPanel(gridbag);
    mainPanel.setPreferredSize(new Dimension(600, 600));

    pTextField = new JTextField();
    //addcomponents(new JLabel("Policy #"), mainPanel, 0, 0, 0, 0, 0, 0, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST);
    //addcomponents(pTextField, mainPanel, null, 2, 0, 2, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);


    Invisible = new JButton();
    //addcomponents(Invisible, mainPanel, 0, 0, 0, 0, 0, 0, GridBagConstraints.NONE, GridBagConstraints.EAST);

    newbTextField = new JTextField();
    newbButton = new JButton("Cal Icon");
    //addcomponents(new JLabel("NB Date:"), mainPanel, 1, 0, 0, 0, 4, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
    //addcomponents(newbTextField,mainPanel, null, 1, 1, 2, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
    //addcomponents(newbButton,mainPanel, 1, 4, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);

    bilimButton = new JComboBox<>(bilimits);
    bilimButton.setEditable(true);
    bicslButton = new JComboBox<>(bicsl);
    bicslButton.setEditable(true);
    //addcomponents(new JLabel("BI Limit:"), mainPanel, 2, 0, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
    //addcomponents(bilimButton, mainPanel, 2, 1, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
    //addcomponents(bicslButton,mainPanel, 2, 2, 0, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);


    lapseButton = new JComboBox<>(lapse);

    //addComponents(new JLabel("Lapse:"), tophalf, 3, 0, 2, 0, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
    //addComponents(lapseButton, lPanel);
    //addcomponents(lapseButton, tophalf, 3, 1, 0, 0, 4, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);


    addcomponents(mainPanel, this, null);
    add(mainPanel, BorderLayout.NORTH);

}
// method to add components to a container 
private void addcomponents(JComponent cont, Container main, Border border)
{
    Color code = new Color(0, 255, 255);

    Border padborder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
    Border bord = BorderFactory.createLineBorder(code, 1, true);
    border = BorderFactory.createCompoundBorder(bord, padborder);

    cont.setBorder(border);

    main.add(cont);  
}

// method to add components to a container 
private void addcomponents(JComponent cont, Container main, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill, int anchor)
{
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx = gridx;
    gbc.gridy = gridy;
    gbc.gridwidth = gridwidth;
    gbc.gridheight = gridheight;
    gbc.weightx = weightx;
    gbc.weighty = weighty;
    gbc.fill = fill;
    gbc.anchor = anchor;
    gbc.insets = new Insets(10, 10, 10, 10);
    gridbag.setConstraints(cont, gbc);
    main.add(cont);
}

// method to add components to a container 
private void addcomponents(JComponent cont, Container main, Border border, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill, int anchor)
{
    GridBagConstraints gbc = new GridBagConstraints();

    Color code = new Color(0, 255, 255);

    Border padborder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
    Border bord = BorderFactory.createLineBorder(code, 1, true); 
    border = BorderFactory.createCompoundBorder(bord, padborder);

    gbc.gridx = gridx;
    gbc.gridy = gridy;
    gbc.gridwidth = gridwidth;
    gbc.gridheight = gridheight;
    gbc.weightx = weightx;
    gbc.weighty = weighty;
    gbc.fill = fill;
    gbc.anchor = anchor;
    gbc.insets = new Insets(10, 10, 10, 10);

    cont.setBorder(border);
    gridbag.setConstraints(cont, gbc);
    main.add(cont);  
}
}
公共类FnaComponents扩展了JPanel
{
公共FnaComponents()
{
gridbag=新的GridBagLayout();
设置布局(网格袋);
设置首选尺寸(新尺寸(600600));
//挫折背景(颜色:浅灰色);
主面板=新JPanel(gridbag);
主面板。设置首选尺寸(新尺寸(600600));
pTextField=新的JTextField();
//addcomponents(新JLabel(“Policy#”)、主面板、0、0、0、0、0、0、GridBagConstraints.NONE、GridBagConstraints.NORTHWEST);
//addcomponents(pTextField,mainPanel,null,2,0,2,0,0,0,GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST);
不可见=新的JButton();
//addcomponents(不可见,主面板,0,0,0,0,0,0,GridBagConstraints.NONE,GridBagConstraints.EAST);
newTextField=新的JTextField();
NewButton=新的JButton(“校准图标”);
//addcomponents(新JLabel(“NB日期:”),主面板,1,0,0,4,0,GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST);
//addcomponents(NewTextField、mainPanel、null、1、1、2、0、0、0、GridBagConstraints.HORIZONTAL、GridBagConstraints.WEST);
//添加组件(新按钮、主面板、1、4、0、0、0、GridBagConstraints.HORIZONTAL、GridBagConstraints.WEST);
bilimButton=新的JComboBox(bilimits);
BILIMBUTON.setEditable(真);
BICSLBUTON=新的JComboBox(bicsl);
bicslButton.setEditable(真);
//addcomponents(新的JLabel(“BI限制”)、主面板、2、0、0、0、0、0、GridBagConstraints.HORIZONTAL、GridBagConstraints.WEST);
//添加组件(Bilibutton,主面板,2,1,0,0,0,GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST);
//添加组件(bicslButton、主面板、2、2、0、0、0、GridBagConstraints.HORIZONTAL、GridBagConstraints.WEST);
失效按钮=新JComboBox(失效);
//addComponents(新的JLabel(“失效:”),上半部分,3,0,2,0,0,0,GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST);
//添加组件(按钮、lPanel);
//添加组件(按钮、上半部分、3、1、0、0、4、0、GridBagConstraints.HORIZONTAL、GridBagConstraints.WEST);
addcomponents(主面板,此,空);
添加(主面板,BorderLayout.NORTH);
}
//方法将组件添加到容器中
私有void addcomponents(JComponent cont、容器主节点、边框)
{
颜色代码=新颜色(0、255、255);
Border padborder=BorderFactory.createEmptyByOrder(10,10,10,10);
Border bord=BorderFactory.createLineBorder(代码,1,true);
border=BorderFactory.createCompoundBorder(bord,padborder);
续订单(边界);
主。添加(续);
}
//方法将组件添加到容器中
私有void addcomponents(JComponent cont、Container main、int gridx、int gridy、int gridwidth、int gridheight、double weights、double weights、int fill、int锚定)
{
GridBagConstraints gbc=新的GridBagConstraints();
gbc.gridx=gridx;
gbc.gridy=gridy;
gbc.gridwidth=gridwidth;
gbc.gridheight=gridheight;
gbc.weightx=weightx;
gbc.weighty=weighty;
gbc.fill=填充;
gbc.anchor=锚;
gbc.插图=新插图(10,10,10,10);
gridbag.setConstraints(续,gbc);
主。添加(续);
}
//方法将组件添加到容器中
私有void addcomponents(JComponent cont、容器main、边框、int-gridx、int-gridy、int-gridwidth、int-gridheight、double-weights、double-weights、int-fill、int-anchor)
{
GridBagConstraints gbc=新的GridBagConstraints();
颜色代码=新颜色(0、255、255);
Border padborder=BorderFactory.createEmptyByOrder(10,10,10,10);
Border bord=BorderFactory.createLineBorder(代码,1,true);
border=BorderFactory.createCompoundBorder(bord,padborder);
gbc.gridx=gridx;
gbc.gridy=gridy;
gbc.gridwidth=gridwidth;
gbc.gridheight=gridheight;
gbc.weightx=weightx;
gbc.weighty=weighty;
gbc.fill=填充;
gbc.anchor=锚;
gbc.插图=新插图(10,10,10,10);
续订单(边界);
gridbag.setConstraints(续,gbc);
主。添加(续);
}
}

代码有点难读

首先,我建议不要在容器之间共享相同的
GridBagLayout
实例

gridbag = new GridBagLayout();
setLayout(gridbag);
//...
mainPanel = new JPanel(gridbag);
这可能会导致问题,相反,为每个容器提供它们自己的
GridBagLayout

这意味着您将无法执行类似于
gridbag.setConstraints(cont,gbc)的操作main.add(cont,gbc)
的方法,它会将约束传递给当前分配的布局管理器

其次,在正常情况下,
gridwidth
gridheight
不应小于
1
,否则,您会遇到问题

所以你应该使用更像

addcomponents(new JLabel("Policy #"), mainPanel, 0, 0, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST);
addcomponents(pTextField, mainPanel, null, 2, 0, 2, 1, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);

虽然这没关系,但由于参数的数量,很容易忘记哪个参数做了什么。我可能会考虑使用构造函数模式来构建约束,但这是我的/P>请确保您所编译的代码可以被编译和执行。也试着压缩你的代码,准确地显示出你认为问题可能出现的地方,这是我不确定的,但我坚信它是我创建的方法。你提供的代码没有实际添加任何组件到你的容器中,事实上,你的代码生成了一个
java.lang.IllegalArgumentException
,它为我运行,我想我漏掉了一些东西,我只是粘贴了我认为可疑的错误代码