10

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

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

4

3 回答 3

19

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

你可以:

编辑

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

编辑2

当使用 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();
            }
        });
    }
}
于 2012-04-23T15:36:13.290 回答
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 );
    }
}
于 2012-04-23T15:34:45.140 回答
3

当您构造对话框时(当您从通常是您的主窗体的 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);
于 2012-04-23T15:29:24.483 回答