不可调整大小的窗口边框和定位 [英] non resizable window border and positioning
问题描述
如果我创建不可调整大小的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);
}
}
我做错了什么或是这个bug?
如何在没有重叠边框的情况下并排显示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:
Aero Off:
Aero Off:
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屋!