0

First, I should let you guys know that I am still very new to Java (and stackoverflow) and am still learning it, so if my question or the answer to this question is an obvious one, then please forgive me. I'm just here in hopes to grow in knowledge.

Ok, so I am working on a simple Gui using JFrames, JPanels, JLabels and JButtons for a game that I am trying to make in my spare time and when I transferred to working on the project on my laptop for a bit of it, I noticed that the Jpanels and their contents don't resize properly to the screen size of my laptop. I am assuming that this would happen if I ran my program on a bigger monitor than what I originally started on as well.

I'm also guessing that setBounds is at least one of the culprits, but I don't have any idea how to set it so that the Jpanel can fit the content that I want and also have the contents resize based on the screen size as well. Any ideas on how I can fix this? Thanks in advance for any help!

Code for the Gui:

import javax.swing.*;
import java.awt.*;

public class UI
{

    private JFrame window;
    private JPanel titlePanel, playPanel, quitPanel;
    private JLabel titleLabel;
    private JButton playButton, quitButton;
    private Font titleFont = new Font("Verdana", Font.PLAIN, 120);
    private Font normalFont = new Font("Verdana", Font.PLAIN, 25);

    Board board = new Board();


    public void createUI(Game.ChoiceHandler mHandler)
    {
        //Creates the window
        window = new JFrame("Game");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().setBackground(Color.black);
        window.setExtendedState(JFrame.MAXIMIZED_BOTH); //Makes the application fullscreen every time

        //Creates the title screen
        titlePanel = new JPanel();
        titlePanel.setBounds(200, 100, 1100, 500);
        titlePanel.setBackground(Color.black);
        titleLabel = new JLabel("Game title");
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setFont(titleFont);
        titlePanel.add(titleLabel);

        //Creates the panel for Play
        playPanel = new JPanel();
        playPanel.setBounds(650, 600, 300, 200);
        playPanel.setBackground(Color.black);
        //Creates the Play button
        playButton = new JButton("Play");
        playButton.setBackground(Color.black);
        playButton.setForeground(Color.WHITE);
        playButton.setFont(normalFont);
        playButton.setFocusPainted(false);
        playButton.addActionListener(mHandler); //Another class handles action listener for buttons
        playButton.setActionCommand("Play"); //Switch case in another class that takes the player
                                             //to the board screen to play the game if the button is 
                                             //pressed
        playPanel.add(playButton);

        //Creates the panel for Quit
        quitPanel = new JPanel();
        quitPanel.setBounds(650, 700, 300, 200);
        quitPanel.setBackground(Color.black);
        //Creates the Quit button
        quitButton = new JButton("Quit");
        quitButton.setBackground(Color.black);
        quitButton.setForeground(Color.WHITE);
        quitButton.setFont(normalFont);
        quitButton.setFocusPainted(false);
        quitButton.addActionListener(mHandler);
        quitButton.setActionCommand("Quit");
        quitPanel.add(quitButton);

        //Adds all the panels to the JFrame
        window.add(titlePanel);
        window.add(playPanel);
        window.add(quitPanel);
        //Adds the board to the JFrame
        window.add(board.getGui());
        window.setLocationRelativeTo(null);
        window.setVisible(true);

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
TopHats
  • 13
  • 1
  • 2
  • 2
    This is what [layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) are for – MadProgrammer Nov 29 '19 at 02:59
  • @MadProgrammer Which one would you say is the most customizable? My ui isnt super complex or anything, but being able to be able to have the same level of customization as I would using setBounds (being able set the X and Y coordinates) would be great – TopHats Nov 29 '19 at 03:06
  • 2
    `GridBagLayout` is one of the most configurable, it's also one of the most complex. But you're not stuck with using just one – MadProgrammer Nov 29 '19 at 03:11
  • So you can use multiple layouts? How would that work? Maybe use a BorderLayout to center something like the title and then use another layout like GridBagLayout to more precisely move the title vertically for example? – TopHats Nov 29 '19 at 03:19
  • Yes. Each container would have it's own layout manager and you can continue adding containers to each other to get the effect you want – MadProgrammer Nov 29 '19 at 03:22
  • 1
    For [example](https://stackoverflow.com/questions/26411775/how-i-can-do-swing-complex-layout-java/26412976#26412976), [exmaple](https://stackoverflow.com/questions/24462297/layout-relative-to-screensize/24462643#24462643), [example](https://stackoverflow.com/questions/33946629/relative-and-absolute-positioning-issues-with-swing/33947433#33947433), [example](https://stackoverflow.com/questions/15320180/jframe-components-size-and-location/15320385#15320385), [example](https://stackoverflow.com/questions/33576358/how-to-use-java-swing-layout-manager-to-make-this-gui/33577874#33577874) – MadProgrammer Nov 29 '19 at 03:25
  • Thanks for linking the examples! These should help me figure out how to at least put together a halfway decent layout. – TopHats Nov 29 '19 at 03:31

0 Answers0