Create a JLabel, resize it so it covers your JFrame. Right Click the JLabel, Find Icon and click on the (...) button. Pick a picture by clicking the Import to project button, then click finish. In the Navigator pane, (Bottom left by default, if disabled go to the Windows tab of your Netbeans IDE and enable it.)
using Jlable you can set Background color as well as image also.
To set the background color for JFrame:
getContentPane().setBackground(Color.YELLOW); //Whatever color
public nameOfTheClass() {
final Container c = this.getContentPane();
public void actionPerformed(ActionEvent e) {
c.setBackground(Color.white);
}
}
Retrieve the content pane for the frame and use the setBackground() method inherited from Component to change the color.
Example:
myJFrame.getContentPane().setBackground( desiredColor );
using:
setBackground(Color.red);
doesn't work properly.
use
Container c = JFrame.getContentPane();
c.setBackground(Color.red);
or
myJFrame.getContentPane().setBackground( Color.red );
you can override the paint method of JFrame and then fill that by your favorite color like this:
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
You can use a container like so:
Container c = JFrame.getContentPane();
c.setBackground(Color.red);
You must of course import java.awt.Color
for the red color constant.
To set the background color for the JFrame try this:
this.getContentPane().setBackground(Color.white);
Probably the SIMPLEST method is this:
super.setBackground(Color.CYAN);
You must extend JFrame in the class before doing this!
Resurrecting a thread from stasis.
In 2018 this solution works for Swing/JFrame in NetBeans (should work in any IDE :):
this.getContentPane().setBackground(Color.GREEN);
This is the simplest and the correct method. All you have to do is to add this code after initComponents();
getContentPane().setBackground(new java.awt.Color(204, 166, 166));
That is an example RGB color, you can replace that with your desired color. If you dont know the codes of RGB colors, please search on internet... there are a lot of sites that provide custom colors like this.
Hello There I did have the same problem and after many attempts I found that the problem is that you need a Graphics Object to be able to draw, paint(setBackgroundColor).
My code usually goes like this:
import javax.swing.*;
import java.awt.*;
public class DrawGraphics extends JFrame{
public DrawGraphics(String title) throws HeadlessException {
super(title);
InitialElements();
}
private void InitialElements(){
setSize(300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// This one does not work
// getContentPane().setBackground(new Color(70, 80, 70));
}
public void paint(Graphics draw){
//Here you can perform any drawing like an oval...
draw.fillOval(40, 40, 60, 50);
getContentPane().setBackground(new Color(70,80,70));
}
}
The missing part on almost all other answers is where to place the code. Then now you know it goes in paint(Graphics G)
import java.awt.*;
import javax.swing.*;
public class MySimpleLayout extends JFrame {
private Container c;
public MySimpleLayout(String str) {
super(str);
c=getContentPane();
c.setLayout(null);
c.setBackground(Color.WHITE);
}
}
Here's another method:
private void RenkMouseClicked(java.awt.event.MouseEvent evt) {
renk = JColorChooser.showDialog(null, "Select the background color",
renk);
Container a = this.getContentPane();
a.setBackground(renk);
}
I'm using netbeans ide. For me, JFrame.getContentPane()
didn't run. I used JFrame.getContentPane()
's class equivalent this.getContentPane
.
I had trouble with changing the JFrame background as well and the above responses did not solve it entirely. I am using Eclipse. Adding a layout fixed the issue.
public class SampleProgram extends JFrame {
public SampleProgram() {
setSize(400,400);
setTitle("Sample");
getContentPane().setLayout(new FlowLayout());//specify a layout manager
getContentPane().setBackground(Color.red);
setVisible(true);
}
You can use this code block for JFrame background color.
JFrame frame = new JFrame("Frame BG color");
frame.setLayout(null);
frame.setSize(1000, 650);
frame.getContentPane().setBackground(new Color(5, 65, 90));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
Source: Stackoverflow.com