爪哇 - 屏幕变黑,设置一个JFrame到全屏时 [英] Java - Screen turns black, when setting a JFrame to Fullscreen
问题描述
我想画在Canvas的东西,把它添加到一个JFrame,然后设置此JFrame的到全屏。我的问题是:在fullscreenmode我只看到一个黑色的屏幕。
屏幕变黑之前,我很快可以看到画布上的粉红色背景。
I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen. Before the screen turns black I shortly can see the pink background of the canvas.
直接在JFrame上绘图,然后将其设置为全屏运行完美,我可以看到testtext。我想有一个与正常显示的画布的一个问题。
Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly.
下面是我的code:
public class FullscreenTest extends Canvas {
private JFrame mainFrame;
public FullscreenTest(){
this.mainFrame = new JFrame();
JPanel contentPane = (JPanel) mainFrame.getContentPane();
contentPane.add(this);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
s.setFullScreen(dm, this.mainFrame);
try {
Thread.sleep(5000);
} catch (InterruptedException exc) { exc.printStackTrace(); }
s.closeFullScreenWindow();
}
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
FullscreenTest test = new FullscreenTest();
test.run(dm);
}
}
下面是个什么Screen.setFullScreen(DISPLAYMODE DM,JFrame的窗口)方法:
Here is what the Screen.setFullScreen(DisplayMode dm, JFrame window) method does:
//graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
// .getDefaultScreenDevice();
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
graphicsDevice.setFullScreenWindow(window);
if(dm != null && graphicsDevice.isDisplayChangeSupported()){
graphicsDevice.setDisplayMode(dm);
}
}
有没有人有一个线索,为什么我没有看到在JFrame的全屏内容?
Does anyone have a clue why I don't see the JFrame's content in fullscreen?
推荐答案
1)你有三个一般性的问题。
1) you have three general issues
(如果不存在真正的重要原因)不混合的 AWT与Swing 剩下的就是这里 ,并使用的JPanel
而不是画布
(为画布
是有油漆
方法,的JPanel
有的paintComponent
)
(if aren't there really important reasons) don't mixing AWT with Swing the rest is here, and use JPanel
instead of Canvas
(for Canvas
is there paint
method, for JPanel
is there paintComponent
)
您公共无效漆(图形G){
是的JFrame
而不是画布
和视频下载(5000);
2)Swing GUI的rellated应该被包装成的invokeLater()
意
2) Swing GUI rellated should be wrapped into invokeLater()
meaning
public static void main(String[] args){
更在初始线程
3)链接code例如 你可以找到demostrations如何使用后台线程在Swing
3) in linked code example you can find out demostrations how to use background thread in the Swing
这篇关于爪哇 - 屏幕变黑,设置一个JFrame到全屏时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!