Why is my background color not showing in JFrame?

I have two class files:

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

JMain https://gist.github.com/3020107

I try to get it to work in full screen for 5 seconds and display the background (or, at the moment, even the foreground), but when I run it, it goes into full screen for 5 seconds, yay, but it's just empty light gray screen.

What am I doing wrong? In the end, I'm going to use the image for the background, and I want to make sure that I'm not scared somewhere.

Thanks guys!

Edit: when I add this to the end of my JMain class, the font color matches the foreground color, but the background is always black, no matter what color I change 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); } } 
+4
source share
2 answers
  • Do not JFrame , but create a local JFrame variable and use it.

  • You cannot draw a background color for a JFrame , but you can do it for a JFrame contentPane (usually a JPanel ). Sample code that does the following:

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

  • Never use Thread.sleep(int) in the code caused by the Swing event stream, as this completely blocks this stream, preventing it from performing the necessary actions to draw the graphical interface and interact with the user and effectively freeze the application for the thread to sleep.

  • Use Swing Timer instead of Thread.sleep(...)

+11
source

Replace:

 setBackground(Color.RED); 

FROM

 getContentPane().setBackground(Color.RED); 

Separately, you should try to put the graphics code in SwingUtilities.invoke ... as you might get unexpected problems if the graphics class is used from the main thread. If you make this change, be sure to avoid sleeping in SwingUtilities.invoke ... as this will block your picture.

+6
source

All Articles