java - 设置 JDialog 相对于 JFrame 的位置

标签 java swing jframe jdialog

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

我想将对话框居中放置在我的 GUI 所在的框架上,但对话框通常出现在屏幕中央而不是 GUI 中。

最佳答案

Is there a way to set a location relative to another JFrame

您可以:

编辑

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

编辑2

when using the JFrame as Component c I am getting a cannot find symbol error

不可能,一些小错误

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

关于java - 设置 JDialog 相对于 JFrame 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10283532/

相关文章:

java - 临时变量的效率(例如java)

java - java中不兼容的类型

java - 我如何使用 jemmy 测试应用程序退出

java - 如何在 JFrame 中创建视口(viewport)?

java - 如何停止方法启动直到 JFrame 关闭

java - Java 中的管道 (|) 运算符

java - Jsoup 在元素之前获取评论

java - DocumentFilter 的每个方法(及其参数)到底做什么?

java - 更改交通灯java切换按钮的状态

java - 如何在解析复杂 XML 时尽量减少 XMLStreamReader 中 if else if 的使用