gpt4 book ai didi

java - 全屏模式下的 JRootPane 大小调整问题

转载 作者:行者123 更新时间:2023-12-02 05:32:18 26 4
gpt4 key购买 nike

我正在尝试创建一个带有占据整个屏幕的 JRootPane 的 JFrame,但是由于某种原因,JRootPane 的内容没有像应有的那样填满屏幕。

我认为问题在于 JRootPane 未填充其父 Pane ,但 JRootPane 的子面板可能以某种方式未填充根 Pane 。

这是当前显示的内容:

Running Application

相关代码如下:

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;
}
}

最佳答案

让我们开始...您不需要创建 JRootPaneJFrame 已经有一个,事实上,这是您真正需要的唯一东西要做的是将 TitleScreenPane 添加或设置为 JRootPanecontentPane,例如...

//rootPane = new JRootPane();
setContentPane(TitleScreenPanel.getInstance(this));
//add(rootPane, BorderLayout.CENTER);

接下来,JPanel 默认使用 FlowLayout,因此您所看到的实际上正是您应该看到的。

为了获得您想要的效果,您需要将 TitleScreenPanel 的布局管理器更改为 BorderLayout...

作为一个可运行的示例...

FullScreen

import java.awt.BorderLayout;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame {

public static void main(String[] args) {
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
new MainFrame(devices[0]);
}

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();
setContentPane(TitleScreenPanel.getInstance(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;
}

public static class TitleScreenPanel extends JPanel {

private static TitleScreenPanel titleScreenPanel;
private JButton exitButton;
private JButton startButton;

private TitleScreenPanel(final MainFrame context) {
setLayout(new BorderLayout());
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;
}
}
}

关于java - 全屏模式下的 JRootPane 大小调整问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25415345/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com