tags:

views:

562

answers:

3
import javax.swing.*;

class Frame extends JFrame{
    Frame() {
        JFrame j = new JFrame();
        j.setBounds(100, 200, 120, 120);
        j.setTitle("null");
        j.setVisible(true);
        j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

public class test001 {

    public static void main (String Args[]){
        Frame f = new Frame();
         System.out.print("Visible = True");

        f.setVisible(false);
        System.out.print("Visible = false");
    }
}

after the setVisible(false) command. The JFrame Window still show on my desktop. How can I fix that ?

+2  A: 

You're creating another JFrame within your constructor. Assuming what you want is your Frame class to be invisible, do this:

class Frame extends JFrame {

   Frame() {
      setBounds(100, 200, 120, 120);
      setTitle("null");
      setVisible(true);
      setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   }
}

public class test001 {

   public static void main(String Args[]) {
      Frame f = new Frame();
      System.out.print("Visible = True");

      f.setVisible(false);
      System.out.print("Visible = false");
   }
}
JRL
Thanks! Problem Solved!
Much better to lose the unnecessary subclassing.
Tom Hawtin - tackline
(And you're missing the `EventQueue.invokeLater` boilerplate in `main`.)
Tom Hawtin - tackline
+1  A: 

Problem is that your main method uses different JFrame that your constructor. Your Frame constructor creates new JFrame instance (using new JFrame). When you call f.setVisible(false), it goes to your frame, but not to created JFrame.

Peter Štibraný
Ahh.. I see. Now I know whats going on. Thanks!
A: 

The problem here is that your "Frame" class instanciates a new JFrame. Calling setVisible on the Frame does not affect the JFrame which is being shown.

You can fix it by either just using a JFrame instance, or just subclassing. Don't do both.

Timothy Pratley