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

我的代码不起作用:

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 

我的窗户越来越小了。 怎么解决?

好吧,你正在使用frame.setSize()frame.pack()

你应该一次使用其中一个。

使用setSize()可以给出所需的帧大小,但如果使用pack() ,它将根据其中组件的大小自动更改帧的大小。 它不会考虑您之前提到的尺寸。

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

大多数布局管理器最适合使用组件的preferredSize,并且大多数GUI最好允许它们包含的组件根据其内容或属性设置自己的preferredSize。 要最大限度地利用这些布局管理器,请在您的顶级容器(如JFrame pack()上调用pack() ,然后再将其显示为可见,这将告诉这些管理员执行操作 – 布局其组件。

通常当我需要在设置我的一个组件的大小时发挥更直接的作用时,我将覆盖getPreferredSize并让它返回一个大于super.preferredSize的Dimension(如果不是,则返回super的值)。

例如,这是我为此站点上的另一个问题创建的一个小型拖动矩形应用程序:

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