ben_g
LV7 Elite (Next: 700)
     
Offline
Gender: 
Last Login: Today at 01:31:43
Date Registered: 08 May, 2011, 21:03:27
Location: (ix+$43)
Posts: 645
Topic starter
Total Post Ratings: +74
|
 |
« on: 16 July, 2011, 16:52:48 » |
0
|
I'm trying to set the background color of an undecorated JFrame to black, but the SetBackground methot does nothing. My Window creation code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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?
|
|
|
|
|
Logged
|
From when I saw this image, I never lost the game again!
|
|
|
|
Munchor
|
 |
« Reply #1 on: 16 July, 2011, 17:19:20 » |
+1
|
1 2 3 4 5 6 7 8 9 10 11 12
| 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: 1 2 3 4 5 6 7 8 9 10 11 12 13
| 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: 16 July, 2011, 17:20:37 by ephan »
|
Logged
|
|
|
|
ben_g
LV7 Elite (Next: 700)
     
Offline
Gender: 
Last Login: Today at 01:31:43
Date Registered: 08 May, 2011, 21:03:27
Location: (ix+$43)
Posts: 645
Topic starter
Total Post Ratings: +74
|
 |
« Reply #2 on: 16 July, 2011, 18:39:34 » |
0
|
Thanks. It works now.
|
|
|
|
|
Logged
|
From when I saw this image, I never lost the game again!
|
|
|
|
Munchor
|
 |
« Reply #3 on: 24 July, 2011, 15:52:41 » |
0
|
Thanks. It works now.
In fact, I even made a post on it on my blog, here. I'm glad to help 
|
|
|
|
|
Logged
|
|
|
|
|