JFrame

2012-04-23 19 views
8

के सापेक्ष JDialog का स्थान सेट करें JFrame से संबंधित एक संवाद स्थान सेट करने का कोई तरीका है?JFrame

मैं अपने जीयूआई वाले फ्रेम में संवाद केंद्रित करना चाहता हूं, इसके बजाय संवाद अक्सर जीयूआई के बजाय स्क्रीन के केंद्र में दिखाई देता है।

उत्तर

12

एक और JFrame

करने के लिए एक स्थान रिश्तेदार स्थापित करने के लिए एक रास्ता है आप कर सकते हैं:

संपादित

सभी स्विंग कोड EventDispatchThread पर किया जाना चाहिए, जिसका अर्थ है कि setVisible(true) जब घटक सी के रूप में JFrame का उपयोग कर मैं एक नहीं कर सकते हो रही है invokeLater

EDIT2

में लपेटा जाना चाहिए प्रतीक त्रुटि

संभव नहीं है, इसलिए मुझे छोटी सी गलती

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

public class ClosingFrameDialog extends JFrame { 

    private JFrame frame = new JFrame(); 
    private static final long serialVersionUID = 1L; 
    private JMenuBar MenuBar; 
    private JMenu File; 
    private JMenuItem Exit; 
    private JMenuItem ShowDialog; 
    private JDialog dialog; 
    private Point location; 

    public ClosingFrameDialog() { 
     Exit = new JMenuItem(" Exit "); 
     Exit.addActionListener(new ExitListener()); 
     ShowDialog = new JMenuItem(" Show Dialog "); 
     ShowDialog.addActionListener(showingDialog()); 
     File = new JMenu(" File "); 
     File.add(Exit); 
     File.add(ShowDialog); 
     MenuBar = new JMenuBar(); 
     MenuBar.add(File); 
     frame.addWindowListener(exitListener); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.setJMenuBar(MenuBar); 
     frame.setPreferredSize(new Dimension(400, 300)); 
     frame.setLocation(100, 100); 
     frame.pack(); 
     frame.setVisible(true); 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       createDialog(); 
      } 
     }); 
    } 

    private void createDialog() { 
     JButton btn = new JButton(" Save changes "); 
     btn.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       //some stuff for saving whatewer programatically 
       dialog.setVisible(false); 
      } 
     }); 
     JButton btn1 = new JButton(" Ignore changes "); 
     btn1.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       dialog.setLocationRelativeTo(frame); 
       dialog.setVisible(false); 
      } 
     }); 
     dialog = new JDialog(); 
     dialog.add(btn); 
     dialog.add(btn1); 
     dialog.setVisible(false); 
     dialog.setAlwaysOnTop(true); 
     dialog.setModal(true); 
     dialog.setLayout(new GridLayout(2, 0, 10, 10)); 
     JPanel pane = (JPanel) dialog.getContentPane(); 
     pane.setBorder(new EmptyBorder(10, 10, 10, 10)); 
     dialog.addWindowListener(closeListener); 
     dialog.pack(); 
    } 
// 
    private WindowListener exitListener = new WindowAdapter() { 

     @Override 
     public void windowClosing(WindowEvent e) { 
      int confirm = JOptionPane.showOptionDialog(frame, 
        "Are You Sure to Close this Application?", 
        "Exit Confirmation", JOptionPane.YES_NO_OPTION, 
        JOptionPane.QUESTION_MESSAGE, null, null, null); 
      if (confirm == 0) { 
       System.exit(1); 
      } 
     } 

     @Override 
     public void windowIconified(WindowEvent e) { 
      int confirm = JOptionPane.showOptionDialog(frame, 
        "Are You Sure to Close this Application?", 
        "Exit Confirmation", JOptionPane.YES_NO_OPTION, 
        JOptionPane.QUESTION_MESSAGE, null, null, null); 
      if (confirm == 0) { 
       //System.exit(1); 
      } 
     } 
    }; 
