为什么我的背景颜色不会在 JFrame 中显示? [英] Why won't my background color display in JFrame?

查看:54
本文介绍了为什么我的背景颜色不会在 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);
    }
}

推荐答案

  1. 不要扩展 JFrame,而是创建一个本地 JFrame 变量并使用它.

  1. Don't extend a JFrame, but instead create a local JFrame 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 中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