JFrame
is a composite component, it's actually made up of a number of other components

(From How to Use Root Panes)
So, when you call setBackground
, you're only changing the background color of the underlying frame, not the contentPane
which resides on it.
Instead, you should be using getContent().setBackground(...)
.
"But why does setForegound
work" I here you ask?
That's because you've overriding paint
of JFrame
, which is pre-configured with the foreground
property of the frame (and not the contentPane
)
Now, having said all that...
- You should avoid extending from top level containers like
JFrame
, for these types of reasons AND...
- You shouldn't override
paint
of them either. You should start with a component, extending from something like JPanel
and override it's paintComponent
method.
Have a look at:
for things that go wrong when you override paint
of top level containers, and also, they aren't double buffered, so updating them causes flickering
Example...
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Sam extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DisplayMode dm = new DisplayMode(1920, 1080, 32, 60);
Sam s = new Sam();
s.run(dm);
}
});
}
public Sam() {
add(new MainView());
}
public void run(DisplayMode dm) {
Screen s = new Screen();
s.setFullScreen(dm, this);
Timer timer = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
s.restoreScreen();
}
});
timer.start();
}
public class MainView extends JPanel {
public MainView() {
this.setBackground(Color.PINK);
this.setForeground(Color.BLACK);
setFont(new Font("Arial", Font.PLAIN, 24));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
String text = "THIS IS GUNNA BE AWESOME";
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
g.drawString(text, x, y);
}
}
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) {
ex.printStackTrace();
}
}
}
public Window getFullScreenWindow() {
return vc.getFullScreenWindow();
}
public void restoreScreen() {
Window w = vc.getFullScreenWindow();
if (w != null) {
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
}