Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: ben_g 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: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:
(http://picturestack.com/827/743/Kj0UntitledFnd.png)
You can see that the SetBackground method hasn't done anything. Is there a way to make the background of a JFrame black?
-
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:
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);
}
}
-
Thanks. It works now.
-
Thanks. It works now.
In fact, I even made a post on it on my blog, here (http://coderecycler.wordpress.com/2011/07/17/jframe-setbackground/).
I'm glad to help :)