8
votes

i have created a JFrame in netbeans. But when i run the program, the Jframe size is too small. here is my code.

import javax.swing.JFrame;    

public class Window {

    private static void demo()
    {
        JFrame frame =new JFrame();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
       javax.swing.SwingUtilities.invokeLater(new Runnable(){
      public void run()
      {
          demo();
      }
        });
    }
}

And the output window looks like this.

5
Swing JComponents by default accepting only PreferredSize,mKorbel
"frame size is too small" Put components in it, then call pack(). The frame will become the smallest size it needs to be, in order to display the components inside it.Andrew Thompson

5 Answers

9
votes

You can use frame.setSize(width, height) in order to set its size or frame.setBounds(x, y, width, height) for setting both the location and size.

A better choice would be to call frame.pack() after you add some components to its content pane.

2
votes

Try this way...

Use the setSize(width, height) method of JFrame.

public class Myframe extends JFrame{

  public Myframe(){    
      this.setSize(300,300);    
  }

public static void main(String[] args){

    EventQueue.invokeLater(new Runnable(){

          public void run(){          
            Myframe f = new Myframe("Frame");
            f.setVisible(true);     
          }
      }); 
    }    
}
0
votes

If you want to maximize it you could try

 this.setVisible(false);
    this.setExtendedState(MAXIMIZED_BOTH);
    this.setVisible(true);
   this.setResizable(false);

Else for some specific size use

this.setSize(width,height);
0
votes

You just need to add one line line so that you can give your frame a size.The line is

frame.setSize(300,300);

Here is the full code:

import javax.swing.JFrame;    

public class Window {

private static void demo()
{
    JFrame frame =new JFrame();
    frame.setSize(300,300);
    frame.setVisible(true);
}
public static void main(String[] args) {
   javax.swing.SwingUtilities.invokeLater(new Runnable(){
  public void run()
  {
      demo();
  }
    });
}
}
-2
votes

You must have a public component method like

public gestion_name_of_class() {
    initComponents();
}