如何将 JPanel 的大小调整为用户屏幕的大小以及让图像占据整个面板? [重复]

问题描述 投票:0回答:0

我希望能够将程序中的 JPanel 大小调整为用户屏幕的大小,并调整面板上的所有内容(例如面板图标),以便所有内容都显示在屏幕上,就像我尝试制作的那样一种视觉小说游戏。我该怎么做?到目前为止,我与此相关的代码是:

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Main {
    JFrame window;
    Container con;
    JPanel titleNamePanel, mainGamePanel, mainTextPanel;
    JLabel titleNameLabel, titleImageLabel;
    JButton newGameButton, loadGameButton, submitButton, choice1, choice2, choice3;
    JTextArea mainTextArea;
    JTextField userTextInput;
    String userName, goTo1;
    Font mtNormalFont = new Font("Serif", Font.PLAIN, 25);
    ImageIcon titleScreenImage;
    
    
    TitleScreenHandler tsHandler = new TitleScreenHandler();
    InfoScreenHandler isHandler = new InfoScreenHandler();
    StartChoice1Handler start1_1Handler = new StartChoice1Handler();
    
    //Main constructor
    Main()
    {
    window = new JFrame();
    window.setExtendedState(JFrame.MAXIMIZED_BOTH); //Setting frame size to max
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLayout(null); //Disabling the default Layout
    
    con = window.getContentPane();
    
    titleNamePanel = new JPanel();
    titleNamePanel.setBounds(0, 0, JFrame.MAXIMIZED_HORIZ, JFrame.MAXIMIZED_VERT);
    titleNamePanel.setBackground(Color.WHITE);
    con.add(titleNamePanel);
    titleNameLabel = new JLabel("Adventure in College");
    titleNameLabel.setFont(new Font("Serif", Font.PLAIN, 25));
    titleNameLabel.setHorizontalTextPosition(JLabel.CENTER);
    titleNameLabel.setVerticalTextPosition(JLabel.CENTER);
    
    titleScreenImage = new ImageIcon(getClass().getResource("images/TitleScreenImg.jpeg"));
    titleNameLabel.setIcon(titleScreenImage);
    titleNamePanel.add(titleNameLabel);
    
    mainGamePanel = new JPanel();
    mainGamePanel.setBounds(850, 500, 100, 200);
    mainGamePanel.setBackground(Color.WHITE);
    con.add(mainGamePanel);
    newGameButton = new JButton("New Game");
    newGameButton.addActionListener(tsHandler);
    loadGameButton = new JButton("Load Game");
    
    mainGamePanel.add(newGameButton);
    mainGamePanel.add(loadGameButton);
    
    window.setVisible(true);
    }

我尝试将边界宽度和高度设置为 JFrame 宽度和高度最大值。期望面板能够占据整个用户屏幕,无论他们的分辨率如何。

java swing
© www.soinside.com 2019 - 2024. All rights reserved.