我的所有组件都放在一起,我已经尝试了一段时间了

时间:2015-03-29 22:08:14

标签: java swing layout-manager gridbaglayout

有人可以告诉我我做错了什么,我尝试首先放置面板但结果相同,然后我回去使用组件和相同的结果,所有的组件彼此叠加,我尝试了各种各样的事情,结果相同,它们相互叠加并且在中间

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

1 个答案:

答案 0 :(得分:2)

代码有点难以理解......

首先,我建议不要在容器之间共享同一个GridBagLayout实例...

gridbag = new GridBagLayout();
setLayout(gridbag);
//...
mainPanel = new JPanel(gridbag);

这可能会导致问题,而是为每个容器提供自己的GridBagLayout

实例

这意味着您无法执行gridbag.setConstraints(cont, gbc);之类的操作,事实上,我可能会建议您根据代码尝试工作的方式来反对它,而是使用某些东西比如main.add(cont, gbc),它会将约束传递给当前分配的布局管理器。

其次,gridwidthgridheight在正常情况下应不低于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);

虽然这没关系,但由于参数的数量,很容易失去跟踪哪个参数做什么。我可能会考虑使用构建器模式来构建约束,但那就是我