设置JDialog相对于JFrame的位置 [英] Set location of JDialog relative to JFrame

查看:148
本文介绍了设置JDialog相对于JFrame的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to set a dialog location relative to a JFrame?

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

I would like to center the dialog to the frame that houses my GUI, instead the dialog often appears in the center of the screen rather than within the GUI.

推荐答案


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

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

您可以:

  • pass JFrame as argument for JDialog.setLocationRelativeTo(Component c)
  • set to desired Point from JFrame to JDialog.setLocation(Point p)

编辑

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

all Swing code must be done on EventDispatchThread, meaning that setVisible(true) should be wrapped into invokeLater

EDIT2

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

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

不可能,一些小错误

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

这篇关于设置JDialog相对于JFrame的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