2012-08-28 24 views
7

मैं एक बहुत बड़ा आवेदन जो कई संवाद है है स्क्रीन यह पता लगाने की। मेरे काम को यकीन है कि एक संवाद है, जो पूरी तरह से दिखाई नहीं देता है (उपयोगकर्ता यह दृश्य स्क्रीन क्षेत्र से बाहर खींच लिया क्योंकि) स्क्रीन के केंद्र में वापस ले जाया जाता है बनाने के लिए है।कैसे जिस पर एक JDialog दिखाया गया है

कोई समस्या नहीं है कि जब मैं केवल एक स्क्रीन के साथ काम कर रहा हूँ। यह ठीक काम करता है ... हालांकि, इस एप्लिकेशन के अधिकांश उपयोगकर्ताओं के पास उनके डेस्कटॉप पर दो स्क्रीन हैं ...

जब मैं यह पता लगाने की कोशिश करता हूं कि संवाद किस स्क्रीन पर दिखाया गया है और उस विशिष्ट स्क्रीन पर केंद्रित है। .. ठीक है, यह वास्तव में केंद्र करता है, लेकिन प्राथमिक स्क्रीन पर (जो स्क्रीन पर संवाद नहीं दिखाया जा सकता है)।

आप को दिखाने के लिए क्या मेरे विचार थे अब तक, यहाँ कोड ...

/** 
* Get the number of the screen the dialog is shown on ... 
*/ 
private static int getActiveScreen(JDialog jd) { 
    int screenId = 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(jd.getLocation())) { 
      screenId = i + 1; 
     } 
    } 
    return screenId; 
} 

/** 
* Get the Dimension of the screen with the given id ... 
*/ 
private static Dimension getScreenDimension(int screenId) { 
    Dimension d = new Dimension(0, 0); 
    if (screenId > 0) { 
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     DisplayMode mode = ge.getScreenDevices()[screenId - 1].getDisplayMode(); 
     d.setSize(mode.getWidth(), mode.getHeight()); 
    } 
    return d; 
} 

/** 
* Check, if Dialog can be displayed completely ... 
* @return true, if dialog can be displayed completely 
*/ 
private boolean pruefeDialogImSichtbarenBereich() { 
    int screenId = getActiveScreen(this); 
    Dimension dimOfScreen = getScreenDimension(screenId); 
    int xPos = this.getX(); 
    int yPos = this.getY(); 
    Dimension dimOfDialog = this.getSize(); 
    if (xPos + dimOfDialog.getWidth() > dimOfScreen.getWidth() || yPos + dimOfDialog.getHeight() > dimOfScreen.getHeight()) { 
     return false; 
    } 
    return true; 
} 

/** 
* Center Dialog... 
*/ 
private void zentriereDialogAufMonitor() { 
    this.setLocationRelativeTo(null); 
} 

मैं डिबगिंग जबकि तरह का तथ्य यह है कि जिस तरह से getActiveScreen() मैं काम करने के लिए हालांकि नहीं लगता भर में आया है; ऐसा लगता है कि हमेशा 2 वापस आते हैं (जो कि बकवास है, क्योंकि इसका मतलब यह होगा कि संवाद हमेशा दूसरी मॉनिटर में दिखाया जाता है ... जो निश्चित रूप से सत्य नहीं है)।

किसी को भी कैसे स्क्रीन यह वास्तव में पर दिखाया जाता है पर मेरे संवाद केंद्र के लिए किसी भी विचार है?

+0

एकाधिक डिस्प्ले पर कितनी स्क्रीन ऐप दिखाई देगी? –

+0

मैं काफी यकीन है कि अगर मैं आपके प्रश्न को नहीं कर रहा हूँ, लेकिन अगर मैं इसे सही ढंग से समझ में आया, ... मैं नहीं जानता। यह भी, कोई फर्क नहीं पड़ता क्योंकि संवाद की स्थिति को बचाया और हर जाँच संवाद दिखाई हो जाता है ... आपके प्रश्न का उत्तर है है? :-) – gilaras

+0

आप संवाद के लिए स्क्रीन पर स्थान निर्दिष्ट क्यों नहीं करते? –

उत्तर

1

आपका getActiveScreen विधि, काम को छोड़कर यह स्क्रीन विंडो के ऊपरी-बाएं कोने युक्त इस्तेमाल किया। यदि आप इसके बजाय Component.getGraphics कॉन्फ़िगरेशन() का उपयोग करते हैं, तो यह आपको देगा कि कौन सी स्क्रीन में विंडो के अधिकांश पिक्सल हैं। setLocationRelativeTo(null) यहां कोई सहायता नहीं है क्योंकि यह हमेशा प्राथमिक स्क्रीन का उपयोग करती है। यह इस प्रकार से हल करने के लिए है:

static boolean windowFitsOnScreen(Window w) { 
    return w.getGraphicsConfiguration().getBounds().contains(w.getBounds()); 
} 

static void centerWindowToScreen(Window w) { 
    Rectangle screen = w.getGraphicsConfiguration().getBounds(); 
    w.setLocation(
     screen.x + (screen.width - w.getWidth())/2, 
     screen.y + (screen.height - w.getHeight())/2 
    ); 
} 

तो फिर तुम कर सकते हैं:

JDialog jd; 
... 
if (!windowFitsOnScreen(jd)) centerWindowToScreen(jd); 

