java 为什么我的背景颜色不会在 JFrame 中显示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11268357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why won't my background color display in JFrame?
提问by Ethan Pieper
I have two class files:
我有两个类文件:
Screen https://gist.github.com/3020101
屏幕https://gist.github.com/3020101
JMain https://gist.github.com/3020107
JMain https://gist.github.com/3020107
I'm trying to get it to go fullscreen for 5 seconds and display the background (or, at this point, even the foreground) but when I run it it goes fullscreen for 5 seconds, yay, but it is just a blank light grey screen.
我试图让它全屏显示 5 秒并显示背景(或者,此时,甚至是前景)但是当我运行它时它全屏显示 5 秒,是的,但它只是一个空白的浅灰色屏幕。
What am I doing wrong? Eventually I'm going to use an image for the background and I want to make sure I'm not screwing up somewhere.
我究竟做错了什么?最终我将使用一张图片作为背景,我想确保我没有在某处搞砸。
Thanks guys!
多谢你们!
Edit: When I add this at the end of my JMain class the font color is the same as the foreground color, but the background is always black no matter what color I change it to in the program.
编辑:当我在 JMain 类的末尾添加它时,字体颜色与前景色相同,但无论我在程序中将其更改为什么颜色,背景始终为黑色。
public void paint(Graphics g) {
g.drawString("This is gonna be awesome", 200, 200);
}
code from github
来自 github 的代码
import java.awt.*;
import javax.swing.JFrame;
public class JMain extends JFrame {
private JFrame frame = new JFrame();
public static void main(String[] args) {
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
JMain m = new JMain();
m.run(dm);
}
public void run(DisplayMode dm) {
this.getContentPane().setBackground(Color.RED);
frame.setForeground(Color.BLACK);
frame.setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
try {
s.setFullScreen(dm, this);
try {
Thread.sleep(5000);
} catch (Exception ex) {
}
} finally {
s.restoreScreen();
}
}
}
and
和
import java.awt.*;
import javax.swing.JFrame;
public class Screen {
private GraphicsDevice vc;
public Screen() {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = env.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode dm, JFrame window) {
window.setUndecorated(true);
window.setResizable(false);
vc.setFullScreenWindow(window);
if (dm != null && vc.isDisplayChangeSupported()) {
try {
vc.setDisplayMode(dm);
} catch (Exception ex) {
}
}
}
public Window getFullScreenWindow() {
return vc.getFullScreenWindow();
}
public void restoreScreen() {
Window w = vc.getFullScreenWindow();
if (w != null) {
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
回答by mKorbel
Don't extend a
JFrame
, but instead create a localJFrame
variable and use it.You can't paint the
JFrame
's background Color, but you can do this for the JFrame'scontentPane
(usually aJPanel
). An example of code that does this follows:this.getContentPane().setBackground(Color.RED);
Never use
Thread.sleep(int)
in code called on the Swing event thread, as this will completely block this thread, preventing it from performing its necessary actions of painting the GUI and interacting with the user and effectively freezing the application for as long as the thread is sleeping.Use a Swing Timerin place of
Thread.sleep(...)
不要扩展 a
JFrame
,而是创建一个局部JFrame
变量并使用它。您不能绘制
JFrame
的背景颜色,但您可以为 JFrame 的contentPane
(通常是 aJPanel
)执行此操作。执行此操作的代码示例如下:this.getContentPane().setBackground(Color.RED);
切勿
Thread.sleep(int)
在 Swing 事件线程上调用的代码中使用,因为这将完全阻塞该线程,阻止它执行绘制 GUI 和与用户交互的必要操作,并在线程休眠期间有效地冻结应用程序。使用 摇摆计时器代替
Thread.sleep(...)
回答by Ashwinee K Jha
Replace:
代替:
setBackground(Color.RED);
With:
和:
getContentPane().setBackground(Color.RED);
Separately, you should try to put graphics related code in SwingUtilities.invoke... as you can get unexpected issues if the graphics related class are used from the main thread. If you make this change make sure to avoid the sleep within SwingUtilities.invoke... as it will block your painting.
另外,您应该尝试将与图形相关的代码放在 SwingUtilities.invoke... 中,因为如果从主线程使用与图形相关的类,您可能会遇到意外问题。如果您进行此更改,请确保避免在 SwingUtilities.invoke 中休眠...因为它会阻止您的绘画。