Why won#39;t my background color display in JFrame?(为什么我的背景颜色不会在 JFrame 中显示?)
问题描述
我有两个类文件:
屏幕https://gist.github.com/3020101
JMain https://gist.github.com/3020107
我试图让它全屏显示 5 秒并显示背景(或者,此时,甚至是前景),但是当我运行它时,它会全屏显示 5 秒,是的,但这只是一个空白浅灰色屏幕.
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.
我做错了什么?最终我将使用一张图片作为背景,并且我想确保我没有搞砸什么地方.
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.
谢谢大家!
当我在 JMain 类的末尾添加它时,字体颜色与前景色相同,但无论我在程序中将其更改为哪种颜色,背景始终为黑色.
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.
public void paint(Graphics g) {
g.drawString("This is gonna be awesome", 200, 200);
}
来自 github 的代码
code from 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();
}
}
}
和
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);
}
}
推荐答案
不要扩展
JFrame
,而是创建一个本地JFrame
变量并使用它.
Don't extend a
JFrame
, but instead create a localJFrame
variable and use it.
您不能绘制 JFrame
的背景颜色,但可以为 JFrame 的 contentPane
(通常是 JPanel代码>).执行此操作的代码示例如下:
You can't paint the JFrame
's background Color, but you can do this for the JFrame's contentPane
(usually a JPanel
). An example of code that does this follows:
this.getContentPane().setBackground(Color.RED);
this.getContentPane().setBackground(Color.RED);
永远不要在调用 Swing 事件线程的代码中使用 Thread.sleep(int)
,因为这将完全阻塞该线程,阻止它执行绘制 GUI 和只要线程处于休眠状态,就可以与用户交互并有效地冻结应用程序.
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.
使用 Swing Timer 代替Thread.sleep(...)
这篇关于为什么我的背景颜色不会在 JFrame 中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我的背景颜色不会在 JFrame 中显示?


- 未找到/usr/local/lib 中的库 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 获取数字的最后一位 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 转换 ldap 日期 2022-01-01