जो निकटतम स्क्रीन (मॉनीटर) करने के लिए संवाद केंद्रित हो जाएगा। तुम्हें यकीन संवाद शुरू में प्रदर्शित किया गया है/पहले तैनात करने की जरूरत हो सकती है।

+0

आपके उत्तर के लिए धन्यवाद :-) यह 100% काम नहीं करता जिस तरह से मैं इसे शुरू करना चाहता था, लेकिन व्यवहार अभी ठीक है :-) बहुत बहुत धन्यवाद! :-) – gilaras

0

यहाँ विंडो के स्थान केंद्रित करने के लिए इस्तेमाल किया कोड है।

//Center the window 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    Dimension frameSize = frame.getSize(); 
    if (frameSize.height > screenSize.height) { 
    frameSize.height = screenSize.height; 
    } 
    if (frameSize.width > screenSize.width) { 
    frameSize.width = screenSize.width; 
    } 
    frame.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2); 
साथ

frame आप के साथ-साथ संवाद का उपयोग कर सकते हैं।

+2

यह सवाल का जवाब नहीं देता है। यह सिर्फ बताता है कि कैसे एक एकल स्क्रीन पर्यावरण (जिसमें कहा गया ओपी पहले से ही उसके लिए काम कर रहा है) ... – brimborium

+0

यह सच है में यह करने के लिए ...:-) – gilaras

1

मुझे यकीन है कि कैसे इस बारे में ज्यादा फायदा नहीं होगा नहीं कर रहा हूँ, लेकिन इस कोड है कि मैं का उपयोग करें जब खिड़कियों के लिए ग्राफिक उपकरणों निर्धारित करने के लिए कोशिश कर रहा है।

मैं थोड़ा धोखा देता हूं, मैं Component का उपयोग करता हूं और उपयोगिता विधियों को या तो शीर्ष स्तर की विंडो ढूंढने या Component के स्क्रीन बिंदु का उपयोग करने की अनुमति देता हूं।

/** 
* Returns the GraphicsDevice that the specified component appears the most on. 
*/ 
public static GraphicsDevice getGraphicsDevice(Component comp) { 

    GraphicsDevice device = null; 

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    GraphicsDevice lstGDs[] = ge.getScreenDevices(); 

    ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length); 
    if (comp != null && comp.isVisible()) { 
     Rectangle parentBounds = comp.getBounds(); 

     /* 
     * If the component is not a window, we need to find its location on the 
     * screen... 
     */ 
     if (!(comp instanceof Window)) { 
      Point p = new Point(0, 0); 
      SwingUtilities.convertPointToScreen(p, comp); 
      parentBounds.setLocation(p); 
     } 

     for (GraphicsDevice gd : lstGDs) { 
      GraphicsConfiguration gc = gd.getDefaultConfiguration(); 
      Rectangle screenBounds = gc.getBounds(); 
      if (screenBounds.intersects(parentBounds)) { 
       lstDevices.add(gd); 
      } 
     } 

     if (lstDevices.size() == 1) { 
      device = lstDevices.get(0); 
     } else { 

      GraphicsDevice gdMost = null; 
      float maxArea = 0; 
      for (GraphicsDevice gd : lstDevices) { 
       int width = 0; 
       int height = 0; 

       GraphicsConfiguration gc = gd.getDefaultConfiguration(); 
       Rectangle bounds = gc.getBounds(); 

       Rectangle2D intBounds = bounds.createIntersection(parentBounds); 

       float perArea = (float) ((intBounds.getWidth() * intBounds.getHeight())/(parentBounds.width * parentBounds.height)); 
       if (perArea > maxArea) { 
        maxArea = perArea; 
        gdMost = gd; 
       } 
      } 

      if (gdMost != null) { 
       device = gdMost; 
      } 
     } 
    } 
    return device; 
} 

/** 
* Returns the GraphicsDevice at the specified point 
*/ 
public static GraphicsDevice getGraphicsDeviceAt(Point pos) { 
    GraphicsDevice device = null; 
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    GraphicsDevice lstGDs[] = ge.getScreenDevices(); 

    List<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length); 
    for (GraphicsDevice gd : lstGDs) { 

     GraphicsConfiguration gc = gd.getDefaultConfiguration(); 
     Rectangle screenBounds = gc.getBounds(); 
     if (screenBounds.contains(pos)) { 
      lstDevices.add(gd); 
     } 
    } 

    if (lstDevices.size() > 0) { 
     device = lstDevices.get(0); 
    } 

    return device; 
} 

/** 
* Returns the Point that would allow the supplied Window to be 
* centered on it's current graphics device. 
* 
* It's VERY important that the Window be seeded with a location 
* before calling this method, otherwise it will appear on the 
* device at 0x0 
* 
* @param window 
* @return 
*/ 
public static Point centerOfScreen(Window window) { 
    // Try and figure out which window we actually reside on... 
    GraphicsDevice gd = getGraphicsDeviceAt(window.getLocation()); 
    GraphicsConfiguration gc = gd.getDefaultConfiguration(); 

    Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration()); 
    Rectangle bounds = gc.getBounds(); 
    Dimension size = bounds.getSize(); 

    size.width -= (screenInsets.left + screenInsets.right); 
    size.height -= (screenInsets.top + screenInsets.bottom); 

    int width = window.getWidth(); 
    int height = window.getHeight(); 

    int xPos = screenInsets.left + ((size.width - width)/2); 
    int yPos = screenInsets.top + ((size.height - height)/2); 

    return new Point(xPos, yPos); 
} 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^