我试图让 JPanel 在您单击按钮时全屏显示,并在您按下转义键时再次返回。
我已经设法让窗口全屏显示,但是由于添加组件从其他容器中删除它们的整个过程,我最终得到了一个空白的 JPanel。
我选择单独制作一个JFrame来渲染全屏,其类如下(注意这是一个内部类,所以myPanel指的是MyJFrame中已经存在的一个面板):
public class FullScreen extends JFrame {
private static final long serialVersionUID = 1L;
private GraphicsDevice device;
private boolean isFullScreen;
public FullScreen() {
this.setContentPane(myPanel);
this.setUndecorated(true);
// Fullscreen return
this.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
// Exit fullscreen when ESC pressed
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
exitFullScreen();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
});
}
public void enterFullScreen() {
if (!isFullScreen) {
// Get the current device
GraphicsEnvironment graphicsEnvironment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
device = graphicsEnvironment.getDefaultScreenDevice();
if (device.isFullScreenSupported()) {
// Make the current window invisible
MyJFrame.this.setVisible(false);
// Set the full screen window
device.setFullScreenWindow(this);
isFullScreen = true;
}
}
}
public void exitFullScreen() {
if (isFullScreen) {
// Reset the full screen window
device.setFullScreenWindow(null);
MyJFrame.this.setVisible(true);
isFullScreen = false;
}
}
}
关于如何实现这一目标的任何其他好主意?