GridBagLayout没有使用锚点

时间:2013-11-13 06:50:24

标签: java swing jbutton gridbaglayout

这里我试图将组件(按钮)固定到面板的一侧,但我显然错过了我的GridBagConstraints。如果可能,我希望他们有填充;任何想法我会怎么做这样的enter image description here?我可以使用Insets作为填充吗?欢呼任何帮助。

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class PlayGamePanel extends JComponent{

    @SuppressWarnings("unused")
    JComponent GamePanel2() throws IOException {

        JComponent GamePanel = new JLabel();

        //Setting up the gridbaglayout
        GamePanel.setLayout(new GridBagLayout());
        GridBagConstraints gbLayout = new GridBagConstraints();

        /*//Creating a button to test out switching panels
         * 
        JButton testButton = new JButton ("test");
        GamePanel.add(testButton);*/

        //Creating buttons for answering the quiz

        gbLayout.gridx = 0;
        gbLayout.gridy = 0;
        gbLayout.weightx = 1.0;
        gbLayout.anchor = GridBagConstraints.NORTHWEST;
        gbLayout.insets = new Insets(20, 20, 20, 20);

        JButton AnswerOneButton = new JButton("Answer One");
        GamePanel.add(AnswerOneButton);
        GamePanel.add(AnswerOneButton, gbLayout);

        gbLayout.gridx = 0;
        gbLayout.gridy = 0;
        gbLayout.weightx = 1.0;
        gbLayout.anchor = GridBagConstraints.NORTHEAST;
        gbLayout.insets = new Insets(20, 20, 20, 20);

        JButton AnswerTwoButton = new JButton("Answer Two");
        GamePanel.add(AnswerTwoButton);
        GamePanel.add(AnswerTwoButton, gbLayout);

        gbLayout.gridx = 0;
        gbLayout.gridy = 0;
        gbLayout.weightx = 1.0;
        gbLayout.anchor = GridBagConstraints.SOUTHEAST;
        gbLayout.insets = new Insets(20, 20, 20, 20);

        JButton AnswerThreeButton = new JButton("Answer Three");
        GamePanel.add(AnswerThreeButton);
        GamePanel.add(AnswerThreeButton, gbLayout);


        gbLayout.gridx = 0;
        gbLayout.gridy = 0;
        gbLayout.weightx = 1.0;
        gbLayout.anchor = GridBagConstraints.SOUTHWEST;
        gbLayout.insets = new Insets(20, 20, 20, 20);


        JButton AnswerFourButton = new JButton("Answer Four");
        GamePanel.add(AnswerFourButton);
        GamePanel.add(AnswerFourButton, gbLayout);

        return GamePanel;
    }
}

1 个答案:

答案 0 :(得分:3)

添加组件时,您需要将GridBagConstraints传递给容器...

例如......

GamePanel.add(AnswerOneButton, gbLayout);

您还需要为每个组件提供单元格位置,例如......

gbLayout.gridx = 0;
gbLayout.gridy = 0;
JButton AnswerOneButton = new JButton("Answer One");
GamePanel.add(AnswerOneButton, gbLayout);

AnswerOneButton按钮放在第一行的第一列。

您可以使用insets属性为每个单元格提供填充,例如......

gbLayout.weightx = 1.0;
gbLayout.gridx = 0;
gbLayout.gridy = 0;
gbLayout.insets = new Insets(20, 20, 20, 20);
JButton AnswerOneButton = new JButton("Answer One");
GamePanel.add(AnswerOneButton, gbLayout);

现在,就个人而言,我会使用NORTHEASTNORTHWESTSOUTHEASTSOUTHWEST作为主播,这对我来说更有意义......

gbLayout.weightx = 1.0;
gbLayout.gridx = 0;
gbLayout.gridy = 0;
gbLayout.insets = new Insets(20, 20, 20, 20);
gbLayout.anchor = GridBagConstraints.NORTHWEST;
JButton AnswerOneButton = new JButton("Answer One");
GamePanel.add(AnswerOneButton, gbLayout);

在更新了所有按钮的所有约束后,我得到类似的东西......

enter image description here

您可能需要查看How to Use GridBagLayout了解更多详情......

您可能还想阅读Code Conventions for the Java Programming Language,这会让人们更容易阅读您的代码;)

使用JLabel作为主要容器也是一个相当有趣的选择,您可能会发现JPanel提供更好的支持

更新了示例代码

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout {

    public static void main(String[] args) {
        new TestLayout();
    }

    public TestLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(GamePanel2());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    @SuppressWarnings("unused")
    JComponent GamePanel2() {

        JComponent GamePanel = new JPanel();
        GamePanel.setOpaque(false);

        //Setting up the gridbaglayout
        GamePanel.setLayout(new GridBagLayout());
        GridBagConstraints gbLayout = new GridBagConstraints();

        /*//Creating a button to test out switching panels
         * 
         JButton testButton = new JButton ("test");
         GamePanel.add(testButton);*/
        //Creating buttons for answering the quiz
        gbLayout.gridx = 0;
        gbLayout.gridy = 0;
        gbLayout.weightx = 1.0;
        gbLayout.anchor = GridBagConstraints.NORTHWEST;
        gbLayout.insets = new Insets(20, 20, 20, 20);
        JButton AnswerOneButton = new JButton("Answer One");
        GamePanel.add(AnswerOneButton, gbLayout);

        gbLayout.gridx = 1;
        gbLayout.anchor = GridBagConstraints.NORTHEAST;
        JButton AnswerTwoButton = new JButton("Answer Two");
        GamePanel.add(AnswerTwoButton, gbLayout);

        gbLayout.gridx = 0;
        gbLayout.gridy = 1;
        gbLayout.anchor = GridBagConstraints.SOUTHWEST;
        JButton AnswerThreeButton = new JButton("Answer Three");
        GamePanel.add(AnswerThreeButton, gbLayout);

        gbLayout.gridx = 1;
        gbLayout.gridy = 1;
        gbLayout.weightx = 1.0;
        gbLayout.anchor = GridBagConstraints.SOUTHEAST;
        JButton AnswerFourButton = new JButton("Answer Four");
        GamePanel.add(AnswerFourButton);
        GamePanel.add(AnswerFourButton, gbLayout);

        return GamePanel;
    }

}