jpanels,jframes和Windows,哦,我的!
-
16-09-2019 - |
题
简而言之,我正在尝试制作一个我正在从事全屏工作的游戏。
我有以下代码试图使用:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
if(!gs.isFullScreenSupported()) {
System.out.println("full-screen not supported");
}
Frame frame = new Frame(gs.getDefaultConfiguration());
Window win = new Window(frame);
try {
// Enter full-screen mode
gs.setFullScreenWindow(win);
win.validate();
}
问题是我在扩展jpanel的类中工作,尽管我有类型帧的变量,但我在类中没有类型的窗口。
我对jpanel的理解是,这是一个窗口,但是我不能将“此”传递到gs.setfullscreenwindow(Window win)...我该怎么做?
使用JPanel有什么简单的调用方法或类似方法?
有什么方法可以从jpanel那里获得类型的窗口?
-
编辑:以下方法更改JFRAME的状态,每10ms称为每10ms:
public void paintScreen()
{
Graphics g;
try{
g = this.getGraphics(); //get Panel's graphic context
if(g == null)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
frame.add(this);
frame.pack();
frame.setResizable(false);
frame.setTitle("Game Window");
frame.setVisible(true);
}
if((g != null) && (dbImage != null))
{
g.drawImage(dbImage, 0, 0, null);
}
Toolkit.getDefaultToolkit().sync(); //sync the display on some systems
g.dispose();
}
catch (Exception e)
{
if(blockError)
{
blockError = false;
}
else
{
System.out.println("Graphics context error: " + e);
}
}
}
我预计在if(g == null)语句(所有frame.somethingorother()s)之后,可能会有一些冗余或不必要的电话,任何清理建议都将不胜感激...
解决方案
您在这个问题上取得了任何进展吗?如果您可以通过预期行为更新问题以及代码实际上在做什么,可能会有所帮助?如前所述,Jframe是窗口的子类,因此,如果您有Jframe,则不需要窗口。
对于它的价值,我有一个以全屏模式运行的Java应用程序。尽管屏幕的重新粉刷不如您的频率,但经常重新粉刷。我做以下操作以输入全屏:
// pseudo-code; not compilable
JPanel container = new JPanel();
container.setOpaque( true ); // make sure the container will be visible
JFrame frame = new JFrame();
frame.getContentPane().add(container); // add the container to the frame
frame. ... //other initialization stuff, like default close operation, maximize, etc
if ( fullScreenModeIsSupported )
frame.setUndecorated( true ); // remove window decorations from the frame
gs.setFullScreenWindow( frame );
frame.validate();
然后,每当我需要更新屏幕时,我都会将新的jpanel插入 container
jpanel:
// pseudo-code; not compilable
container.removeAll(); // clean out the container
container.add( jPanelWithNewDisplay ); // add the new display components to the container
container.validate(); // update and redisplay
container.repaint();
不能声称它在技术上是完美的,但是对我来说很好。如果伪代码示例没有剪切,我可以花一些时间将一个可编译的示例放在一起。
其他提示
我想我有你需要的。
设置框架未装饰,因此没有任何标题栏和东西。
将面板添加到框架中。因此,看起来只有您的面板显示。
最大化框架。因此,现在看起来只有您的面板在没有窗户的情况下将全屏幕拿走。
frame.setundecorated(true); frame.Add(面板); //现在最大化您的框架。
笔记: :重要的是要注意,只有在框架不可分录的情况下才能调用未装饰的API,因此,如果它已经显示出来,则首先您需要进行固定(false)。
Edit1: :如果您只想获取包含面板的窗口,那么您可以做到这一点:
Window win = SwingUtilities.getAncestorOfClass(Window.class, myPanel);
获得窗口实例后,您可以在任何需要的地方传递它。
Edit2: :也是 Frame
课程扩展 Window
所以你可以直接做 gs.setFullScreen(frame)
. 。您无需为该框架创建一个新窗口。
我对jpanel的理解是,这是一个窗口
你为什么那么想?您读过API吗? jpanel会从窗户延伸吗?
您可以尝试使用Swingutilities类。它具有返回给定组件的窗口的方法。