newbie_coder
asked on
EASY: Set Container Color, How?
How come this doesn't work?
Container c = getContentPane();
c.setBackground(Color.whit e);
It still displays an ugly gray colored background.
Help?
Container c = getContentPane();
c.setBackground(Color.whit
It still displays an ugly gray colored background.
Help?
try this:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame {
JPanel contentPane;
public Frame1(){
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null );
this.setDefaultCloseOperat ion(3);
this.setSize(new Dimension(400, 300));
contentPane.setBackground( Color.whit e);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame {
JPanel contentPane;
public Frame1(){
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null
this.setDefaultCloseOperat
this.setSize(new Dimension(400, 300));
contentPane.setBackground(
}
}
Oh man, i am few minutes late! ha :)
ASKER
It works using a panel, but I have shapes and objects to add to the container before and it shoulds up, but after I add the panel you suggested, nothing shows up but a white window.
========================== ========
public class Frame1 extends JFrame
{
JPanel c;
public Frame1()
{
c = (JPanel) this.getContentPane();
c.setLayout(null);
this.setDefaultCloseOperat ion(3);
this.setSize(new Dimension(400,600));
c.setBackground(Color.whit e);
c.add(new MyCircles());
}
}
class MyCircles extends JPanel
{
public void paintComponent(Graphics g)
{
// Contains shapes and objects, unrelated to this question
}
}
========================== ========
So yeah, for the above, if I just have a regular container, it'll show all my objects in the MyCircles class, but with a gray background. Adding the panel you suggested, all the shapes disappears.
:o(
==========================
public class Frame1 extends JFrame
{
JPanel c;
public Frame1()
{
c = (JPanel) this.getContentPane();
c.setLayout(null);
this.setDefaultCloseOperat
this.setSize(new Dimension(400,600));
c.setBackground(Color.whit
c.add(new MyCircles());
}
}
class MyCircles extends JPanel
{
public void paintComponent(Graphics g)
{
// Contains shapes and objects, unrelated to this question
}
}
==========================
So yeah, for the above, if I just have a regular container, it'll show all my objects in the MyCircles class, but with a gray background. Adding the panel you suggested, all the shapes disappears.
:o(
You have to decide whether you want your painting to occur on the frame or on the MyCircles panel. The latter would make you code more reusable.
I get a white background when I run the code you posted above. Check that it's not the MyCircles class that is painting the grey background.
try:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame
{
JPanel c;
MyCircles cir = new MyCircles();
public Frame1()
{
c = (JPanel) this.getContentPane();
c.setLayout(null);
this.setDefaultCloseOperat ion(3);
this.setSize(new Dimension(600,600));
cir.setBounds(new Rectangle(10,10,500,500));
c.setBackground(Color.whit e);
c.add(new MyCircles());
c.add(cir);
}
}
-------------------------- ---------- ---------- ---------- --
import java.awt.*;
import javax.swing.JPanel;
public class MyCircles extends JPanel {
public void paintComponent(Graphics g)
{
g.drawOval(20,20,50,50);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame
{
JPanel c;
MyCircles cir = new MyCircles();
public Frame1()
{
c = (JPanel) this.getContentPane();
c.setLayout(null);
this.setDefaultCloseOperat
this.setSize(new Dimension(600,600));
cir.setBounds(new Rectangle(10,10,500,500));
c.setBackground(Color.whit
c.add(new MyCircles());
c.add(cir);
}
}
--------------------------
import java.awt.*;
import javax.swing.JPanel;
public class MyCircles extends JPanel {
public void paintComponent(Graphics g)
{
g.drawOval(20,20,50,50);
}
}
ASKER
Okay, that works, but all my circles and shapes are filled with gray colors, which means it didn't really work, I could fill the objects with white, but that would be cheating and the code would be longer.
Ahhh, I thought this would be easy! I guess I'll increase the points.
Ahhh, I thought this would be easy! I guess I'll increase the points.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>all my circles and shapes are filled with gray colors
??
i tried the codes and the circles is filled with white color.
so, just try objects's suggestion :)
??
i tried the codes and the circles is filled with white color.
so, just try objects's suggestion :)
question: are u trying to draw shapes on white color background?
do u mind post your MyCircles class coding?
do u mind post your MyCircles class coding?
ASKER
Nevermind!
I added:
g.setColor(Color.white);
g.fillRect(0,0,400,600);
before all my other circles and objects, now it displays a white background, and I can keep my container. Thanks for your help anyways!
I added:
g.setColor(Color.white);
g.fillRect(0,0,400,600);
before all my other circles and objects, now it displays a white background, and I can keep my container. Thanks for your help anyways!
As I said in my previous comment:
> get a white background when I run the code you posted
> above. Check that it's not the MyCircles class that is
> painting the grey background.
> get a white background when I run the code you posted
> above. Check that it's not the MyCircles class that is
> painting the grey background.
alright then :)
newbie_coder:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
Container c = getContentPane();
JPanel panel = new JPanel();
panel.setBackground(Color.
c.add(panel);