【问题标题】:Java - Set size of decorated JFrameJava - 设置装饰 JFrame 的大小
【发布时间】:2015-01-02 08:46:18
【问题描述】:

我对 JFrame 的大小有疑问:

我想在 Jframe 中显示内容。内容的大小为 640 x 480。但我不能使用 JFrame.setSize(640, 480); 的方法,因为我还想显示窗口的装饰。 我的第一个想法是:添加一个具有首选尺寸的面板,而不是使用 pack();。但这也不起作用 - 我只得到一个非常小的窗口。

我认为我的问题的解决方案可能与此类似:

这是我的代码:

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Fenster extends JFrame {

    JPanel panel;
    Dimension dim;

    Fenster(){

        dim = new Dimension(640, 480);

        panel = new JPanel();
        panel.setSize(dim);
        panel.setMinimumSize(dim);
        panel.setMaximumSize(dim);
        panel.setPreferredSize(dim);
        panel.setBounds(0, 0, 640, 480);
        panel.setDoubleBuffered(true);

        JLabel label = new JLabel("bla");
        panel.add(label);

        this.setLayout(null);
        this.getContentPane().add(panel);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        this.pack();
        this.setVisible(true);

    }


}

【问题讨论】:

  • this.setLayout(null); 是你的问题。让布局管理器完成它的工作
  • 前面你强调的问题不是完全相同的问题,或者至少接受的解决方案不正确......
  • “我的第一个想法是:添加一个具有首选尺寸的面板,而不是使用 pack();。” 你的第一个想法是正确的(就像在最佳实践中一样,可以工作)。我会坚持找出失败的原因。

标签: java swing jframe size window-decoration


【解决方案1】:

this.setLayout(null); 是你的问题。让布局管理器完成它的工作

来自JavaDocs

public void pack()

调整此窗口的大小以适应 首选尺寸及其子组件的布局。结果宽度 如果其中任何一个,窗口的高度和高度都会自动放大 尺寸小于前面指定的最小尺寸 调用 setMinimumSize 方法。

如果窗口和/或其 所有者还不能显示,它们都可以显示 在计算首选尺寸之前。窗口在之后被验证 正在计算它的大小。

大多数容器(JComponentJPanel)都有一个默认的首选大小0x0,布局管理器根据布局管理器本身的要求和容器的内容提供此信息,但使用null,你有效地制作了容器0x0...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setUndecorated(true);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            add(new JLabel("Look ma, no null layouts!"));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(640, 480);
        }

    }

}

避免使用null 布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

详情请见Why is it frowned upon to use a null layout in SWING?...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    相关资源
    最近更新 更多