简体   繁体   English

如何在另一个JFrame旁边设置一个JFrame位置?

[英]How to set a JFrame location beside another JFrame?

In Java, how could I set a JFrame to automatically go beside another JFrame? 在Java中,如何设置一个JFrame使其自动在另一个JFrame旁边?

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: 因此,假设我有两个JFrame对象frameAframeB ,并且在程序运行时,它通过使用以下命令将frameA的位置设置为在屏幕中间:

setLocationRelativeTo(null);

Now what I want is to make frameB to be on the right side of frameA , so they would be right beside each other. 现在,我要的是使frameB是在右侧frameA ,所以他们会是正确彼此旁边。 ( frameA s right side would be touching frameB s left side) frameA的右侧将与frameB的左侧接触)

How would you do this? 你会怎么做?

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. 您使用f1.getX()+ f1.getWidth()查找第二帧的正确位置。

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);

    }

}

How about something like: 怎么样:

final Rectangle bounds = frameA.getBounds();
frameB.setLocation(bounds.x + bounds.width, bounds.y);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM