首先,我应该让你们知道我对Java(以及stackoverflow)仍然很陌生,并且仍在学习它,因此,如果我的问题或对该问题的答案很明显,请原谅我。我只是在这里,希望能增进知识。
好吧,所以我正在使用JFrames
,JPanels
,JLabel
和JButton
s开发一个简单的Gui,用于我要制作的游戏业余时间,当我转而从事笔记本电脑上的项目时,我注意到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);
}
}