不可调整大小的窗口边框和定位 [英] non resizable window border and positioning

查看:23
本文介绍了不可调整大小的窗口边框和定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建了不可调整大小的 JFrame,并且启用了 windows Aero,setLocation 似乎没有正确考虑窗口边框.

If i create non-resizable JFrames, and windows Aero is enabled setLocation does not seem to take account of the window border correctly.

在下面的代码中,我希望第二帧位于第一帧的右侧,而不是边框​​重叠.如果 Aero 被禁用或者如果我删除了对 setResizable 的调用,这将按预期完成.

In the following code I would expect the second frame to be positioned to the right of the first frame, instead the borders are overlapping. If Aero is disabled or if I remove the calls to setResizable this is done as expected.

import java.awt.Rectangle;
import javax.swing.JFrame;
public class FrameBorders {
public static void main(String[] args) {
    JFrame frame1 = new JFrame("frame 1");
    JFrame frame2 = new JFrame("frame 2");

    frame1.setResizable(false);
    frame2.setResizable(false);

    frame1.setVisible(true);        
    Rectangle bounds = frame1.getBounds();      
    frame2.setLocation(bounds.x+bounds.width, bounds.y);
    frame2.setVisible(true);

}
}

我做错了什么还是这是一个错误?如何在没有重叠边框的情况下并排显示 2 个不可调整大小的对话框?

Am I doing something wrong or is this a bug? How can I display 2 unresizable dialogs side by side without having overlapping borders?

添加屏幕截图(也将 frame2 更改为 JDialog 而不是 JFrame)

added screenshots (also changed frame2 to a JDialog instead of a JFrame)

气动开启:

气动关闭:

Aero On 但可调整大小:

Aero On but resizable:

推荐答案

不可调整大小的容器的设置边界有什么问题?

What are the problems with settings bounds on non-resizable containers?

假设您调整边界以在您的平台上看起来不错.假设用户的平台具有不同的字体,例如更大的 FontMetrics.这个例子有点做作,但你明白了.如果您更改不可调整大小的容器的边界,请确保无论主机平台的默认字体如何,任何文本都可见.

Suppose you adjust the bounds to look good on your platform. Suppose the user's platform has a font with different, say larger, FontMetrics. This example is somewhat contrived, but you get the idea. If you change the bounds of a non-resizable container, be sure any text is visible regardless of the host platform's default font.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 * @see http://stackoverflow.com/a/12532237/230513
 */
public class Evil extends JPanel {

    private static final String s =
        "Tomorrow's winning lottery numbers: 42, ";
    private JLabel label = new JLabel(s + "3, 1, 4, 1, 5, 9", JLabel.LEFT);

    public Evil() {
        this.add(label);
    }

    private void display() {
        JFrame f = new JFrame("Evil");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this, BorderLayout.WEST);
        f.pack();
        int w = SwingUtilities.computeStringWidth(
            label.getFontMetrics(label.getFont()), s);
        int h = f.getHeight();
        f.setSize(w, h);
        f.setResizable(false);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Evil().display();
            }
        });
    }
}

这篇关于不可调整大小的窗口边框和定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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