将JDialog的位置设置相对于JFrame

11
有没有一种方法可以将对话框位置相对于 JFrame 设置?
我想将对话框居中于包含GUI的框架中,而不是在屏幕中间显示,而不是在GUI中显示。
3个回答

20

有没有一种方法来设置与另一个JFrame相关的位置?

您可以:

编辑

所有Swing代码必须在事件分派线程(EventDispatchThread)上完成,这意味着setVisible(true)应该包装在invokeLater中。

编辑2

使用JFrame作为组件c时出现了找不到符号错误

不可能,可能有一些小错误

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

4
我还要提到,在设置相对位置之前,必须调用大小(setSize()或pack())的方法。 - Jakub Zaverka
当我将JFrame用作组件c时,我遇到了“找不到符号”错误。 - Itchy Nekotorych
父框架是GUI,在另一个类中我使用JDialog.setLocationRelativeTo(SchedulerGUI);但找不到SchedulerGUI。 - Itchy Nekotorych
抱歉,我不是通灵者,也无法看到您的监视器,请编辑您的问题并提供有关JFrame、JDialog和相关方法返回异常的简短SSCCE - mKorbel

7
你需要的方法是: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
当你构建对话框(通常是你的主窗体)时,应将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);

1
这将把对话框的左上角放置在框架的中心,并出现在父级的右下角。请参见其他答案中的 setLocationRelativeTo() - Andrew Thompson
2
奇怪,如果我使用屏幕的宽度和高度,对话框会完美地居中于我的屏幕上。哦,是啊,但我忘记了减去对话框高度和宽度的一半... - MarioDS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接