我创建在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();
}
});
}
}
可以使用frame.setSize(width, height)
,以便设置其尺寸或frame.setBounds(x, y, width, height)
为同时设置位置和大小。
更好的选择是调用frame.pack()
你添加一些组件,它的内容窗格后。
试试这个方法...
使用setSize(width, height)
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);
}
});
}
}
如果你想最大化你可以尝试
this.setVisible(false);
this.setExtendedState(MAXIMIZED_BOTH);
this.setVisible(true);
this.setResizable(false);
否则,对于一些特定的大小使用
this.setSize(width,height);
你只需要添加一行行,以便你可以给你的框架size.The线
frame.setSize(300,300);
下面是完整的代码:
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();
}
});
}
}
你必须有这样一个公共组件的方法
public gestion_name_of_class() {
initComponents();
}