java - 如何更改 JButton 的大小?

标签 java swing jbutton jlabel layout-manager

我必须设计一个 Swing 游戏,其中一侧是网格,另一侧是显示面板,其中有几个 JLabel 和一个 JButton。但无论我是否使用 setSize();或setPreferedSize();或 setBounds();甚至 setPreferedSize(new Dimension());它不会变小,而是会拉伸(stretch)整个该部分。任何居中对齐的 JLabel/JButton 都会占据整个中心。我该如何解决这个问题?

这是我的类的代码,引用包含网格的项目中的另一个类:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Scoreboard extends JPanel{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JLabel scoreLabel;
    JLabel coord;
    JLabel title;
    JButton quit;
    public Scoreboard (int score){
        setLayout(new BorderLayout());
        setSize(490,400);
        setPreferredSize(getSize());
        setBackground(Color.BLUE);
        title = new JLabel();
        title.setIcon(new ImageIcon("C:\\Users\\Rachel\\Workspace\\Assignment2\\Planet1.png"));
        title.setSize(200,200);
        title.setHorizontalAlignment(SwingConstants.CENTER);
        add (title,BorderLayout.NORTH);
        scoreLabel = new JLabel("Score: "+Integer.toString(score));
        scoreLabel.setSize(200,200);
        scoreLabel.setBackground(Color.BLUE);
        scoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
        scoreLabel.setFont(new Font("Source Sans Pro", Font.BOLD, 40));
        scoreLabel.setForeground(Color.WHITE);
        add(scoreLabel, BorderLayout.CENTER);
        coord = new JLabel ("Click the aliens!");
        coord.setSize(200,400);
        coord.setBackground(Color.RED);
        coord.setHorizontalAlignment(SwingConstants.CENTER);
        coord.setFont(new Font("Source Sans Pro", Font.BOLD, 20));
        coord.setForeground(Color.WHITE);
        add(coord,BorderLayout.SOUTH);
        JButton quit = new JButton ("Quit Game");
        quit.setBounds(20,30,50,30);
        quit.setHorizontalAlignment(SwingConstants.CENTER);
        add(quit, BorderLayout.CENTER);

    }
}

最佳答案

下面的代码并不能解决问题。相反,它显示了一些与您的问题相关的提示以及其他一些提示。 一般来说,尽量避免“手动控制”组件布局,但使用正确的布局管理器。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font; 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class Scoreboard extends JPanel{


    JLabel scoreLabel;
    JLabel coord;
    JLabel title;
    JButton quit;
    private final int W = 490;
    private final int H = 400;

    public Scoreboard (int score){
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(W, H));
        setBackground(Color.BLUE);
        title = new JLabel("My Title");
        //if you use images in your SO posted code use web links
        //title.setIcon(new ImageIcon("C:\\Users\\Rachel\\Workspace\\Assignment2\\Planet1.png"));
        //title.setSize(200,200); run the code without it and see it has no effect
        title.setHorizontalAlignment(SwingConstants.CENTER);
        add (title,BorderLayout.NORTH);

        scoreLabel = new JLabel("Score: "+Integer.toString(score));
        scoreLabel.setSize(200,200);
        scoreLabel.setBackground(Color.BLUE);
        scoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
        scoreLabel.setFont(new Font("Source Sans Pro", Font.BOLD, 40));
        scoreLabel.setForeground(Color.WHITE);
        //this is directly related to your question. You can't add 2 components 
        //to the center. 
        //instead add a JPanel to the center, apply a layout manager to it,
        //and add scorelabel and quit button to that JPanel 
        add(scoreLabel, BorderLayout.CENTER);

        coord = new JLabel ("Click the aliens!");
        //coord.setSize(200,400); run the code without it and see it has no effect
        coord.setBackground(Color.RED);
        coord.setOpaque(true); //if you want the color show
        coord.setHorizontalAlignment(SwingConstants.CENTER);
        coord.setFont(new Font("Source Sans Pro", Font.BOLD, 20));
        coord.setForeground(Color.WHITE);
        add(coord,BorderLayout.SOUTH);

        JButton quit = new JButton ("Quit Game");
        //no need to set bounds. That is what the layout manager does
        //quit.setBounds(20,30,50,30); 
        quit.setHorizontalAlignment(SwingConstants.CENTER);
        add(quit, BorderLayout.CENTER);
    }

    //add main to your questions to make it runnable 
    public static void main(String[] args){

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        JPanel panel = new Scoreboard(50);
        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);
    }
}

关于java - 如何更改 JButton 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44349665/

相关文章:

Java 俄罗斯方 block - 旋转

java - 使用 gradle 时为 "java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper"

java - Java 中的十字准线

java - 如何在运行时向面板添加新组件

java - 有没有办法在 jbutton 之上设置 jbutton?

java - 我的 tictactoe 游戏中的每个按钮都有相同的代码。如何缩短这个?

java - 如何使用 Rational Function Tester 记录 Java Web Start 应用程序

java - Java中如何让每次按回车键时弹出一个新文本?

java - 在 JFrame 内布局 JPanel

java - JButton 和 jgraphx 不会同时出现