Java 使用GridBagLayout在JPanel中居中放置组件
我的JPanel有一个GridBagLayout,在组件对齐方面有问题。JLabel位于顶部,但不居中,下面的JButtons位于右侧。有没有办法让他们都站在中间?下面是我初始化GUI的方法Java 使用GridBagLayout在JPanel中居中放置组件,java,swing,jpanel,layout-manager,gridbaglayout,Java,Swing,Jpanel,Layout Manager,Gridbaglayout,我的JPanel有一个GridBagLayout,在组件对齐方面有问题。JLabel位于顶部,但不居中,下面的JButtons位于右侧。有没有办法让他们都站在中间?下面是我初始化GUI的方法 public void initialize() { JButton[] moveChoices = new JButton[3]; JPanel buttonsPanel = new JPanel(); buttonsPanel.setBackground(
public void initialize() {
JButton[] moveChoices = new JButton[3];
JPanel buttonsPanel = new JPanel();
buttonsPanel.setBackground(Color.green);
buttonsPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel welcome = new JLabel();
for(int i = 0; i < moveChoices.length; i++) {
moveChoices[i] = new JButton();
c.gridx = i;
c.gridy = 1;
if (i == 0) c.anchor = GridBagConstraints.EAST;
if (i == 2) c.anchor = GridBagConstraints.WEST;
moveChoices[i].addActionListener(this);
buttonsPanel.add(moveChoices[i], c);
}
moveChoices[0].setText("Rock");
moveChoices[1].setText("Paper");
moveChoices[2].setText("Scissors");
welcome.setText("Welcome to rock paper scissors! Please enter your move.");
c.gridy = 0; c.gridx = 1; c.insets = new Insets(10, 0, 0, 0);
buttonsPanel.add(welcome);
winText = new JLabel();
buttonsPanel.add(winText, c);
this.add(buttonsPanel);
}
public void initialize(){
JButton[]moveChoices=新JButton[3];
JPanel buttonPanel=新的JPanel();
按钮面板。挫折背景(颜色。绿色);
setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
JLabel welcome=新JLabel();
for(int i=0;i
这是您的代码,它的格式是有效的。要使其正常工作,您需要注意以下事项:
public void initialize(){
JButton[]moveChoices=新JButton[3];
JPanel buttonPanel=新的JPanel();
按钮面板。挫折背景(颜色。绿色);
setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
JLabel welcome=新JLabel();
c、 fill=GridBagConstraints.BOTH;
c、 锚点=GridBagConstraints.CENTER;
welcome.setText(“欢迎来到石头剪纸!请输入您的动作”);
欢迎使用.setHorizontalAlignment(SwingConstants.CENTER);
c、 权重x=1;
c、 gridy=0;c.gridx=0;c.insets=newinsets(10,0,0,0);
c、 gridwidth=moveChoices.length;
c、 网格高度=1;
按钮面板。添加(欢迎,c);
c、 插图=新插图(0,0,0,0);
c、 网格宽度=1;
for(int i=0;i
为了更快地获得更好的帮助,请发布一个。任何JC组件都通过删除(不要使用任何)GBC来居中
public void initialize() {
JButton[] moveChoices = new JButton[3];
JPanel buttonsPanel = new JPanel();
buttonsPanel.setBackground(Color.green);
buttonsPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel welcome = new JLabel();
c.fill=GridBagConstraints.BOTH;
c.anchor=GridBagConstraints.CENTER;
welcome.setText("Welcome to rock paper scissors! Please enter your move.");
welcome.setHorizontalAlignment(SwingConstants.CENTER);
c.weightx=1;
c.gridy = 0; c.gridx = 0; c.insets = new Insets(10, 0, 0, 0);
c.gridwidth=moveChoices.length;
c.gridheight=1;
buttonsPanel.add(welcome,c);
c.insets=new Insets(0,0,0,0);
c.gridwidth=1;
for(int i = 0; i < moveChoices.length; i++) {
moveChoices[i] = new JButton();
c.gridx = i;
c.gridy = 1;
c.weightx=0.5;
c.weighty=1;
moveChoices[i].addActionListener(this);
buttonsPanel.add(moveChoices[i], c);
}
moveChoices[0].setText("Rock");
moveChoices[1].setText("Paper");
moveChoices[2].setText("Scissors");
this.add(buttonsPanel);
}