简体   繁体   English

如何在屏幕上居中使用JDialog?

[英]How do I center a JDialog on screen?

如何将JDialog定位在屏幕中央?

In Java 1.4+ you can do: 在Java 1.4+中,您可以:

final JDialog d = new JDialog();
d.setSize(200,200);
d.setLocationRelativeTo(null);
d.setVisible(true);

Or perhaps (pre 1.4): 或许(1.4之前):

final JDialog d = new JDialog();
d.setSize(200, 200);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();
final int x = (screenSize.width - d.getWidth()) / 2;
final int y = (screenSize.height - d.getHeight()) / 2;
d.setLocation(x, y);
d.setVisible(true);

pack()方法之后使用此行:

setLocation((Toolkit.getDefaultToolkit().getScreenSize().width)/2 - getWidth()/2, (Toolkit.getDefaultToolkit().getScreenSize().height)/2 - getHeight()/2);

Two helpers for centering within the screen or within the parent. 两个助手,用于在屏幕内或父母内部居中。

// Center on screen ( absolute true/false (exact center or 25% upper left) )
public void centerOnScreen(final Component c, final boolean absolute) {
    final int width = c.getWidth();
    final int height = c.getHeight();
    final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screenSize.width / 2) - (width / 2);
    int y = (screenSize.height / 2) - (height / 2);
    if (!absolute) {
        x /= 2;
        y /= 2;
    }
    c.setLocation(x, y);
}

// Center on parent ( absolute true/false (exact center or 25% upper left) )
public void centerOnParent(final Window child, final boolean absolute) {
    child.pack();
    boolean useChildsOwner = child.getOwner() != null ? ((child.getOwner() instanceof JFrame) || (child.getOwner() instanceof JDialog)) : false;
    final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    final Dimension parentSize = useChildsOwner ? child.getOwner().getSize() : screenSize ;
    final Point parentLocationOnScreen = useChildsOwner ? child.getOwner().getLocationOnScreen() : new Point(0,0) ;
    final Dimension childSize = child.getSize();
    childSize.width = Math.min(childSize.width, screenSize.width);
    childSize.height = Math.min(childSize.height, screenSize.height);
    child.setSize(childSize);        
    int x;
    int y;
    if ((child.getOwner() != null) && child.getOwner().isShowing()) {
        x = (parentSize.width - childSize.width) / 2;
        y = (parentSize.height - childSize.height) / 2;
        x += parentLocationOnScreen.x;
        y += parentLocationOnScreen.y;
    } else {
        x = (screenSize.width - childSize.width) / 2;
        y = (screenSize.height - childSize.height) / 2;
    }
    if (!absolute) {
        x /= 2;
        y /= 2;
    }
    child.setLocation(x, y);
}

here's my solution to retrieve screen dimension with multiple monitors. 这是我用多个显示器检索屏幕尺寸的解决方案。

import java.awt.*;
import javax.swing.JFrame;

/**
 * Méthodes statiques pour récupérer les informations d'un écran.
 *
 * @author Jean-Claude Stritt
 * @version 1.0 / 24.2.2009
 */
public class ScreenInfo {

  /**
   * Permet de récupérer le numéro de l'écran par rapport à la fenêtre affichée.
   * @return le numéro 1, 2, ... (ID) de l'écran
   */
  public static int getScreenID( JFrame jf ) {
    int scrID = 1;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gd = ge.getScreenDevices();
    for (int i = 0; i < gd.length; i++) {
      GraphicsConfiguration gc = gd[i].getDefaultConfiguration();
      Rectangle r = gc.getBounds();
      if (r.contains(jf.getLocation())) {
        scrID = i+1;
      }
    }
    return scrID;
  }

  /**
   * Permet de récupérer la dimension (largeur, hauteur) en px d'un écran spécifié.
   * @param scrID --> le n° d'écran
   * @return la dimension (largeur, hauteur) en pixels de l'écran spécifié
   */
  public static Dimension getScreenDimension( int scrID ) {
    Dimension d = new Dimension(0, 0);
    if (scrID > 0) {
      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
      DisplayMode mode = ge.getScreenDevices()[scrID - 1].getDisplayMode();
      d.setSize(mode.getWidth(), mode.getHeight());
    }
    return d;
  }

  /**
   * Permet de récupérer la largeur en pixels d'un écran spécifié.
   * @param scrID --> le n° d'écran
   * @return la largeur en px de l'écran spécifié
   */
  public static int getScreenWidth( int scrID ) {
    Dimension d = getScreenDimension(scrID);
    return d.width;
  }

  /**
   * Permet de récupérer la hauteur en pixels d'un écran spécifié.
   * @param scrID --> le n° d'écran
   * @return la hauteur en px de l'écran spécifié
   */
  public static int getScreenHeight( int scrID ) {
    Dimension d = getScreenDimension(scrID);
    return d.height;
  }

}

AFAIK you can pass a GraphicEnvironment to each JDialog/JFrame/JWindow constructor. AFAIK可以将GraphicEnvironment传递给每个JDialog / JFrame / JWindow构造函数。 This object describes the monitor to use. 该对象描述了要使用的监视器。

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

相关问题 屏幕中心的JDialog而不是所需的JApplet中心 - JDialog in center of screen instead of desired center of JApplet 如何确定JDialog是否有子级? - How do I determine if a JDialog has children? JDialog 出现在屏幕的左上角,当我将它的位置设置为其父级的中心时 - JDialog appears in the top left corner of screen, when when I've set it's position to the center of its parent 在intellij插件的多屏幕环境中,在屏幕中心启动JDialog - Launching a JDialog at the center of screen in a multi screen environment in an intellij plugin 我如何获取已从jdialog类创建的对象以在另一个jdialog类中使用 - how do i get a object that has been created from a jdialog class to be use in another jdialog class 如何在屏幕上居中放置一个java.awt.FileDialog - How do I center a java.awt.FileDialog on the screen 如何将JDialog框附加到Eclipse的事件循环? - How do I attach a JDialog box to Eclipse's event loop? 在插件中使用JDialog时,如何防止冻结? - How do I prevent a JDialog from freezing when used in a plugin? 如何关闭JDialog并通知Window事件监听器? - How do I close a JDialog and have the Window Event Listeners be notified? 如何在屏幕上获取要放置在JDialog中的JFrame位置? - How to get a JFrame position in the screen to put in a JDialog?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM