65

我创建了一个使用 JFrame 的 GUI。我应该如何使它成为模态的?

4

13 回答 13

75

如果要使窗口模态化,最好的办法是使用 JDialog 而不是 JFrame。查看有关在 Java 6 中引入 Modality API 的详细信息以获取信息。还有一个教程

这是一些示例代码,它将JPanel panelJDialog模态到Frame parentFrame. 除了构造函数之外,这遵循与打开 a 相同的模式JFrame

final JDialog frame = new JDialog(parentFrame, frameTitle, true);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);

编辑:更新了 Modality API 链接并添加了教程链接(向@spork 点头致意)。

于 2009-09-26T15:13:50.513 回答
20

您可以创建一个传递对父级的引用JFrame并将其保存在JFrame变量中的类。然后您可以锁定创建新框架的框架。

parentFrame.disable();

//Some actions

parentFrame.enable();
于 2012-02-05T20:02:16.250 回答
10

只需在课堂上替换JFrameJDialog

public class MyDialog extends JFrame // delete JFrame and write JDialog

然后setModal(true);在构造函数中写入

之后,您将能够在 netbeans 中构建您的表单,并且表单变为模态

于 2015-02-11T18:37:25.137 回答
6
  1. 创建一个新的 JPanel 表单
  2. 向其中添加所需的组件和代码

YourJPanelForm stuff = new YourJPanelForm();
JOptionPane.showMessageDialog(null,stuff,"Your title here bro",JOptionPane.PLAIN_MESSAGE);


您的模态对话框正在等待...

于 2013-05-22T08:15:07.243 回答
4

据我所知,JFrame 不能做模态模式。改用 JDialog 并调用setModalityType(Dialog.ModalityType type)将其设置为模态(或非模态)。

于 2009-09-26T15:14:05.770 回答
1

如果您准备使用 JDialog 而不是 JFrame,则可以将ModalityType设置为APPLICATION_MODAL

这提供了与典型 JOptionPane 相同的行为:

import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class MyDialog extends JFrame {

public MyDialog() {
    setBounds(300, 300, 300, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setLayout(new FlowLayout());
    JButton btn = new JButton("TEST");
    add(btn);
    btn.addActionListener(new ActionListener() 
    {

        @Override
        public void actionPerformed(ActionEvent e) {
            showDialog();
        }
    });
}

private void showDialog() 
{

    JDialog dialog = new JDialog(this, Dialog.ModalityType.APPLICATION_MODAL);
    //OR, you can do the following...
    //JDialog dialog = new JDialog();
    //dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);

    dialog.setBounds(350, 350, 200, 200);
    dialog.setVisible(true);
}

public static void main(String[] args) 
{
    new MyDialog();
}
}
于 2012-12-06T17:35:24.243 回答
1

此静态实用程序方法也通过秘密打开模态 JDialog 来显示模态 JFrame。我在 Windows 7、8 和 10-with-multiple-desktops 上成功地使用了它并且行为正确。

这是本地类很少使用的特性的一个很好的例子。

import javax.swing.*;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

// ... (class declaration)

/**
 * Shows an already existing JFrame as if it were a modal JDialog. JFrames have the upside that they can be
 * maximized.
 * <p>
 * A hidden modal JDialog is "shown" to effect the modality.
 * <p>
 * When the JFrame is closed, this method's listener will pick up on that, close the modal JDialog, and remove the
 * listener.
 *
 * made by dreamspace-president.com
 *
 * @param window the JFrame to be shown
 * @param owner  the owner window (can be null)
 * @throws IllegalArgumentException if argument "window" is null
 */
public static void showModalJFrame(final JFrame window, final Frame owner) {

    if (window == null) {
        throw new IllegalArgumentException();
    }
    window.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
    window.setVisible(true);
    window.setAlwaysOnTop(true);

    final JDialog hiddenDialogForModality = new JDialog(owner, true);
    final class MyWindowCloseListener extends WindowAdapter {
        @Override
        public void windowClosed(final WindowEvent e) {
            window.dispose();
            hiddenDialogForModality.dispose();
        }
    }

    final MyWindowCloseListener myWindowCloseListener = new MyWindowCloseListener();
    window.addWindowListener(myWindowCloseListener);

    final Dimension smallSize = new Dimension(80, 80);
    hiddenDialogForModality.setMinimumSize(smallSize);
    hiddenDialogForModality.setSize(smallSize);
    hiddenDialogForModality.setMaximumSize(smallSize);
    hiddenDialogForModality.setLocation(-smallSize.width * 2, -smallSize.height * 2);
    hiddenDialogForModality.setVisible(true);
    window.removeWindowListener(myWindowCloseListener);
}
于 2015-11-28T07:55:19.890 回答
1

唯一对我有用的代码:

childFrame.setAlwaysOnTop(true);

在使子/模态框架可见之前,应在主/父框架上调用此代码。您的孩子/模态框架也应该有这个代码:

parentFrame.setFocusableWindowState(false);
this.mainFrame.setEnabled(false);
于 2020-07-22T18:29:45.517 回答
0

在这种情况下,我所做的是,在我想要保持可见的主 jframe(例如,菜单框架)中,我取消选择focusableWindowState属性窗口中的选项,因此它将是FALSE. 一旦完成,我调用的 jframe 在我关闭它们之前不会失去焦点。

于 2014-10-31T06:40:15.873 回答
0

正如其他人提到的,您可以使用 JDialog。如果您无权访问父框架,或者您想冻结洞应用程序,只需将 null 作为父框架传递:

final JDialog frame = new JDialog((JFrame)null, frameTitle, true); frame.setModal(true);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);

