News:

Help! We're trapped in the computer, and the computer is trapped in 2008! Someone call the time police!

Main Menu

Java GUI's

Started by Hdx, March 07, 2006, 01:10:33 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Hdx

Well, I need help, how do you set the background of a JFrame?
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;

public class Form extends JFrame {
public Form() {
super("Helo homies!");
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.setSize(300, 300);
this.setVisible(true);
setBackground(Color.BLACK);
}

public void paint(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth, getHeight);
g.setColor(Color.RED);
g.drawLine(10, 0, 200, 20);

}
}

I'm trying to have a black background, and a red line.
The black background shows up, untill i start re-sizing.
then it reverts tot he defult grey-ish color.
Anyone have suggestions?
~-~(HDX)~-~
http://img140.exs.cx/img140/6720/hdxnew6lb.gif
09/08/05 - Clan SBs @ USEast
[19:59:04.000] <DeadHelp> We don't like customers.
[19:59:05.922] <DeadHelp> They're assholes
[19:59:08.094] <DeadHelp> And they're never right.

Joe

import java.awt.Color;
import javax.swing.JFrame;

public class JFrameColor extends JFrame
{
    public JFrameColor()
    {
        super("uh?")
        this.getContentPane..setBackground(Color.BLUE);
        this.setVisible(true);
    }
}
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Hdx

Compile error FTW!
Anyways, tryed it, corrrecting your misrtake, and sill nothing, whne I re-sized the edges turned white, and the line was no where in sight.
Oh well, they have a example at school i'll look at today :/
~-~(HDX)~-~
http://img140.exs.cx/img140/6720/hdxnew6lb.gif
09/08/05 - Clan SBs @ USEast
[19:59:04.000] <DeadHelp> We don't like customers.
[19:59:05.922] <DeadHelp> They're assholes
[19:59:08.094] <DeadHelp> And they're never right.

iago

Try putting a panel on.  Then make the panel (JPanel, whatever) colored. 

Ender

#4
You should be calling super.paint(g) in your paint(Graphics) method. You are overriding Container's paint(Graphics) method which renders all the lightweight AWT components. There must be something in the Container.paint(Graphics) method implementation that handles the resizing.

Joe

Oops.

import java.awt.Color;
import javax.swing.JFrame;

public class JFrameColor extends JFrame
{
    public JFrameColor()
    {
        super("uh?")
        this.getContentPane().setBackground(Color.BLUE);
        this.setVisible(true);
    }
}


Should work. Forgot the () on getContentPane().
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.