0

在这里,我试图将组件(按钮)锚定到面板的一侧,但我的 GridBagConstraints 显然缺少一些东西。如果可能的话,我希望他们有填充;知道我会怎么做这样的事情在此处输入图像描述吗?我可以只使用插图作为填充吗?为任何帮助而欢呼。

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

1 回答 1

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

现在,就个人而言,我会使用NORTHEAST,NORTHWEST和作为锚点SOUTHEASTSOUTHWEST这对我来说更有意义......

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

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

在此处输入图像描述

您可能想查看如何使用 GridBagLayout了解更多详细信息...

您可能还想通读Java 编程语言的代码约定,这将使人们更容易阅读您的代码;)

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

}
于 2013-11-13T06:53:55.163 回答