java JFrame 尺寸太小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13100103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:29:10  来源:igfitidea点击:

JFrame size is too small

javaswingjframepreferredsize

提问by Sam

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

我在 netbeans 中创建了一个 JFrame。但是当我运行程序时,Jframe 的大小太小了。这是我的代码。

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.

输出窗口看起来像这样。

回答by Dan D.

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.

您可以使用frame.setSize(width, height)来设置其大小或frame.setBounds(x, y, width, height)设置位置和大小。

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

更好的选择是frame.pack()在将一些组件添加到其内容窗格后调用。

回答by Kumar Vivek Mitra

Try this way...

试试这个方法...

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

使用JFramesetSize(width, height)方法。

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);     
          }
      }); 
    }    
}

回答by harry4

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);

回答by Insane Coder

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();
  }
    });
}
}

回答by Hafsa KHAROUBI

You must have a public component method like

您必须有一个公共组件方法,如

public gestion_name_of_class() {
    initComponents();
}