Results 1 to 5 of 5
  1. #1
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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);
    	}
    	
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What have you tried to solve it?

  3. #3
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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()

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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

  1. Background color of window
    By Annie George in forum NetBeans
    Replies: 1
    Last Post: 09-18-2010, 09:52 AM
  2. Changing text color in SWT
    By ourimaler in forum SWT / JFace
    Replies: 1
    Last Post: 06-02-2010, 01:08 PM
  3. background color with jpanel
    By hannerz06 in forum New To Java
    Replies: 6
    Last Post: 03-31-2010, 03:25 AM
  4. window background color?
    By javan00b in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 10:43 AM
  5. Changing the color of text
    By Lang in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 09:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •