【问题标题】:How to set specific window (frame) size in java swing?java - 如何在java swing中设置特定的窗口(框架)大小?
【发布时间】:2012-01-01 21:19:53
【问题描述】:

我的代码不起作用:

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

我的窗口越来越小。如何解决?

【问题讨论】:

  • "如何在 java swing 中设置特定的窗口(框架)大小?" 更常见的情况是您希望设置框架内容窗格的首选大小,或者可能是内容窗格中的一个组件。假设知道框架本身有多大,以及如何安排或渲染内容,是充满麻烦的。

标签: java swing user-interface


【解决方案1】:

嗯,你同时使用了frame.setSize()frame.pack()

您应该一次使用其中之一。

使用setSize()你可以给你想要的框架大小,但是如果你使用pack(),它会根据其中组件的大小自动改变框架的大小。它不会考虑你前面提到的大小。

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

【讨论】:

  • pack() 移动到setSize() 之前,但不要 将其删除!需要调用pack()(帧)或validate()(小程序)才能从GUI 中正确布局。或者更好的是,将一些内容添加到框架中,以便布局管理器可以计算出打包时 应该有多大(并删除任何设置大小、首选大小、最大值或(甚至可能) 最小尺寸)。 -1
【解决方案2】:

大多数布局管理器最好使用组件的 preferredSize,而大多数 GUI 最好允许它们包含的组件根据其内容或属性设置自己的 preferredSize。要充分利用这些布局管理器,请在使它们可见之前在您的顶级容器(例如 JFrame)上调用 pack(),因为这将告诉这些管理器执行他们的操作 - 布局他们的组件。

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

例如,这是我为another question on this site 创建的一个小的拖动矩形应用程序:

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,在 JFrame 上调用 pack();,设置其位置,然后在我的 JFrame 上调用 setVisible(true);

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

【讨论】:

    【解决方案3】:

    试试这个,但您可以调整边框大小并编辑标题。

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

    【讨论】:

      猜你喜欢
      • 2010-12-28
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2013-08-11
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      相关资源
      最近更新 更多