// 
    private WindowListener closeListener = new WindowAdapter() { 

     @Override 
     public void windowClosing(WindowEvent e) { 
      int confirm = JOptionPane.showOptionDialog(dialog, 
        "Are you want to save changes", 
        "Exit Confirmation", JOptionPane.YES_NO_OPTION, 
        JOptionPane.QUESTION_MESSAGE, null, null, null); 
      if (confirm == 0) { 
       //some stuff for saving whatewer programatically 
       dialog.setVisible(true); 
      } else if (confirm == 1) {// nothing only hide JDialog 
       dialog.setVisible(true); 
      } 
     } 
    }; 

    private Action showingDialog() { 
     return new AbstractAction("Show Dialog") { 

      private static final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       Runnable doRun = new Runnable() { 

        @Override 
        public void run() { 
         dialog.setVisible(false); 
         location = frame.getLocationOnScreen(); 
         int x = location.x; 
         int y = location.y; 
         //dialog.setLocation(x, y); 
         dialog.setLocationRelativeTo(frame); 
         dialog.setVisible(true); 
        } 
       }; 
       SwingUtilities.invokeLater(doRun); 
      } 
     }; 
    } 

    private class ExitListener implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      int confirm = JOptionPane.showOptionDialog(frame, 
        "Are You Sure to Close this Application?", 
        "Exit Confirmation", JOptionPane.YES_NO_OPTION, 
        JOptionPane.QUESTION_MESSAGE, null, null, null); 
      if (confirm == 0) { 
       System.exit(1); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       ClosingFrameDialog cf = new ClosingFrameDialog(); 
      } 
     }); 
    } 
} 
+3

मैं यह भी उल्लेख करता हूं कि सापेक्ष स्थान सेट करने से पहले आकार (या तो सेटसाइज() या पैक()) को कॉल किया जाना चाहिए। –

+0

जेएफआरएएम को कंपोनेंट सी के रूप में उपयोग करते समय मुझे एक त्रुटि त्रुटि नहीं मिल रही है। –

+0

@ शेन केल्सी उदाहरण के लिए – mKorbel

3

आपको को parent पैरामीटर के लिए तर्क के रूप में देना चाहिए जब आप संवाद बनाते हैं (जब आप इसे एक जेएफआरएएम कक्षा से कॉल करते हैं जो आमतौर पर आपका मुख्य प्रारूप होता है)। जब तक आप अपने प्रश्न में कोड दे, मैं और अधिक विस्तृत मदद नहीं कर सकते हैं ...

संपादित करें: माता-पिता के भीतर यह केंद्र के लिए, ऐसा करते हैं:

MyDialog dialog = new MyDialog(this, true); //or false for non-modal 
dialog.setLocation(this.getWidth/2 - dialog.getWidth/2, this.getHeight/2 - dialog.getHeight/2); 
dialog.setVisible(true); 
+1

यह फ्रेम के केंद्र में संवाद के ऊपरी-बाएं कोने जगह होगी, और यह माता पिता के निचले दाएँ में प्रदर्शित होगी। 'SetLocationRelativeTo()' के लिए अन्य उत्तरों देखें। –

+2

अजीब, अगर मैं अपनी स्क्रीन की चौड़ाई और ऊंचाई का उपयोग करता हूं, तो संवाद पूरी तरह से मेरी स्क्रीन पर केंद्रित होता है। ओह हाँ, लेकिन मैं आधा संवाद ऊंचाई और चौड़ाई को घटाना भूल गया ... – MarioDS

5

विधि आप चाहते है: setLocationRelativeTo()

इसे जेएफआरएएम में शून्य के साथ जोड़कर, इसे स्क्रीन पर केंद्रित कर देगा। संवाद में जेएफआरएएम जोड़ना इसे jframe पर केंद्रित करेगा।

चीयर्स।

import javax.swing.JDialog; 
import javax.swing.JFrame; 

public class Centered 
{ 
    public static void main(String args[]) 
    { 
     JFrame jFrame = new JFrame(); 

     jFrame.setSize(250 , 250); 
     jFrame.setLocationRelativeTo(null); 
     jFrame.setVisible(true); 

     JDialog jDialog = new JDialog(); 
     jDialog.setLocationRelativeTo(jFrame); 
     jDialog.setVisible(true); 
    } 
}