将Jframe与不同位置对齐


无思
2025-03-19 02:49:38 (4小时前)
  1. * 100像素的jframe0,15.5“,一个变量告诉特定的理想值?并根据我的工具栏的位置改变其值或


操作
</跨度>
具有更小/更大尺寸和工具栏的系统

3 条回复
  1. 0# 土豆 | 2019-08-31 10-32



    您可以使用以下代码获取屏幕大小:




    1. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      double width = screenSize.getWidth();
      double height = screenSize.getHeight();

    2. </code>


    在您的框架中,您可以通过从屏幕宽度中减去JFrame的宽度来设置相应的位置。对于高度,它是相同的程序。


  2. 1# google你他吗 | 2019-08-31 10-32




    Toolkit.getDefaultToolkit().getScreenSize();

    返回的全屏大小

    默认
    </强>
    屏幕,它没有考虑其他屏幕元素,如停靠栏或任务栏,它们并不总是在屏幕的底部,并不总是相同的大小



    一个更好的解决方案可能是利用

    Toolkit.getDefaultToolkit().getScreenInsets



    GraphicsConfiguration




    1. public static Rectangle getScreenViewableBounds(Window window) {
      return getScreenViewableBounds((Component) window);
      }

    2. public static Rectangle getScreenViewableBounds(Component comp) {
      return getScreenViewableBounds(getGraphicsDevice(comp));
      }

    3. public static Rectangle getScreenViewableBounds(GraphicsDevice gd) {
      Rectangle bounds = new Rectangle(0, 0, 0, 0);
      if (gd == null) {
      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
      gd = ge.getDefaultScreenDevice();
      }

    4. if (gd != null) {
    5.     GraphicsConfiguration gc = gd.getDefaultConfiguration();
    6.     bounds = gc.getBounds();
    7.     Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
    8.     bounds.x += insets.left;
    9.     bounds.y += insets.top;
    10.     bounds.width -= (insets.left + insets.right);
    11.     bounds.height -= (insets.top + insets.bottom);
    12. }
    13. return bounds;
    14. }

    15. </code>


    这将返回特定屏幕的“安全可视”范围。如果你通过它

    null

    ,它将使用“默认”屏幕




    1. // Safe viewable area for default screen
      Rectangle bounds = getScreenViewableBounds(null);
      int x = bounds.x + ((bounds.width - getWidth());
      int y = bounds.y + ((bounds.width - getHeight());

    2. setLocation(x, y);

    3. </code>


    将窗口置于窗口的底部/右侧位置,但是将其与任务栏对齐(因为大多数人将它沿着底部对齐)



    对于像我这样将任务栏放在屏幕顶部的怪人




    1. // Safe viewable area for default screen
      Rectangle bounds = getScreenViewableBounds(null);
      int x = bounds.x;
      int y = bounds.y;

    2. setLocation(x, y);

    3. </code>


    将窗口放在可查看的顶部/左角,在任务栏下方对齐



    是的,我见过太多的开发人员使用

    setLocation(0, 0)

    并将窗口置于任务栏/菜单栏下,其名称变为“泥”


登录 后才能参与评论