如何更改JButton的大小?

时间:2017-06-04 00:10:40

标签: java swing jbutton jlabel layout-manager

我必须设计一个挥杆游戏,其中一侧是网格,另一侧是一个显示面板,我有几个JLabel和一个JButton。但无论我是否使用setSize();或setPrefferedSize();或setBounds();甚至setPrefferedSize(new Dimension());它不会变小,而是延伸整个部分。在中心对齐的任何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);

    }
}

1 个答案:

答案 0 :(得分:1)

以下代码无法解决问题。相反,它会显示与您的问题相关的一些提示,以及其他一些提示。 一般情况下尽量避免"手动控制"组件布局,但使用正确的布局管理器。

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