Author Topic: Java GUI's  (Read 3310 times)

0 Members and 1 Guest are viewing this topic.

Offline Hdx

  • The Hdx!
  • Full Member
  • ***
  • Posts: 311
  • <3 Java/Cpp/VB/QB
    • View Profile
Java GUI's
« on: March 07, 2006, 01:10:33 am »
Well, I need help, how do you set the background of a JFrame?
Code: [Select]
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.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Java GUI's
« Reply #1 on: March 07, 2006, 08:48:44 am »
Code: [Select]
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);
    }
}
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Hdx

  • The Hdx!
  • Full Member
  • ***
  • Posts: 311
  • <3 Java/Cpp/VB/QB
    • View Profile
Re: Java GUI's
« Reply #2 on: March 07, 2006, 10:23:13 am »
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.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Java GUI's
« Reply #3 on: March 07, 2006, 12:38:18 pm »
Try putting a panel on.  Then make the panel (JPanel, whatever) colored. 

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Java GUI's
« Reply #4 on: April 23, 2006, 09:56:31 pm »
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.
« Last Edit: April 23, 2006, 10:07:58 pm by Ender »

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Java GUI's
« Reply #5 on: May 20, 2006, 08:39:44 pm »
Oops.

Code: [Select]
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().
I'd personally do as Joe suggests

You might be right about that, Joe.