java - 为什么 gridbag 约束不起作用

标签 java swing user-interface gridbaglayout

我使用了网格袋约束,当我使用插图添加间距时效果很好,但是当我使用 gridx 和 gridy 设置位置时什么也没有发生。我刚刚发布了下面的完整代码,我不知道这是否有帮助,谢谢

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

这是您将其添加到网格中的位置。

panelButton.add(btNextThree, gbc);

"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 的位置;

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

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

所以在我胡言乱语之后,简短的答案是您可能应该使用另一个 LayoutManager。但如果您想要快速修复,您可以创建第二个与 btNextThree 大小大致相同的不可见组件,并将其添加到最左边的列,这会将它们均匀地间隔成两个偶数列。下面的代码应该可以做到这一点。

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

panelButton.add(jButtonThatsSetToInvisible, gbc2);

如果您稍后希望它们在调整大小时在容器内保持正确对齐,您可能需要使用 GridBagConstraints 权重属性,但我认为这是另一篇文章的内容。

关于java - 为什么 gridbag 约束不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18231995/

相关文章:

java - 随机性的类型

java - 代号 一个事件在包含更多子容器的容器中监听

java - 如何让用户通过双击从 JOptionPane 中选择一个项目

java - 存储过程可以返回结果集吗?

java - 使用 Mysql 数据库的搜索结果刷新 Jtable?

java - KeyListener 和 keyPressed

windows - QML 缩放不适用于非整数缩放因子

java - 如何使用 set Location 移动组件

java - 如何将 EJB 注入(inject) ResourceFilterFactory ( Jersey )

java - 如何在Java中根据用户给定的长度和宽度创建X模式?