为什么gridbag约束不起作用 [英] Why is the gridbagconstraint not working

查看:95
本文介绍了为什么gridbag约束不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了网格袋约束,当我使用插入物添加可以正常工作的间距时,但是当我使用gridx和gridy设置位置时,什么也没发生.我刚刚在下面发布了整个代码,不知道这是否有帮助,谢谢

I have used a grid bag constraint, when I use the insets to add the spacing that works fine however when I use the gridx and gridy to set the position nothing happens. I just posted the entire code below, I don't know if this would help, thanks

import javax.swing.*;import java.awt.BorderLayout;import java.awt.CardLayout;importjava.awt.Color;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.ImageIcon import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JScrollPane;importjavax.swing.JTree;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeModel;public class MyWizard {
private JFrame frame = new JFrame("My Wizard");
private JPanel panelContainer = new JPanel();
private JPanel panelFirst = new JPanel();
private JPanel panelSecond = new JPanel();
private JPanel panelThird = new JPanel();

private JPanel panelButton = new JPanel(new GridBagLayout());

private JButton btNext = new JButton ("Next");
private JButton btNextTwo = new JButton ("Next");
private JButton btNextThree = new JButton("Next");
private JRadioButton btLdap, btKerbegos, btSpnego, btSaml2;
private JCheckBox btSan, btNFS, btYUMserver;
private CardLayout c1 = new CardLayout();
private JScrollPane scrollPane;
private JLabel lblPicture, lblPictureTwo;
DefaultMutableTreeNode root = new DefaultMutableTreeNode("FMW Components");


public MyWizard() {
    //tree code
    final DefaultMutableTreeNode accessibility =
            add(root, "DB Tier", true);
    add(accessibility, "RAC", false);
    add(accessibility, "Gateways", false);
    add(accessibility, "Datavault", false);
    add(accessibility, "Agent", false);
    add(accessibility, "Custom Databases", false);
    root.add(accessibility);

    final DefaultMutableTreeNode browsing =
            new DefaultMutableTreeNode("APP Tier");
    add(browsing, "IDM (OID, OVD)", false);
    add(browsing, "IAM (Access Manager)", false);
    add(browsing, "BIEE", false);
    add(browsing, "Forms and Reports", false);
    add(browsing, "Discoverer", false);
    add(browsing, "Apps", false);
    add(browsing, "Apex(4.2.1)", false);
    root.add(browsing);

    final DefaultTreeModel treeModel = new DefaultTreeModel(root);
    final JTree tree = new JTree(treeModel);

    final CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
    tree.setCellRenderer(renderer);

    final CheckBoxNodeEditor editor = new CheckBoxNodeEditor(tree);
    tree.setCellEditor(editor);
    tree.setEditable(true);
    scrollPane = new JScrollPane(tree);
    //tree code ends

    panelFirst.setLayout(null); 
    panelSecond.setLayout(null); 
    panelThird.setLayout(new BorderLayout());


    panelContainer.setLayout(c1);
    panelFirst.add(btNext);
    panelSecond.add(btNextTwo);


    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(5,5,5,5);

    //this is not working
    gbc.gridx = 1;
    gbc.gridy = 0;

    panelButton.add(btNextThree, gbc);
    panelThird.add(scrollPane, BorderLayout.CENTER);
    panelThird.add(panelButton,  BorderLayout.SOUTH);

    panelFirst.setBackground(Color.white);
    panelSecond.setBackground(Color.white);

    panelContainer.add(panelFirst, "1");
    panelContainer.add(panelSecond,"2");
    panelContainer.add(panelThird,"3");
    c1.show(panelContainer, "1");


    btNext.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            c1.show(panelContainer,"2");

        }

    });

    btNextTwo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            c1.show(panelContainer,"3");

        }

    });
    RadioButtons();
    Button();
    Buttons();
    CheckList();
    groupButton();
    Image();
    frame.add(panelContainer);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.pack();
    frame.setSize(400,310);
    frame.setVisible(true); 
}


public void RadioButtons() {
    btLdap = new JRadioButton ("Ldap");
    btLdap.setBounds(60,85,100,20);
    btLdap.setBackground(Color.white);
    panelFirst.add(btLdap);

    btKerbegos = new JRadioButton ("Kerbegos");
    btKerbegos.setBounds(60,115,100,20);
    btKerbegos.setBackground(Color.white);
    panelFirst.add(btKerbegos);

    btSpnego =new JRadioButton("Spnego");
    btSpnego.setBounds(60,145,100,20);
    btSpnego.setBackground(Color.white);
    panelFirst.add(btSpnego);

    btSaml2 = new JRadioButton("Saml2");
    btSaml2.setBounds(60,175,100,20);
    btSaml2.setBackground(Color.white);
    panelFirst.add(btSaml2);
}

