Results 1 to 5 of 5
Thread: Changing background color
- 03-12-2011, 05:33 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Changing background color
That is the code of the simple program that generates frame with some basic parameters. It's all okay except one thing: it doesn't change the background color to RED. Why is that?
Java Code:import java.awt.*; import javax.swing.*; public class FirstSwing { public static void main(String[] args) { SimpleFrame f = new SimpleFrame(); f.setBackground(Color.RED); f.showIt("my first window", 400, 300); } } class SimpleFrame extends JFrame { public SimpleFrame() { this.setSize(200,200); this.setLocation(200,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void showIt(String s) { this.setTitle(s); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void showIt(String s, int x, int y) { this.setTitle(s); this.setSize(x, y); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void hideIt() { this.setVisible(false); } public void showIt() { this.setVisible(true); } }
- 03-12-2011, 05:47 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What have you tried to solve it?
- 03-12-2011, 05:49 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
Well, the thing is that every time when I run the program, the frame IS red for a fraction of second, and then it's changed to default-grey again.
- 03-12-2011, 05:53 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What happens if in the showIt method you overload it to take a color and set the color in the method? Also, try and use print lines to print the current value of the background with getBackground()
- 03-12-2011, 05:54 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Typically you add/remove/alter and view not so much the frame as the content pane which holds all the nonmenu items.
So,
Java Code:public static void main(String[] args) { SimpleFrame f = new SimpleFrame(); f.getContentPane().setBackground(Color.RED); f.showIt("my first window", 400, 300); }
I would recommend working through a decent Tutorial rather than trying to figure out what might not be completely intuitive. The Creating a GUI With JFC/Swing section of Oracle's Tutorial is good. Others may be able to suggest something that aims more at introducing and less at being comprehensive.Last edited by pbrockway2; 03-12-2011 at 05:57 AM.
Similar Threads
-
Background color of window
By Annie George in forum NetBeansReplies: 1Last Post: 09-18-2010, 09:52 AM -
Changing text color in SWT
By ourimaler in forum SWT / JFaceReplies: 1Last Post: 06-02-2010, 01:08 PM -
background color with jpanel
By hannerz06 in forum New To JavaReplies: 6Last Post: 03-31-2010, 03:25 AM -
window background color?
By javan00b in forum New To JavaReplies: 3Last Post: 01-29-2008, 10:43 AM -
Changing the color of text
By Lang in forum New To JavaReplies: 1Last Post: 11-04-2007, 09:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks