Just how do you set the background color for a JFrame?
asked Jul 4, 2009 at 4:26
Retrieve the content pane for the frame and use the setBackground() method inherited from Component to change the color.
Example:
myJFrame.getContentPane().setBackground( desiredColor );
answered Jul 4, 2009 at 4:30
Brandon E TaylorBrandon E Taylor
24.6k6 gold badges46 silver badges70 bronze badges
To set the background color for JFrame:
getContentPane().setBackground(Color.YELLOW); //Whatever color
answered Jul 4, 2009 at 4:33
using:
setBackground(Color.red);
doesn’t work properly.
use
Container c = JFrame.getContentPane();
c.setBackground(Color.red);
or
myJFrame.getContentPane().setBackground( Color.red );
Reins
1,1091 gold badge17 silver badges35 bronze badges
answered Sep 12, 2015 at 8:07
darxdarx
1491 silver badge2 bronze badges
2
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.
answered Jul 21, 2017 at 22:49
To set the background color for the JFrame try this:
this.getContentPane().setBackground(Color.white);
luk2302
53.3k23 gold badges96 silver badges136 bronze badges
answered Jan 11, 2017 at 10:24
Mr. JMr. J
711 silver badge1 bronze badge
2
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)
answered Aug 15, 2015 at 2:20
T04435T04435
11.5k4 gold badges52 silver badges53 bronze badges
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.
Ankur
2,17123 silver badges29 bronze badges
answered Jul 4, 2009 at 4:32
John TJohn T
23.5k11 gold badges56 silver badges82 bronze badges
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
.
demongolem
9,33036 gold badges89 silver badges104 bronze badges
answered Nov 25, 2009 at 2:11
0
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());
}
answered Mar 8, 2015 at 5:48
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);
}
answered Jul 28, 2020 at 19:42
DebraDebra
191 bronze badge
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);
answered Jan 9, 2021 at 5:36
public nameOfTheClass() {
final Container c = this.getContentPane();
public void actionPerformed(ActionEvent e) {
c.setBackground(Color.white);
}
}
answered Dec 11, 2017 at 18:35
3
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);
}
}
Stephan
41.1k62 gold badges237 silver badges324 bronze badges
answered Nov 29, 2012 at 13:13
1
Probably the SIMPLEST method is this:
super.setBackground(Color.CYAN);
You must extend JFrame in the class before doing this!
answered Mar 30, 2013 at 4:11
Red_HatRed_Hat
751 silver badge7 bronze badges
2
I am trying to make a planner and want to change the background of my JFrame.
I have already tried frame.getContentPane().setBackground(Color.);
but that doesn’t seem to work.
Here is the code for the frame portion
`public Planner(){
frame = new JFrame();
main = new JPanel();
menu = new Menu(this);
frame.setPreferredSize(preferredSize);
frame.add(main);
frame.setJMenuBar(menu);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Myva");
JLabel loading = new JLabel();
JOptionPane pane = new JOptionPane();
pane.showMessageDialog( null, "Hi. ");
name = pane.showInputDialog("What is your name:");
}
Thanks in advance
asked Aug 23, 2015 at 2:16
0
If I understand your question, then you could call JFrame.setBackground(Color)
like
frame.setBackground(Color.BLUE);
If you want to change the color in a more visible manner, you can make it on a JPanel
. Like,
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 480);
frame.setVisible(true);
}
which will give you a very BLUE
window.
answered Aug 23, 2015 at 2:19
Elliott FrischElliott Frisch
195k20 gold badges157 silver badges246 bronze badges
2
To change the color of your JFrame, use the following code:
frame.setBackground(Color.BLUE);
(Color doesn’t have to be blue, I just used it as an example)
answered Aug 23, 2015 at 2:19
Wyatt LoweryWyatt Lowery
5131 gold badge7 silver badges21 bronze badges
I am trying to make a planner and want to change the background of my JFrame.
I have already tried frame.getContentPane().setBackground(Color.);
but that doesn’t seem to work.
Here is the code for the frame portion
`public Planner(){
frame = new JFrame();
main = new JPanel();
menu = new Menu(this);
frame.setPreferredSize(preferredSize);
frame.add(main);
frame.setJMenuBar(menu);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Myva");
JLabel loading = new JLabel();
JOptionPane pane = new JOptionPane();
pane.showMessageDialog( null, "Hi. ");
name = pane.showInputDialog("What is your name:");
}
Thanks in advance
asked Aug 23, 2015 at 2:16
0
If I understand your question, then you could call JFrame.setBackground(Color)
like
frame.setBackground(Color.BLUE);
If you want to change the color in a more visible manner, you can make it on a JPanel
. Like,
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 480);
frame.setVisible(true);
}
which will give you a very BLUE
window.
answered Aug 23, 2015 at 2:19
Elliott FrischElliott Frisch
195k20 gold badges157 silver badges246 bronze badges
2
To change the color of your JFrame, use the following code:
frame.setBackground(Color.BLUE);
(Color doesn’t have to be blue, I just used it as an example)
answered Aug 23, 2015 at 2:19
Wyatt LoweryWyatt Lowery
5131 gold badge7 silver badges21 bronze badges
VaneKKrasav4ik 0 / 0 / 0 Регистрация: 15.05.2014 Сообщений: 14 |
||||
1 |
||||
Цвет фона окна18.05.2015, 20:55. Показов 17968. Ответов 1 Метки нет (Все метки)
Как задать цвет окну ?
__________________
0 |
Pablito 2882 / 2294 / 769 Регистрация: 12.05.2014 Сообщений: 7,978 |
||||
18.05.2015, 21:48 |
2 |
|||
0 |