我正在尝试使用占据整个屏幕的 JRootPane 创建一个 JFrame,但是由于某种原因,JRootPane 的内容没有像应有的那样填满屏幕。
我认为问题在于 JRootPane 没有填满它的父级,但是 JRootPane 的子面板可能以某种方式没有填满根窗格。
这是它目前显示的内容:
以下是相关代码:
public class Runner {
public static void main(String[] args){
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
new MainFrame(devices[0]);
}
}
这是带有应该完全填充它的 rootPane 的 JFrame:
public class MainFrame extends JFrame{
private GraphicsDevice graphicsDevice;
private DisplayMode origDisplay;
private final JRootPane rootPane;
public MainFrame(GraphicsDevice graphicsDevice){
super();
this.setGraphicsDevice(graphicsDevice);
this.setOrigDisplay(graphicsDevice.getDisplayMode());
rootPane = new JRootPane();
rootPane.setContentPane(TitleScreenPanel.getInstance(this));
this.add(rootPane, BorderLayout.CENTER);
if (graphicsDevice.isFullScreenSupported()){
setUndecorated(true);
setResizable(false);
graphicsDevice.setFullScreenWindow(this);
revalidate();
}else{
System.out.println("Full-screen mode not supported");
}
try {
Theme.loadTheme(new File("res/IS.theme"));
UIManager.setLookAndFeel(new TinyLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(this);
Toolkit.getDefaultToolkit().setDynamicLayout(true);
System.setProperty("sun.awt.noerasebackground", "true");
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
rootPane.revalidate();
}
public DisplayMode getOrigDisplay() {
return origDisplay;
}
public void setOrigDisplay(DisplayMode origDisplay) {
this.origDisplay = origDisplay;
}
public GraphicsDevice getGraphicsDevice() {
return graphicsDevice;
}
public void setGraphicsDevice(GraphicsDevice graphicsDevice) {
this.graphicsDevice = graphicsDevice;
}
}
添加到 JRootPane 的面板:
public class TitleScreenPanel extends JPanel{
private static TitleScreenPanel titleScreenPanel;
private JButton exitButton;
private JButton startButton;
private TitleScreenPanel(final MainFrame context){
startButton = new JButton("START");
startButton.setFont(startButton.getFont().deriveFont(48f));
startButton.setBorder(BorderFactory.createEmptyBorder());
startButton.setContentAreaFilled(false);
exitButton = new JButton("Exit Full-Screen Mode");
exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
context.getGraphicsDevice().setDisplayMode(context.getOrigDisplay());
System.exit(0);
}
});
this.add(startButton, BorderLayout.CENTER);
this.add(exitButton, BorderLayout.SOUTH);
}
public static TitleScreenPanel getInstance(MainFrame context){
if(titleScreenPanel == null){
titleScreenPanel = new TitleScreenPanel(context);
}
return titleScreenPanel;
}
}