如何在另一个JFrame旁边设置一个JFrame位置? [英] How to set a JFrame location beside another JFrame?

查看:116
本文介绍了如何在另一个JFrame旁边设置一个JFrame位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,如何设置一个JFrame使其自动在另一个JFrame旁边?

所以,假设我有两个JFrame对象frameAframeB,并且在程序运行时,它使用以下命令将frameA s的位置设置在屏幕中间:

So, say I have two JFrame objects, frameA and frameB, and when the program runs, it sets frameAs 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. (frameAs right side would be touching frameBs 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位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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