如何在Swing java中创build一个JFrame模式

我创build了一个使用JFrame的GUI。 我应该如何使它Modal?

你最好的select是使用一个JDialog,而不是一个JFrame,如果你想使窗口模态。 有关详细信息,请参阅Java 6中的Modality API的介绍 。 还有一个教程 。

这里是一些示例代码,它将在一个JDialog显示一个JPanel panel ,这个JDialog模式是Frame parentFrame 。 除了构造函数外,这与打开JFrame模式相同。

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

编辑:更新Modality API链接和添加教程链接(点到@spork的凹凸)。

您可以创build一个传递给父JFrame的引用的类,并将其保存在JFramevariables中。 然后,您可以locking创build新框架的框架。

 parentFrame.disable(); //Some actions parentFrame.enable(); 
  1. 创build一个新的JPanel表单
  2. 添加你想要的组件和代码

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

你的模态对话正在等待…

只需将JFramereplace为类中的JDialog

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

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

之后,您将能够在NetBeans中构build您的表单,表单变为模态

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

如果您准备使用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(); } } 

有一些代码可以帮助:

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

我在这种情况下所做的是,在主要的jframe中,我想保持可见(例如,一个菜单框架),我取消select属性窗口中的选项focusableWindowState ,这将是FALSE 。 一旦完成,我所称的jframes不会失去焦点,直到closures它们。

这个静态工具方法也通过暗中打开一个模态JDialog来显示一个模态JFrame。 我成功地使用了这个function,并且在Windows 7,8和10个多桌面上运行正常。

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

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

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

 myFrame frm = new myFrame(); frm.pack(); frm.setVisible(true); 

不确定你的JFrame的情况,如果你问一些用户的input,你可以使用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"); }