1

I have to design a swing game where one side is a grid and the other side is somewhat of a display panel where I have several JLabels and a JButton. But no matter if I use setSize(); or setPrefferedSize (); or setBounds(); or even setPrefferedSize(new Dimension()); it will not become smaller but instead stretches the entirety of that section. Any JLabel/JButton aligned in the center takes up the entire center. How do I fix this?

This is the code to my class referencing to another class in the project containing the grid:

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

    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
Rachel Su
  • 13
  • 3
  • I think you need to spend more time researching how layout managers work and which ones work with the `preferredSize` of components and which ones don't (or like to do there own thing) - `BorderLayout` will cause the component to fill the entire available space of the container – MadProgrammer Jun 04 '17 at 00:24
  • Maybe have a look at [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – MadProgrammer Jun 04 '17 at 00:25
  • Swing layout managers are not required to care about size, bound or preferred size of components. Usually preferred size is the most common one used by layout managers. – tsolakp Jun 04 '17 at 03:44
  • 1) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jun 05 '17 at 01:26

1 Answers1

1

The following code does not solve the problem. Instead it shows some tips related to your question, as well as some others. In general try to avoid "manually control" components layout, but use the right layout managers.

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);
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65