Setting Background Color for a Jframe

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() or EventQueue.invokeLater()
  • use paintComponent() method for custom painting
  • don't draw on JFrame directly.
  • use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) to close the JFrame

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 the paint method
  • Prefer paintComponent of paint - 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



Leave a reply



Submit