gpt4 book ai didi

java - GridBagLayout 不使用 anchor

转载 作者:行者123 更新时间:2023-12-01 23:26:14 24 4
gpt4 key购买 nike

这里我试图将组件(按钮)锚定到面板的一侧,但我的 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;
}
}

最佳答案

添加组件时,您需要将 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 作为 anchor ,它只是对我来说更有意义......

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

}

关于java - GridBagLayout 不使用 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19947243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com