質問

JFrame を使用した GUI を 1 つ作成しました。モーダルにするにはどうすればよいですか?

役に立ちましたか?

解決

あなたの最善の策は、ウィンドウをモーダルにしたい場合のJFrameの代わりに使って、JDialogを使用することです。 Java 6のの中モダリティAPIの導入に詳細をチェック情報のため。 チュートリアルでもあります。

ここJPanel panelするモーダルJDialogFrame parentFrameを表示するいくつかのサンプルコードです。コンストラクタを除いて、これはJFrameを開くと同じパターンに従う。

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

編集:更新されたモダリティのAPIリンク&追加チュートリアルリンク(うなずきバンプのため@sporkします)。

他のヒント

あなたは親JFrameへの参照を渡され、JFrame変数に保持されたクラスを作成することができます。次に、あなたの新しいフレームを作成したフレームをロックすることができます。

parentFrame.disable();

//Some actions

parentFrame.enable();

ちょうどクラスにJFrameするJDialogを置き換える

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

し、

コンストラクタでsetModal(true);を書きます

その後、あなたは、NetBeansでフォームを構築することができるようになります そして、フォームがモーダルになります。

  1. 新しい JPanel フォームを作成する
  2. 必要なコンポーネントとコードを追加します

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


モーダル ダイアログが待っています...

私の知る限りでは、JFrameのはモーダルモードを行うことはできません。使用JDialogの代わりと呼ん<のhref = "https://docs.oracle.com/javase/8/docs/api/java/awt/Dialog.html#setModalityType-java.awt.Dialog.ModalityType-" のrel =」 "nofollowをnoreferrer> setModalityType(Dialog.ModalityType type) には(モーダルか)それはモーダルに設定します。

あなたはJFrameのの代わりに使って、JDialogを使用する準備している場合は、

、あなたが設定できるのへた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();
}
}

この静的ユーティリティメソッドも、密かにモーダルJDialogのを開くことによって、モーダルのJFrameを示しています。私はこれに成功し、Windows 7、8上の適切な行動を、そして10-で-複数のデスクトップを使用ます。

これはのローカルのクラスます。

の非常に稀にしか使われない機能のために良い例です
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);
}

役立つ可能性のあるコードのビットがあります:

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

私はそれがfocusableWindowStateされますので、私は目に見える維持したい主なJFrameの中に(たとえば、メニューフレームのために)、私はプロパティウィンドウでオプションFALSEの選択を解除され、この場合には何をやりましたか。それが終わると、jframesは、私はそれらを閉じるまでフォーカスを失うドント呼び出します。

他の人が述べたように、

、あなたは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);

ではない、あなたのJFrameの内容は、あなたがユーザーからいくつかの入力を求めるなら、あなたはのJOptionPaneを使用することができ、これはまた、モーダルとしてのIFrameを設定することができますしてください。

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

最も簡単な方法は、のJFrameののオブジェクトを視覚化する前に、のパック()の方法を使用することです。ここでの例です。

myFrame frm = new myFrame();
frm.pack();
frm.setVisible(true);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top