【问题标题】:How to set a JFrame location beside another JFrame?如何在另一个 JFrame 旁边设置 JFrame 位置?
【发布时间】:2014-04-13 05:48:21
【问题描述】:

在 Java 中,如何将 JFrame 设置为自动放在另一个 JFrame 旁边?

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

setLocationRelativeTo(null);

现在我想要让frameB 位于frameA 的右侧,这样它们就在彼此旁边。 (frameAs 右侧会碰到frameBs 左侧)

你会怎么做?

【问题讨论】:

标签: java swing layout jframe location


【解决方案1】:

这是我创建的一个测试类,用于演示如何完成它的示例。您使用 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);

    }

}

【讨论】:

    【解决方案2】:

    怎么样:

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

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 2014-12-22
      相关资源
      最近更新 更多