Sign in to follow this  
VaderJ

Java JFrame windows

Recommended Posts

Hi guys. Well I am learning java and I am working on a small program that displays two different windows. One on top of the other. My problem is that I want one window to display a white background and the other black. I am following a sample program froma textbook from school. The program compiles and runs just fine, but the window colors do not change. They stay the default gray. Here is the code.
// imports
import java.awt.Color;
import javax.swing.JFrame;

public class Window
{
    
    /** Creates a new instance of Window */
    public Window()
    {
    }
    
    public static void main( String[] args )
    {
        JFrame window;
        JFrame blackWindow;
        
        window = new JFrame( "First Window" );
        window.setBounds( 50, 50, 300, 200 );
        window.setLayout( null );
        window.setBackground(Color.white);
        window.setVisible( true );

        
        blackWindow = new JFrame("Black Window");
        blackWindow.setBounds(150, 150, 250, 125);
        blackWindow.setLayout(null);
        blackWindow.setBackground(Color.black);
        blackWindow.setVisible(true);

     } // end main
}

I tried a different example using labels and the background and foreground colors changed as they should. I just don't know why the windows won't. By the way I am using Netbeans 5.0 with the JDK 5 that installed with it. Any ideas? Thanks for your help.

Share this post


Link to post
Share on other sites
Hey thanks. I never would have even thought that it would be something like that. I was just blindly following what the book was teaching me. The bad thing is is that the book was published this year(2006) and you would think that the auther would know about using contentPane(). But maybe not.

After reading the link you provided and doing a little bit more research I now have working programs. Thanks again. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this