Java-根据屏幕大小调整JPanel及其内容的大小

Java-根据屏幕大小调整JPanel及其内容的大小,java,swing,resize,jpanel,layout-manager,Java,Swing,Resize,Jpanel,Layout Manager,首先,我应该让你们知道,我对Java(和stackoverflow)还是很陌生,并且还在学习,所以如果我的问题或这个问题的答案是显而易见的,那么请原谅我。我只是希望在这里增长知识 好的,我正在使用JFrames、JPanels、JLabels和JButtons开发一个简单的图形用户界面,这是我在业余时间尝试制作的一个游戏,当我开始在笔记本电脑上开发这个项目时,我注意到Jpanels及其内容的大小与我的笔记本电脑屏幕大小不匹配。我假设如果我在一个比我最初启动的更大的显示器上运行我的程序,就会发生这

首先,我应该让你们知道,我对Java(和stackoverflow)还是很陌生,并且还在学习,所以如果我的问题或这个问题的答案是显而易见的,那么请原谅我。我只是希望在这里增长知识

好的,我正在使用
JFrames
JPanels
JLabel
s和
JButton
s开发一个简单的图形用户界面,这是我在业余时间尝试制作的一个游戏,当我开始在笔记本电脑上开发这个项目时,我注意到
Jpanels
及其内容的大小与我的笔记本电脑屏幕大小不匹配。我假设如果我在一个比我最初启动的更大的显示器上运行我的程序,就会发生这种情况

我还猜测,
setBounds
至少是罪魁祸首之一,但我不知道如何设置它,以使
Jpanel
能够适合我想要的内容,并根据屏幕大小调整内容大小。有没有办法解决这个问题?提前感谢您的帮助

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

    }
}

这就是我们要做的for@MadProgrammer你认为哪一个是最可定制的?我的ui不是超级复杂的,但是能够像使用setBounds一样进行同样级别的定制(能够设置X和Y坐标)将非常棒
GridBagLayout
是最可配置的,也是最复杂的。但你不会因为只使用一个布局就被困在这里,这样你就可以使用多个布局了?那怎么办?可以使用BorderLayout将标题居中,然后使用GridBagLayout等其他布局更精确地垂直移动标题,例如?例如,