Link to home
Start Free TrialLog in
Avatar of tiger0516
tiger0516

asked on

Weird frame behavior

I am not sure why, but when I run the a JFrame program, nothing displays. I have to resize the window, then everything displays correctly.

I have used pack(), setBounds(), setVisiable()

Where do I make mistakes?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

can you post your code
sounds like your adding/removing components without calling revalidate()
Avatar of sciuriware
sciuriware

Does it display correctly after the first              setVisible(true);                ??

;JOOP!
>> nothing displays

Meaning that the JFrame is visible (full-size) with no components in it? Or that you see only one tiny window and then you have to resize it? If its the second case, you have probably not set the size of the JFrame itself using setSize ( width, height ) ; like say, setSize ( 800, 600 ) ; Try setting it in the constructor after you add all components to the content-pane. If its the first case - what layout are you using? If it is null layout, try adding a new JLabel ( "" ) to the content-pane after you add everything else.
Avatar of tiger0516

ASKER

           JFrame frame=new JFrame();
            frame.pack();
                                //frame.setSize(800,500);
            frame.setBounds(10,10,800,500);
            frame.setTitle("Assignment 3");
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
            frame.setJMenuBar(new MenuBar().createMenuBar());
            frame.setLayout(new FlowLayout());
                                frame.add(new MainPane().getPane());
            frame.setVisible(true);

>Meaning that the JFrame is visible (full-size) with no components in it?
Yes. Full size, nothing displays; have to resize the window(just a little bit) and then everything shows up
move your call to pack:

    frame.pack();
    frame.setVisible(true);

>move your call to pack:

>    frame.pack();
>    frame.setVisible(true);

It brings a window which is minimized. Yes, after I maxmize the window, everything shows up correctly, but there is no difference with my previous case, which also requires me to resize the window.
sorry didn't see the setBouinds() call, you want to move it

   frame.setBounds(10,10,800,500);
 frame.setVisible(true);
and get rid of pack() its not doing anything.
setBounds () should be used if you have a null layout (i.e., setLayout ( null ) ;, then add your components to the content-pane).
ASKER CERTIFIED SOLUTION
Avatar of hoomanv
hoomanv
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> frame.add(new MainPane().getPane());

Perhaps it should be frame.getContentPane ().add ( new MainPane ().getPane () ) ;

>> frame.add(new JButton("test"));        

Should also be frame.getContentPane ().add ( new JButton ( "test" ) ) ;
As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

       frame.add(child);
 
And the child will be added to the contentPane