我创建了一个GUI,在其中使用了JFrame。如何使它成为模态窗口?
我创建了一个GUI,在其中使用了JFrame。如何使它成为模态窗口?
Frame parentFrame
模态的 JDialog
中显示一个JPanel panel
。除了构造函数之外,这与打开 JFrame
的过程相同。final JDialog frame = new JDialog(parentFrame, frameTitle, true);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
编辑:更新了Modality 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中构建表单并使其成为模态
setModal(true);
.. 这太棒了。 - db2791YourJPanelForm stuff = new YourJPanelForm();
JOptionPane.showMessageDialog(null,stuff,"Your title here bro",JOptionPane.PLAIN_MESSAGE);
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 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);
}
对我有效的唯一代码:
childFrame.setAlwaysOnTop(true);
parentFrame.setFocusableWindowState(false);
this.mainFrame.setEnabled(false);
public static void showFrameAsDialog(Component parentComponent, JFrame frame)
{
try
{
JOptionPane pane = new JOptionPane(frame.getContentPane(), JOptionPane.PLAIN_MESSAGE,
JOptionPane.NO_OPTION, null,
new Object[]
{
}, null);
pane.setComponentOrientation(((parentComponent == null)
? getRootFrame() : parentComponent).getComponentOrientation());
int style = JRootPane.PLAIN_DIALOG;
Method method = pane.getClass().getDeclaredMethod("createDialog", Component.class, String.class, int.class);
method.setAccessible(true);
Object objDialog = method.invoke(pane, parentComponent, frame.getTitle(), style);
JDialog dialog = (JDialog) objDialog;
if (frame.getWidth() > dialog.getWidth() || frame.getHeight() > dialog.getHeight())
{
dialog.setSize(frame.getWidth(), frame.getHeight());
dialog.setLocationRelativeTo(parentComponent);
}
frame.addWindowListener(new java.awt.event.WindowAdapter()
{
@Override
public void windowClosed(java.awt.event.WindowEvent windowEvent)
{
dialog.dispose();
}
});
dialog.show();
dialog.dispose();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
正如其他人提到的,您可以使用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);