问题描述
在Java中,如何设置一个JFrame使其自动在另一个JFrame旁边?
所以,假设我有两个JFrame
对象frameA
和frameB
,并且在程序运行时,它使用以下命令将frameA
s的位置设置在屏幕中间:
So, say I have two JFrame
objects, frameA
and frameB
, and when the program runs, it sets frameA
s location to be in the middle of the screen by using:
setLocationRelativeTo(null);
现在,我要使frameB
位于frameA
的右侧,因此它们将彼此相邻. (frameA
的右侧将触摸frameB
的左侧)
Now what I want is to make frameB
to be on the right side of frameA
, so they would be right beside each other. (frameA
s right side would be touching frameB
s left side)
您将如何做?
推荐答案
这是我创建的测试类,用于演示如何完成此操作的示例.您可以使用f1.getX()+ f1.getWidth()查找第二帧的正确位置.
This is a test class I created to demonstrate an example of how it could be done. You use the f1.getX() + f1.getWidth() to find to correct location for the second frame.
public class Test
{
public static void main (String[] args)
{
JFrame f1 = new JFrame();
f1. setSize(100, 100);
f1.setLocationRelativeTo(null);
f1.setVisible(true);
JFrame f2 = new JFrame();
f2.setSize(100, 100);
f2.setLocation(f1.getX() + f1.getWidth(), f1.getY());
f2.setVisible(true);
}
}
这篇关于如何在另一个JFrame旁边设置一个JFrame位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!