gpt4 book ai didi

java - 如何将标签和按钮居中?

转载 作者:行者123 更新时间:2023-12-04 07:43:48 25 4
gpt4 key购买 nike

I want my Game to look like this:
但它不会,我不知道为什么......一切都没有发生在我想要的地方。
有人可以帮助我吗?我究竟做错了什么?
It looks like this:
颜色变化不是问题......它只是所有东西所在的地方。

package view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class WelcomeScreen extends JFrame{
JButton button;
JLabel label;
ActionListener action;
GridBagLayout gb = new GridBagLayout();

public <button> WelcomeScreen(ActionListener action){
JPanel panel = new JPanel();
this.setSize(800,600);

GridBagConstraints gcon = new GridBagConstraints();
gcon.weightx = 1;
gcon.weighty = 1;
gcon.fill = GridBagConstraints.HORIZONTAL;
gcon.insets = new Insets(5,5,5,5);

button = new JButton("Start");
button.setPreferredSize(new Dimension(200, 50));
button.setFont(new Font("Arial", Font.PLAIN, 20 ));
button.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 2));
button.setHorizontalAlignment(SwingConstants.LEFT);

label = new JLabel("Game");
label.setPreferredSize(new Dimension(200, 50));
label.setFont(new Font("Arial", Font.PLAIN, 60 ));

this.action = action;
panel.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 10));
panel.setLayout(gb);
//label
gcon.gridx = 2;
gcon.gridy = 0;
//gcon.gridwidth = 4;
//gcon.gridheight = 1;
gb.setConstraints(label,gcon);
panel.add(label);

//button
gcon.gridx = 2;
gcon.gridy = 1;
//gcon.gridwidth = 2;
//gcon.gridheight = 1;
gb.setConstraints(button,gcon);
panel.add(button);
this.add(panel,BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Start");
this.setVisible(true);
button.addActionListener(action);
}
}

最佳答案

据我了解,您希望标签和按钮在框架中居中。
为此,不要设置 weightx 和 weighty,因为它们会告诉布局管理器扩展到可用空间。
所以删除

    gcon.weightx = 1;
gcon.weighty = 1;
然后,替换以下代码
    //label
gcon.gridx = 2;
gcon.gridy = 0;
//gcon.gridwidth = 4;
//gcon.gridheight = 1;
gb.setConstraints(label,gcon);
panel.add(label);


//button
gcon.gridx = 2;
gcon.gridy = 1;
//gcon.gridwidth = 2;
//gcon.gridheight = 1;
gb.setConstraints(button,gcon);
panel.add(button);
有了这个
    //label
gcon.gridx = 0;
gcon.gridy = 0;
panel.add(label, gcon);

//button
gcon.gridx = 0;
gcon.gridy = 1;
panel.add(button, gcon);
由于没有行和列的权重大于 0,布局管理器将自动将标签按钮块放在中间。
Result
完整代码
package test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class WelcomeScreen extends JFrame{
JButton button;
JLabel label;
ActionListener action;
GridBagLayout gb = new GridBagLayout();



public <button> WelcomeScreen(ActionListener action){
JPanel panel = new JPanel();
this.setSize(800,600);

GridBagConstraints gcon = new GridBagConstraints();
gcon.fill = GridBagConstraints.HORIZONTAL;
gcon.insets = new Insets(5,5,5,5);

button = new JButton("Start");
button.setPreferredSize(new Dimension(200, 50));
button.setFont(new Font("Arial", Font.PLAIN, 20 ));
button.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 2));
button.setHorizontalAlignment(SwingConstants.CENTER);

label = new JLabel("Game");
label.setPreferredSize(new Dimension(200, 50));
label.setFont(new Font("Arial", Font.PLAIN, 60 ));

this.action = action;
panel.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 10));
panel.setLayout(gb);
//label
gcon.gridx = 0;
gcon.gridy = 0;
panel.add(label, gcon);


//button
gcon.gridx = 0;
gcon.gridy = 1;
panel.add(button, gcon);
this.add(panel,BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Start");
this.setVisible(true);
button.addActionListener(action);





}

public static void main(String[] args) {
new WelcomeScreen(null).setVisible(true);
}
}

关于java - 如何将标签和按钮居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67300696/

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