The moose likes Swing / AWT / SWT and the fly likes Why I can't change the JWindow's color? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why I can Watch "Why I can New topic

Why I can't change the JWindow's color?

Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
Hi everyone:
I want to use Jwindow to my application. But I find that Jwindow 's background color can't be changed.
my code is:
public static void main(String[] args){JWindow jw=new JWindow();jw.setSize(150,150);jw.setBackground(new Color(23,232,23));jw.setVisible(true);JButton jb=new JButton("Test");jb.setBackground(new Color(23,34,122));jb.setContentAreaFilled(false);//jb.setSize(30,40);jb.setVisible(true);jw.getContentPane().setLayout(new BorderLayout());jw.getContentPane().add(jb);jw.setLocation(1000,800);for(int i=800;i>700;i--){jw.setLocation(1000,i);}
Second, the JButton can't be added to the Jwindow,Why?
Third , When I run the application ,the JWindow will splash one time ,and appear. I don't want it splash when the applicatio starup.How to do it?

When I face C# and Java I choose Java.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

  • JWindows background color can be changed. Set the color on the content pane and not the JWindow reference itself.
  • The JButton *is* being added to the window, but you set the window visible before you add the button. You'll either need to add the button to the window before you cann setVisible() on the JWindow, or you'll need to call validate() on the JWindow after you add the button to the already visible window.
  • I don't understand your third question...

  • -Nate
    Write once, run anywhere, because there's nowhere to hide! - /. A.C.
    Yashnoo lyo
    Ranch Hand

    Joined: Feb 17, 2003
    Posts: 152
    Thks.You are right.
    The top question are resolved.
    The third problem's meaning is that:
    I expected my window appear in the right top of my screen.But now ,When I run it ,it splash in the left top,and later appear in the right bottom, why this happen?
    Do you know MSN MEssenger? It have a mail notifier.It will appear when your mailbox have new Email.It appear in the right bottom of your screen,right? I want to do that.
    But my code will appear in the left first,and later appear in the right bottom. I don't know whether I say clearly.
    Can you try my code in your system? Thks:

    Can you try it? If you try it you may find a problem. :roll:
    Nathan Pruett

    Joined: Oct 18, 2000
    Posts: 4121

    If you don't set a location for a window it defaults to the upper left corner of the screen (0,0). You set the location to 1000, 800, this is the lower right corner of the screen (based on your screen size... see next paragraph) You then loop and move the window up the screen. It looks like you are thinking screen coordinates are based off the *lower* left corner of the screen, like normal Cartesian coordinates... nope. The origin (0,0) is in the *upper* left corner of the screen. X coordinates behave the same way, but y coordinates get *larger* as they go down the screen.

    Since you've hard coded the value (1000, 800) as the location of the window, people with different screen sizes may have problems with your code. People who are running at really high resolutions may see the window pop up closer to the middle of their screen, and people that are running at low resolution wouldn't be able to see the window at all... it would pop up off the screen entirely. You can get the screen size by calling Toolkit.getDefaultToolkit().getScreenSize(). You can make calculations off the returned screen size to make sure your code works in all screen resolutions.
    subject: Why I can't change the JWindow's color?
    Similar Threads
    Calling a simple splash screen class from main application
    why no border around the button !!
    Blank JFrame caused by creation in an Inner Class
    JLabel not displaying in JWindow splash
    Keeping a JWindow on top