2012-04-23 47 views

Trả lời

12

Có cách nào để thiết lập một vị trí tương đối với một JFrame

Bạn có thể:

EDIT

tất cả các mã Swing phải được thực hiện trên EventDispatchThread, có nghĩa là setVisible(true) nên được bọc vào invokeLater

EDIT2

khi sử dụng JFrame như Component c Tôi nhận được một không thể tìm lỗi biểu tượng

không thể, vì vậy tôi sai lầm nhỏ

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

Tôi cũng đề cập đến kích thước (hoặc setSize() hoặc pack()) phải được gọi trước khi thiết lập vị trí tương đối. –

+0

khi sử dụng JFrame làm Component c Tôi nhận được lỗi biểu tượng không thể tìm thấy. –

+0

@Shane Kelsey ví dụ – mKorbel

3

Bạn nên cung cấp this làm đối số cho tham số parent khi bạn xây dựng hộp thoại (khi bạn gọi từ lớp JFrame thường là biểu mẫu chính của bạn). Trừ khi bạn cung cấp cho mã trong câu hỏi của bạn, tôi không thể đưa ra trợ giúp chi tiết hơn ...

Edit: Để tập trung nó trong phụ huynh, làm điều này:

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

này sẽ đặt góc trên bên trái của hộp thoại ở trung tâm của khung hình, và nó sẽ xuất hiện ở phía dưới bên của cha mẹ. Xem các câu trả lời khác cho 'setLocationRelativeTo()'. –

+2

Lạ, nếu tôi sử dụng chiều rộng và chiều cao của màn hình, hộp thoại xuất hiện hoàn toàn chính giữa trên màn hình của tôi. Oh yeah nhưng tôi quên trừ một nửa chiều cao và chiều rộng của hộp thoại ... – MarioDS

5

Phương pháp bạn muốn là: setLocationRelativeTo()

Thêm nó bằng null vào JFrame, sẽ căn giữa nó trên màn hình. Thêm JFrame vào hộp thoại sẽ căn giữa nó trên khung hình jframe.

Chúc mừng.

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); 
    } 
} 
Các vấn đề liên quan