于 2019-08-16T15:13:01.653 回答
-1

有一些代码可能会有所帮助:

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class ModalJFrame extends JFrame {

    Object currentWindow = this;

    public ModalJFrame() 
    {
        super();
        super.setTitle("Main JFrame");
        super.setSize(500, 500);
        super.setResizable(true);
        super.setLocationRelativeTo(null);

        JMenuBar menuBar = new JMenuBar();
        super.setJMenuBar(menuBar);

        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");

        menuBar.add(fileMenu);
        menuBar.add(editMenu);

        JMenuItem newAction = new JMenuItem("New");
        JMenuItem openAction = new JMenuItem("Open");
        JMenuItem exitAction = new JMenuItem("Exit");
        JMenuItem cutAction = new JMenuItem("Cut");
        JMenuItem copyAction = new JMenuItem("Copy");
        JMenuItem pasteAction= new JMenuItem("Paste");

        fileMenu.add(newAction);
        fileMenu.add(openAction);
        fileMenu.addSeparator();
        fileMenu.add(exitAction);

        editMenu.add(cutAction);
        editMenu.add(copyAction);
        editMenu.addSeparator();
        editMenu.add(pasteAction);

        newAction.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {

                JFrame popupJFrame = new JFrame();

                popupJFrame.addWindowListener(new WindowAdapter()
                {
                      public void windowClosing(WindowEvent e) 
                      {
                          ((Component) currentWindow).setEnabled(true);                     }
                      });

                ((Component) currentWindow).setEnabled(false);
                popupJFrame.setTitle("Pop up JFrame");
                popupJFrame.setSize(400, 500);
                popupJFrame.setAlwaysOnTop(true);
                popupJFrame.setResizable(false);
                popupJFrame.setLocationRelativeTo(getRootPane());
                popupJFrame.setVisible(true);
                popupJFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            }
        });

        exitAction.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                System.exit(0);
            }
        });
    }
    public static void main(String[] args) {

        ModalJFrame myWindow = new ModalJFrame();
        myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myWindow.setVisible(true);
    }
}
于 2013-05-17T07:33:16.637 回答
-2

不确定您的 JFrame 的内容,如果您询问用户的一些输入,您可以使用 JOptionPane,这也可以将 JFrame 设置为模态

            JFrame frame = new JFrame();
            String bigList[] = new String[30];

            for (int i = 0; i < bigList.length; i++) {
              bigList[i] = Integer.toString(i);
            }

            JOptionPane.showInputDialog(
                    frame, 
                    "Select a item", 
                    "The List", 
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    bigList,
                    "none");
            }
于 2012-10-23T15:08:20.583 回答
-3

最简单的方法是在可视化JFrame对象之前使用pack()方法。这是一个例子:

myFrame frm = new myFrame();
frm.pack();
frm.setVisible(true);
于 2017-03-25T17:07:51.810 回答