无法调整大小的JFrame包错误 [英] Unresizable JFrame pack error

查看:68
本文介绍了无法调整大小的JFrame包错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JFramepack()方法都不起作用.请自己尝试一下(可能需要重试几次):

JFrame's pack() method won't work every time when the window is not resizable it seems. Try it for yourself (it may need a few retries) please:

import javax.swing.*;
import java.awt.*;

public class FramePackBug {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                for (int i = 0; i < 20; i++) {
                    JFrame window = new JFrame("Buggy");
                    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    window.setResizable(false);
                    window.setBackground(Color.RED);
                    JPanel jp = new JPanel() {
                        public void paintComponent(Graphics g) {
                            g.setColor(Color.GREEN);
                            g.fillRect(0, 0, 200, 100);
                        }
                    };
                    jp.setPreferredSize(new Dimension(200, 100));
                    window.add(jp);
                    window.pack();
                    window.setLocation((i % 5) * 250, (i / 5) * 150);
                    window.setVisible(true);
                }
            }
        });
    }
}

(您可以通过使其无法调整大小后直接使其可见来对其进行修复.)

(You can fix it by making the frame visible directly after making it unresizable.)

为什么会这样?

推荐答案

作为参考,以下是Mac OS X上的一个变体.注意,

For reference, here's the appearance of a variation on Mac OS X. Note,

  • 一个像素的差异表明可能存在用于聚焦指示器的空间;但是作为@ Marco13 评论偶然 >外观表明存在(非显而易见的)线程问题.

  • A one-pixel disparity suggests the possibility of space intended for a focus indicator; but as @Marco13 comments, the occasional appearance suggests a (non-obvious) threading issue.

外观似乎与平台有关,因此标签会显示OS和版本系统属性.

As the appearance seems platform dependent, a label displays the OS and version system properties.

该示例将覆盖getPreferredSize(),以建立封闭面板的几何形状,如此处所示.

The example overrides getPreferredSize() to establish the enclosed panel's geometry, as suggested here.

如果实现尊重不透明度属性,通过填充每个像素.

The call to super.paintComponent() is not strictly necessary if the implementation honors the opacity property by filling every pixel.

在@AyCe的Windows屏幕快照中看到的不规则位置是由@ Marco13建议的线程问题组成的.

The irregular locations seen in @AyCe's Windows screenshot are consisted with a threading issue suggested by @Marco13.

Mac OS X:

Mac OS X:

Windows 7(@AyCe):

Windows 7 (@AyCe):

Ubuntu 14:

Ubuntu 14:

import javax.swing.*;
import java.awt.*;

public class FramePackBug {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    JFrame window = new JFrame("Buggy");
                    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    window.setResizable(false);
                    window.setBackground(Color.RED);
                    JPanel jp = new JPanel() {
                        @Override
                        public void paintComponent(Graphics g) {
                            super.paintComponent(g);
                            g.setColor(Color.GREEN);
                            g.fillRect(0, 0, getWidth(), getHeight());
                        }

                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(200, 100);
                        }
                    };
                    window.add(jp);
                    window.add(new JLabel(System.getProperty("os.name") + ", "
                        + System.getProperty("os.version")), BorderLayout.NORTH);
                    window.pack();
                    window.setLocation((i % 5) * (window.getWidth() + 5),
                        (i / 5) * (window.getHeight() + 5) + 20);
                    window.setVisible(true);
                }
            }
        });
    }
}

这篇关于无法调整大小的JFrame包错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