public void Button() {
    btNext.setBounds(250,240,100,20);
}

public void CheckList () {
    btSan = new JCheckBox ("San");
    btSan.setBounds(60,85,100,20);
    btSan.setBackground(Color.white);
    panelSecond.add(btSan);

    btNFS = new JCheckBox ("NFS");
    btNFS.setBounds(60,115,100,20);
    btNFS.setBackground(Color.white);
    panelSecond.add(btNFS);

    btYUMserver =new JCheckBox("Spnego");
    btYUMserver.setBounds(60,145,100,20);
    btYUMserver.setBackground(Color.white);
    panelSecond.add(btYUMserver);
}

public void Buttons() {
    btNextTwo.setBounds(250,240,100,20);
}



public void Image() {
    ImageIcon image = new ImageIcon("iconpic.png");
    lblPicture = new JLabel(image);
    lblPicture.setBounds(140,5, 330, 270);
    panelFirst.add(lblPicture);

    ImageIcon imageTwo = new ImageIcon("iconpic.png");
    lblPictureTwo = new JLabel(imageTwo);
    lblPictureTwo.setBounds(140,5, 330, 270);
    panelSecond.add(lblPictureTwo);
}

private void groupButton() {

    ButtonGroup bg1 = new ButtonGroup( );

    bg1.add(btLdap);
    bg1.add(btKerbegos);
    bg1.add(btSpnego);
    bg1.add(btSaml2);

}

private static DefaultMutableTreeNode add(
        final DefaultMutableTreeNode parent, final String text,
        final boolean checked)
{
    final CheckBoxNode data = new CheckBoxNode (text, checked);
    final DefaultMutableTreeNode node = new DefaultMutableTreeNode(data);
    parent.add(node);
    return node;
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new MyWizard();
        }
    });

}}

推荐答案

在我看来,您对GridBagLayout的运作方式有一些严重的误解.在当前代码中,GridBagLayout ...中只有一个JComponent,即btNextThree.

It looks to me like you have some serious misunderstandings of the workings of GridBagLayout. In your current code, there is only one JComponent inside your GridBagLayout... and that is btNextThree.

在这里您将其添加到网格中.

Here is where you added it into the Grid.

panelButton.add(btNextThree, gbc);

目前,panelButton中的按钮位于中间,但我只是 想要将其移到最右边,什么是最好的方法?".

"At the moment the button in panelButton is in the center but I just want to move it to the far right, what is the best way to do that?".

您不能使用GridBagLayout做到这一点……至少不是您的想法.在向GridBagLayout的Grid中的单元格添加大小之前,它们没有大小,因此在其中添加btNextThree的位置;

You can't do that with GridBagLayout... at least not the way you think. The cells in the Grid of GridBagLayout don't have a size until they have a component added to them, so where you have added btNextThree to;

gbc.gridx = 1;
gbc.gridy = 0;

现在位于两列和一行的网格的最右侧单元格中.但是,最左边的列没有大小,因为它内部只有潜在的空间.因此,内部带有btNextThree的右列将占用所有可用空间.

It is now in the right-most cell in a grid of two columns and one row. However, the left-most column has no size, as it only has potential space inside it. Therefore, your right column with btNextThree inside takes up all available space.

因此,在我呀bab咕之后,简短的答案是您可能应该使用另一个LayoutManager.但是,如果您想快速进行肮脏的修复,则可以创建第二个不可见的组件,其大小与btNextThree大致相同,并将其添加到最左边的列中,这样可以将它们均匀地分成两个偶数列.下面的代码应该做到这一点.

So after all my babbling the short answer is that you should probably use another LayoutManager. But if you want a quick dirty fix you could create a second invisible component of roughly the same size as btNextThree and add it to the left most column, which would space them out evenly into two even columns. The below code should do that.

GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.insets = new Insets(5,5,5,5);
gbc2.gridx = 0;
gbc2.gridy = 0;

panelButton.add(jButtonThatsSetToInvisible, gbc2);

如果以后您希望它们在调整大小时在其容器中正确对齐,则可能必须使用GridBagConstraints weight属性,但这是我认为的另一篇文章.

If you later want them to stay correctly aligned within their container when resizing you may have to play with the GridBagConstraints weight attributes, but that is for another post I think.

这篇关于为什么gridbag约束不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