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
, JLabel
s and JButton
s 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);
}
}