Why won't my background color display in JFrame? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-09 18:36:30

问题


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:


  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(...)




回答2:


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.



来源:https://stackoverflow.com/questions/11268357/why-wont-my-background-color-display-in-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!