如何在Java Swing中设置特定的窗口(框架)大小?

37

我的代码无法运行:

JFrame frame = new JFrame("mull");

mull panel = new mull();

// add panel to the center of window
frame.getContentPane().add("Center", panel);
frame.setSize(500, 300); // << not working!!!
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); // give a suitable size to window automatically
frame.setVisible(true); // make window visible

我得到了一个非常小的窗口。如何修复它?


2
如何在Java Swing中设置特定的窗口(框架)大小?更常见的情况是您希望设置框架的内容面板或者内容面板中的组件的首选大小。假设知道如何使框架本身变大,以及从中如何排列或呈现内容,这样做会带来很多问题。 - Andrew Thompson
3个回答

75

好的,你正在同时使用frame.setSize()frame.pack()

你应该在同一时间只使用其中一个。

使用setSize()可以设置窗口的大小,但如果你使用pack(),它将根据窗口中组件的大小自动调整窗口的大小。它不会考虑你之前设置的大小。

尝试从代码中删除frame.pack()或者将其放在设置大小之前,然后运行它。


4
pack() 放在 setSize() 之前,但是不要移除它!对于正确布局GUI而言,调用pack()(框架)或validate()(小程序)是必要的。或者更好的做法是,向框架添加一些内容,以便布局管理器可以在打包时计算出它应该有多大(并移除任何设置大小、首选大小、最大或(可能甚至)最小大小的调用)。-1 - Andrew Thompson

9
大多数布局管理器都最适合使用组件的preferredSize,而大多数GUI最好允许包含的组件根据其内容或属性设置自己的preferredSizes。为了最大限度地利用这些布局管理器,请在使它们可见之前调用pack()在您的顶级容器(如JFrames)上,因为这将告诉这些管理器执行它们的操作--布置它们的组件。
通常情况下,当我需要更直接地设置一个组件的大小时,我会重写getPreferredSize并返回一个比super.preferredSize大的Dimension(如果没有,则返回超级值)。
例如,这里是一个我为这个网站上的另一个问题创建的小拖动矩形应用程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MoveRect extends JPanel {
   private static final int RECT_W = 90;
   private static final int RECT_H = 70;
   private static final int PREF_W = 600;
   private static final int PREF_H = 300;
   private static final Color DRAW_RECT_COLOR = Color.black;
   private static final Color DRAG_RECT_COLOR = new Color(180, 200, 255);
   private Rectangle rect = new Rectangle(25, 25, RECT_W, RECT_H);
   private boolean dragging = false;
   private int deltaX = 0;
   private int deltaY = 0;

   public MoveRect() {
      MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
      addMouseListener(myMouseAdapter);
      addMouseMotionListener(myMouseAdapter);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (rect != null) {
         Color c = dragging ? DRAG_RECT_COLOR : DRAW_RECT_COLOR;
         g.setColor(c);
         Graphics2D g2 = (Graphics2D) g;
         g2.draw(rect);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private class MyMouseAdapter extends MouseAdapter {

      @Override
      public void mousePressed(MouseEvent e) {
         Point mousePoint = e.getPoint();
         if (rect.contains(mousePoint)) {
            dragging = true;
            deltaX = rect.x - mousePoint.x;
            deltaY = rect.y - mousePoint.y;
         }
      }

      @Override
      public void mouseReleased(MouseEvent e) {
         dragging = false;
         repaint();
      }

      @Override
      public void mouseDragged(MouseEvent e) {
         Point p2 = e.getPoint();
         if (dragging) {
            int x = p2.x + deltaX;
            int y = p2.y + deltaY;
            rect = new Rectangle(x, y, RECT_W, RECT_H);
            MoveRect.this.repaint();
         }
      }
   }

   private static void createAndShowGui() {
      MoveRect mainPanel = new MoveRect();

      JFrame frame = new JFrame("MoveRect");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

请注意,我的主类是一个JPanel,并且我重写了JPanel的getPreferredSize方法:
public class MoveRect extends JPanel {
   //.... deleted constants

   private static final int PREF_W = 600;
   private static final int PREF_H = 300;

   //.... deleted fields and constants

   //... deleted methods and constructors

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

还要注意的是,当我展示我的GUI时,我会将它放入一个JFrame中,调用pack(); 在JFrame上,设置它的位置,然后调用setVisible(true); 在我的JFrame上:

   private static void createAndShowGui() {
      MoveRect mainPanel = new MoveRect();

      JFrame frame = new JFrame("MoveRect");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

0

试试这个,但你可以用bounds调整框架大小并编辑标题。

package co.form.Try;

import javax.swing.JFrame;

public class Form {

    public static void main(String[] args) {
        JFrame obj =new JFrame();
        obj.setBounds(10,10,700,600); 
        obj.setTitle("Application Form");
        obj.setResizable(false);                
        obj.setVisible(true);       
        obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接