我想创建一个 Java 应用程序屏幕,可以通过切换 F11 键在窗口模式和全屏模式之间切换。
但是当我按 F11 时,它会将屏幕更改为更大的窗口而不是全屏。这是输出窗口的图片。
所以,你可以看到在屏幕的左侧、右侧或底部,它仍然有一点空间没有覆盖窗口,在顶部,它仍然显示我不想要的退出和最小化按钮以显示。我该如何解决这个问题?
这是我的代码。
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Game extends JFrame
{
Font titleFont = new Font("Times new Roman", Font.PLAIN, 72);
Font normalFont = new Font("Times new Roman", Font.PLAIN, 32);
JFrame window;
Container con;
JPanel titleNamePanel, startButtonPanel, informationButtonPanel;
JLabel titleNameLable;
JButton startButton, informationButton;
GraphicsEnvironment environment;
GraphicsDevice device;
public static void main(String[] args)
{
new Game();
}
public Game()
{
setWindow();
setTitle();
setStartButton();
setInformationButton();
setContainer();
toggleChangeScreenMode();
}
public void toggleChangeScreenMode()
{
window.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_F11)
{
changeScreenMode();
}
}
}
);
}
public void changeScreenMode()
{
environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
if(device.isFullScreenSupported())
{
if(device.getFullScreenWindow() != null)
{
device.setFullScreenWindow(null);
window.setSize(854, 576);
window.setResizable(false);
window.setLocationRelativeTo(null);
}
else
{
device.setFullScreenWindow(window);
}
}
else
{
}
}
public void setWindow()
{
window = new JFrame();
window.setSize(854, 576);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.BLACK);
window.setLayout(null);
window.setVisible(true);
window.setResizable(false);
window.setFocusable(true);
window.requestFocus();
}
public void setContainer()
{
con = window.getContentPane();
con.add(titleNamePanel);
con.add(startButtonPanel);
con.add(informationButtonPanel);
}
public void setTitle()
{
titleNamePanel = new JPanel();
titleNamePanel.setBounds(100, 100, window.getContentPane().getWidth() - 200, 100);
titleNamePanel.setOpaque(false);
titleNameLable = new JLabel("MONSTER SLAYER");
titleNameLable.setForeground(Color.WHITE);
titleNameLable.setFont(titleFont);
titleNamePanel.add(titleNameLable);
}
public void setStartButton()
{
startButtonPanel = new JPanel();
startButtonPanel.setBounds(325, 300, window.getContentPane().getWidth() - 650, 100);
startButtonPanel.setOpaque(false);
startButtonPanel.setBorder(null);
startButton = new JButton("START");
startButton.setForeground(Color.WHITE);
startButton.setOpaque(false);
startButton.setContentAreaFilled(false);
startButton.setBorderPainted(false);
startButton.setFont(normalFont);
startButtonPanel.add(startButton);
}
public void setInformationButton()
{
informationButtonPanel = new JPanel();
informationButtonPanel.setBounds(325, 350, window.getContentPane().getWidth() - 650, 100);
informationButtonPanel.setOpaque(false);
informationButtonPanel.setBorder(null);
informationButton = new JButton("INFO");
informationButton.setForeground(Color.WHITE);
informationButton.setOpaque(false);
informationButton.setContentAreaFilled(false);
informationButton.setBorderPainted(false);
informationButton.setFont(normalFont);
informationButtonPanel.add(informationButton);
}
}