Change background color of a JFrame
Use frame.getContentPane().setBackground(Color.BLACK);
to set the color.
How change background color of JFrame
Try this one. Draw a rectangle filled with your background color.
- use
SwingUtilities.invokeLater()
orEventQueue.invokeLater()
- use
paintComponent()
method for custom painting - don't draw on
JFrame
directly. - use
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
to close theJFrame
Sample code:
public class MyFrame extends JFrame {
public MyFrame() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
setSize(new Dimension(600, 600));
setResizable(false);
JPanel panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.clearRect(0, 0, 600, 600);
Color prevColor = g.getColor();
g.setColor(Color.BLUE); // background color
g.fillRect(0, 0, 600, 600); // fill a rectangle with background color
g.setColor(prevColor);
// your custom painting
...
}
};
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
});
}
}
How to change the background color of a JFrame?
You'll need to go through getContentPane
to change the frame's background color:
frame.getContentPane().setBackground(Color.BLACK);
Setting the background color of JFrame isn't working
The JPanel
you're adding to the frame is blocking the background color. Either set the panel's background color via panel.setBackground
or make the panel transparent by setting panel.setOpaque(false)
.
How to change the background color of a JFrame dynamically?
Could you please make an example?
Below is an example switching from blue to green and green to blue. There's bug with the first click to green. And it's probably not the most robust code, but It's just an example that I'm too lazy to improve on right now. You can play with it
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ChangeColor {
public ChangeColor() {
JFrame frame = new JFrame();
frame.add(new ColorPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class ColorPanel extends JPanel {
private static final int DELAY = 30;
private static final int INCREMENT = 5;
private Color currentColor = Color.BLUE;
boolean isBlue = true;
boolean isGreen = false;
private int r,g,b;
private Timer timer = null;
private JButton greenButton = null;
private JButton blueButton = null;
public ColorPanel() {
r = 0; g = 0; b = 255;
greenButton = createGreenButton();
blueButton = createBlueButton();
timer = new Timer(DELAY, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isBlue) {
if (b == 0) {
stopTimer();
enableButtons();
} else {
blueToGreen();
setColor(new Color(r, b, g));
}
}
if (isGreen) {
if (g == 0) {
stopTimer();
enableButtons();
} else {
greenToBlue();
setColor(new Color(r, b, g));
}
}
repaint();
}
});
add(blueButton);
add(greenButton);
}
public JButton createBlueButton() {
JButton button = new JButton("BLUE");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (currentColor != new Color(0, 255, 0)) {
System.out.println("turn blue");
isBlue = true;
isGreen = false;
diableButtons();
startTimer();
}
}
});
return button;
}
public void diableButtons() {
blueButton.setEnabled(false);
greenButton.setEnabled(false);
}
public void enableButtons() {
blueButton.setEnabled(true);
greenButton.setEnabled(true);
}
public JButton createGreenButton() {
JButton button = new JButton("GREEN");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (currentColor != new Color(0, 0, 255)) {
System.out.println("turn green");
isGreen = true;
isBlue = false;
diableButtons();
startTimer();
}
}
});
return button;
}
private void blueToGreen() {
b -= INCREMENT;
g += INCREMENT;
}
private void greenToBlue() {
g -= INCREMENT;
b += INCREMENT;
}
public void setColor(Color color) {
this.currentColor = color;
}
public void startTimer() {
timer.start();
}
public void stopTimer() {
timer.stop();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(currentColor);
g.fillRect(0, 0, getWidth(), getHeight());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new ChangeColor();
}
});
}
}
How do I set background colour and drawString without cancelling the other out in Java?
There's a few ways you can do this...
You could..
Make the Test
panel the content panel for the frame...
JFrame jf = new JFrame();
jf.setContentPane(new Test());
jf.getContentPane().setBackground(Color.YELLOW);
jf.setSize(1920, 1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
You could...
Make the Test
panel transparent
JFrame jf = new JFrame();
Test test = new Test();
test.setOpaque(false);
jf.getContentPane().setBackground(Color.YELLOW);
jf.add(test);
jf.setSize(1920, 1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
You could...
Just set the background color of the Test
panel
JFrame jf = new JFrame();
Test test = new Test();
test.setBackground(Color.YELLOW);
jf.add(test);
jf.setSize(1920, 1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Regardless of which you do...
You Should...
- Call the
super
method of thepaint
method - Prefer
paintComponent
ofpaint
- it's just a lot safer - Only manipulate the UI from the context of the Event Dispatching Thead
For example...
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Serif", Font.PLAIN, 500);
g.setFont(font);
g.setColor(Color.red);
g.drawString("Hello", 300, 900);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame jf = new JFrame();
jf.setContentPane(new Test());
jf.getContentPane().setBackground(Color.YELLOW);
jf.setSize(1920, 1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
Related Topics
Why am I Getting Inputmismatchexception
Eclipse 2021-09 Code Completion Not Showing All Methods and Classes
Returning Null as an Int Permitted with Ternary Operator But Not If Statement
Convert Java.Util.Date to What "Java.Time" Type
Which Java Collection Should I Use
How to Get the Current Time in Yyyy-Mm-Dd Hh:Mi:Sec.Millisecond Format in Java
The Performance Impact of Using Instanceof in Java
Using Pairs or 2-Tuples in Java
Abstract Class VS Interface in Java
Convert Latitude/Longitude Point to a Pixels (X,Y) on Mercator Projection
What Are Classes, References, and Objects
Build Eclipse Java Project from Command Line
Spring Boot CSS Showing Up Blank/Not Loading After Trying Everything
Why Java Classes Do Not Inherit Annotations from Implemented Interfaces
Natural Sort Order String Comparison in Java - Is One Built In