Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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?
0
tiger0516
Asked:
tiger0516
  • 5
  • 3
  • 2
  • +2
1 Solution
 
objectsCommented:
can you post your code
0
 
objectsCommented:
sounds like your adding/removing components without calling revalidate()
0
 
sciuriwareCommented:
Does it display correctly after the first              setVisible(true);                ??

;JOOP!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
 
tiger0516Author Commented:
           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
0
 
objectsCommented:
move your call to pack:

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

0
 
tiger0516Author Commented:
>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.
0
 
objectsCommented:
sorry didn't see the setBouinds() call, you want to move it

   frame.setBounds(10,10,800,500);
 frame.setVisible(true);
0
 
objectsCommented:
and get rid of pack() its not doing anything.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
setBounds () should be used if you have a null layout (i.e., setLayout ( null ) ;, then add your components to the content-pane).
0
 
hoomanvCommented:
I tested your code just with a little change and then everything have gone well
possibly theres somthing wrong in "new MainPane().getPane()"

import javax.swing.*;
import java.awt.*;

class Test {
    public static void main(String[] args) {
        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());      // commented by me
        frame.setLayout(new FlowLayout());
        frame.add(new JButton("test"));        // instead of frame.add(new MainPane().getPane());
        frame.setVisible(true);
    }
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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" ) ) ;
0
 
hoomanvCommented:
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now