Author Topic: Setting the background of a JFrame  (Read 10169 times)

0 Members and 1 Guest are viewing this topic.

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Setting the background of a JFrame
« on: July 16, 2011, 10:52:48 am »
I'm trying to set the background color of an undecorated JFrame to black, but the SetBackground methot does nothing.

My Window creation code:
Code: [Select]
JFrame.setDefaultLookAndFeelDecorated(true);
    Loading = new JFrame("");
    Loading.setAlwaysOnTop(true);
    Loading.setLayout(null);
    Loading.setUndecorated(true);
    Loading.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
    JLabel LoadingText = new JLabel("Loading... - Please wait.");
    LoadingText.setBounds(250,0,500,15);
    LoadingText.setHorizontalAlignment((int)JFrame.CENTER_ALIGNMENT);
    LoadingText.setForeground(Color.WHITE);
    LoadingBar.setMinimum(0);
    LoadingBar.setMaximum(100);
    LoadingBar.setValue(0);
    LoadingBar.setForeground(Color.GREEN);
    LoadingBar.setBackground(Color.RED);
    LoadingBar.setBounds(0,30,1000,30);
    Loading.getContentPane().add(LoadingText);
    Loading.getContentPane().add(LoadingBar);
    Loading.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width/2-500,Toolkit.getDefaultToolkit().getScreenSize().height/2-100,1000,200);
    Loading.setBackground(Color.BLACK);
    Loading.setFocusable(true);
    Loading.setVisible(true);

And this is the result:


You can see that the SetBackground method hasn't done anything. Is there a way to make the background of a JFrame black?
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Setting the background of a JFrame
« Reply #1 on: July 16, 2011, 11:19:20 am »
Code: [Select]
import javax.swing.*;
import java.awt.*;

class jframtest {
        public static void main (String[] args) {
                JFrame myFrame = new JFrame();
                myFrame.setDefaultLookAndFeelDecorated(true);
                myFrame.setBackground(Color.BLACK);
                myFrame.setSize(400,400);
                myFrame.setVisible(true);
        }
}

I tried that and it didn't work either, so I investigated on it and found out:

The method "JFrame.setBackground()" has been deprecated on Swing.

In other words, everything about the JFrame relating to it being a container has been rendered void of use. Instead, you have to call javax.swing.JFrame.getContentPane() and use that container.

I tried this and it worked:

Code: [Select]
import javax.swing.*;
import java.awt.*;

class javaframetest {
        public static void main (String[] args) {
                JFrame myFrame = new JFrame();
                myFrame.setDefaultLookAndFeelDecorated(true);
                myFrame.getContentPane().setBackground(Color.BLACK);
                myFrame.setSize(400,400);
                myFrame.setVisible(true);
        }
}
« Last Edit: July 16, 2011, 11:20:37 am by ephan »

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: Setting the background of a JFrame
« Reply #2 on: July 16, 2011, 12:39:34 pm »
Thanks. It works now.
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Setting the background of a JFrame
« Reply #3 on: July 24, 2011, 09:52:41 am »
Thanks. It works now.

In fact, I even made a post on it on my blog, here.

I'm glad to help :)