2008-10-17 18 views

उत्तर

137

जावा में आप कर 1.4 + कर सकते हैं:

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

या शायद (पूर्व 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); 
+7

वास्तव में जब आप शून्य पर रिश्तेदार setlocation क्या हो रहा है? –

+5

यह जोड़ना चाहता था कि आपको setSize() का उपयोग करने की आवश्यकता है या फिर setLocationRelativeTo() काम नहीं करेगा। सब कुछ ठीक दिखने के लिए मुझे setSize() और setPreferredSize() दोनों का उपयोग करना पड़ा। – Richard

+2

@marked द्वारा प्रश्न के उत्तर में: यदि घटक वर्तमान में प्रदर्शित नहीं हो रहा है, या सी शून्य है, तो विंडो स्क्रीन के केंद्र में रखी गई है। (java.awt.Window javadocs से) –

3

AFAIK आप प्रत्येक JDialog/JFrame/JWindow निर्माता के लिए एक GraphicEnvironment पारित कर सकते हैं। यह ऑब्जेक्ट मॉनीटर का उपयोग करने का वर्णन करता है।

5

एकाधिक मॉनीटर के साथ स्क्रीन आयाम पुनर्प्राप्त करने का मेरा समाधान यहां है।

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

} 
+0

प्लस वन क्योंकि किसी की मातृभाषा का उपयोग कोड की गुणवत्ता में वृद्धि करेगा। –

6

स्क्रीन के भीतर या माता-पिता के भीतर केंद्रित करने के लिए दो सहायक।

// 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); 
} 
5

pack() विधि के बाद इस लाइन का उपयोग करें:

setLocation((Toolkit.getDefaultToolkit().getScreenSize().width)/2 - getWidth()/2, (Toolkit.getDefaultToolkit().getScreenSize().height)/2 - getHeight()/2); 
संबंधित मुद्दे