【问题标题】:Set location of JDialog relative to JFrame设置 JDialog 相对于 JFrame 的位置
【发布时间】:2012-05-04 05:47:01
【问题描述】:

有没有办法设置相对于JFrame 的对话框位置?

我想将对话框置于包含我的 GUI 的框架的中心,而不是对话框经常出现在屏幕的中心而不是 GUI 内。

【问题讨论】:

    标签: java swing jframe jdialog


    【解决方案1】:

    您应该在构造对话框时(当您从通常是您的主窗体的 JFrame 类调用它时)将 this 作为参数提供给 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);
    

    【讨论】:

    • 这会将对话框的左上角放在框架的中心,它会出现在父级的右下角。请参阅setLocationRelativeTo() 的其他答案。
    • 奇怪,如果我使用屏幕的宽度和高度,对话框会完全居中显示在我的屏幕上。哦,是的,但我忘了减去一半的对话框高度和宽度......
    【解决方案2】:

    你要的方法是:setLocationRelativeTo()

    将它与 null 添加到 JFrame,将它在屏幕上居中。将 JFrame 添加到对话框中会将其居中放置在 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 );
        }
    }
    

    【讨论】:

      【解决方案3】:

      有没有办法设置相对于另一个 JFrame 的位置

      你可以:

      编辑

      所有 Swing 代码都必须在 EventDispatchThread 上完成,这意味着 setVisible(true) 应该包装到 invokeLater

      EDIT2

      当使用 JFrame 作为组件 c 时,我得到一个找不到符号错误

      不可能,有点小错误

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

      【讨论】:

      • 我还要提一下,在设置相对位置之前必须调用大小(setSize() 或 pack())。
      • 当使用 JFrame 作为组件 c 时,我得到一个找不到符号错误。
      • 父Frame是GUI,在另一个类中我使用JDialog.setLocationRelativeTo(SchedulerGUI);并且找不到 SchedulerGUI。
      • 对不起,我不是通灵者,也看不到你的显示器,用简短的SSCCE 编辑你的问题,关于 JFrame、JDialog 和相关方法返回异常......
      猜你喜欢
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 2015-03-11
      • 2016-12-31
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      相关资源
      最近更新 更多