Results 1 to 4 of 4
  1. #1
    Doodayer is offline Member
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default jframe not drawing background color

    Hi my name is Doodayer. I've just started working on a program and i'm already having difficulties with the program. I can't get the background color to change from the basic gray color. Maybe you can tell me what i'm doing wrong.
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    import java.awt.*;
    import java.lang.Object;
    import java.awt.Color;
     
    public class FrameDemo{
     
    public static void main(String args[]){
     
    	Toolkit toolkit =  Toolkit.getDefaultToolkit ();
    	Dimension dim = toolkit.getScreenSize();	
     
    	JFrame myFrame = new JFrame("Xapper");
    	myFrame.setSize(800,600);
    	myFrame.setAlwaysOnTop(true);
    	myFrame.setBackground(Color.RED);
    	myFrame.setLocation(dim.width/2-400, dim.height/2-300);
    	myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	myFrame.setVisible(true);
    }
     
    }
    also if theres any way to shorten up what I have there, that would be great.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: jframe not drawing background color

    First, to set the background color you need to do it via the content pane.
    Java Code:
    myFrame.getContentPane().setBackground(Color.red);
    And you don't need to compute the location of the frame on the screen. If you use
    Java Code:
    myFrame.setLocationRelativeTo(null);
    It will center it for you.

    Finally, use JFrame.EXIT_ON_CLOSE as the close option. JFrame implements the WindowConstants
    interface so you may as well take advantage of it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Doodayer is offline Member
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default Re: jframe not drawing background color

    thanks for the help! is there a simpler way to import all of these?

    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    import java.awt.*;
    import java.lang.Object;
    import java.awt.Color;

    can I use one simple method to import all these?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: jframe not drawing background color

    Not really. You can use a * but that is only for current level, not recursively into a package. Of course
    you don't need WindowConstants or awt.* (note java.lang is implicitly imported). If you get an
    IDE they will usually import them for you.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. New to Java, background color help
    By Estvan in forum New To Java
    Replies: 4
    Last Post: 10-22-2012, 05:03 AM
  2. How to change background color
    By Jeffrey4u in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 10-22-2011, 09:32 AM
  3. JFrame window doesnt change background color
    By Addez in forum New To Java
    Replies: 7
    Last Post: 11-07-2009, 10:38 PM
  4. setting background color of JFrame form with NetBeans 6.1
    By onefootswill in forum New To Java
    Replies: 4
    Last Post: 08-12-2008, 08:02 AM
  5. window background color?
    By javan00b in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 11:43 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
  •