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

查看:219
本文介绍了为什么会在JFrame中没有我的背景色彩显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类文件:

https://gist.github.com/3020101

https://gist.github.com/3020107

我试图让它去全屏5秒显示背景(或者,在这一点上,就连前台),但是当我运行它,它进入全屏模式,持续5秒,耶,但它仅仅是一个空白的浅灰色屏幕。

我是什么做错了吗?最后,我要使用图像作为背景,我想确保我不会搞砸了某个地方。

谢谢你们!

编辑:当我加入这个在我JMain类的末尾字体颜色是一样的前景色,但背景是黑色的始终,无论什么颜色我在计划将其更改为

 公共无效漆(图形G){
    g.drawString(这会是真棒,200,200);
}

从GitHub code

 进口java.awt中的*。
进口javax.swing.JFrame中;公共类JMain扩展的JFrame {    私人的JFrame框架=新的JFrame();    公共静态无效的主要(字串[] args){
        DISPLAYMODE DM =新DISPLAYMODE(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
        JMain M =新JMain();
        m.run(DM);
    }    公共无效的run(DISPLAYMODE DM){
        this.getContentPane()的setBackground(Color.RED)。
        frame.setForeground(Color.BLACK);
        frame.setFont(新字体(宋体,Font.PLAIN,24));
        屏幕S =新画面();
        尝试{
            s.setFullScreen(DM,这一点);
            尝试{
                视频下载(5000);
            }赶上(例外前){
            }
        } {最后
            s.restoreScreen();
        }
    }
}

 进口java.awt中的*。
进口javax.swing.JFrame中;公共类屏幕{    私人GraphicsDevice的VC;    公众网(){
        GraphicsEnvironment中ENV = GraphicsEnvironment.getLocalGraphicsEnvironment();
        VC = env.getDefaultScreenDevice();
    }    公共无效setFullScreen(DISPLAYMODE DM,JFrame的窗口){
        window.setUndecorated(真);
        window.setResizable(假);
        vc.setFullScreenWindow(窗口);
        如果(DM = NULL&放大器;!&安培; vc.isDisplayChangeSupported()){
            尝试{
                vc.setDisplayMode(DM);
            }赶上(例外前){
            }
        }
    }    公共窗口getFullScreenWindow(){
        返回vc.getFullScreenWindow();
    }    公共无效restoreScreen(){
        窗口W = vc.getFullScreenWindow();
        如果(W!= NULL){
            w.dispose();
        }
        vc.setFullScreenWindow(NULL);
    }
}


解决方案

  1. 不要延长的JFrame ,而是创建一个本地的JFrame 变量并使用它。


  2. 您无法描绘的JFrame 的背景颜色,但你可以对JFrame的的contentPane (通常是的JP​​anel )。这是否code的示例如下:

    this.getContentPane()的setBackground(Color.RED);


  3. 不要使用视频下载(INT)在$称为Swing事件线程上C $ C,因为这将完全阻止这个线程,$ P $从执行绘画GUI和与用户交互并有效地冻结只要线程休眠应用程序的必要的操作pventing它。


  4. 摇摆定时器 >视频下载(...)


I have two class files:

Screen https://gist.github.com/3020101

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.

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.

public void paint(Graphics g) {
    g.drawString("This is gonna be awesome", 200, 200);
}

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();
        }
    }
}

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);
    }
}

解决方案

  1. Don't extend a JFrame, but instead create a local JFrame variable and use it.

  2. 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);

  3. 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.

  4. Use a Swing Timer in place of Thread.sleep(...)

这篇关于为什么会在JFrame中没有我的背景色彩显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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