如何在 Swing java 中制作 JFrame Modal
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1481405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a JFrame Modal in Swing java
提问by om.
I have created one GUI in which I have used a JFrame. How should I make it Modal?
我创建了一个使用 JFrame 的 GUI。我应该如何使它成为模态?
采纳答案by akf
Your best bet is to use a JDialog instead of a JFrame if you want to make the window modal. Check out details on the introduction of the Modality API in Java 6for info. There is also a tutorial.
如果要使窗口模式化,最好的办法是使用 JDialog 而不是 JFrame。查看有关在 Java 6 中引入 Modality API 的详细信息以获取信息。还有一个教程。
Here is some sample code which will display a JPanel panel
in a JDialog
which is modal to Frame parentFrame
. Except for the constructor, this follows the same pattern as opening a JFrame
.
这是一些示例代码,它将JPanel panel
在JDialog
模态 to 中显示 a Frame parentFrame
。除了构造函数,这遵循与打开 a 相同的模式JFrame
。
final JDialog frame = new JDialog(parentFrame, frameTitle, true);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
Edit: updated Modality API link & added tutorial link (nod to @spork for the bump).
编辑:更新了 Modality API 链接并添加了教程链接(向 @spork 致敬)。
回答by NawaMan
As far as I know, JFrame cannot do Modal mode. Use JDialog instead and call setModalityType(Dialog.ModalityType type)
to set it to be modal (or not modal).
据我所知,JFrame 不能做模态模式。改用 JDialog 并调用setModalityType(Dialog.ModalityType type)
将其设置为模态(或非模态)。
回答by Kamil
You can create a class that is passed a reference to the parent JFrame
and holds it in a JFrame
variable. Then you can lock the frame that created your new frame.
您可以创建一个类,该类将引用传递给父项JFrame
并将其保存在JFrame
变量中。然后您可以锁定创建新框架的框架。
parentFrame.disable();
//Some actions
parentFrame.enable();
回答by Ryan
not sure the contetns of your JFrame, if you ask some input from users, you can use JOptionPane, this also can set JFrame as modal
不确定你的 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");
}
回答by Stephen Paul
If you're prepared to use a JDialog instead of a JFrame, you can set the ModalityTypeto APPLICATION_MODAL.
如果您准备使用 JDialog 而不是 JFrame,则可以将ModalityType设置为APPLICATION_MODAL。
This provides identical behaviour to your typical JOptionPane:
这提供了与典型 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();
}
}
回答by The Codartofonist
There's a bit of code that might help:
有一些代码可能会有所帮助:
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);
}
}
回答by Richard Kenneth Niescior
- Create a new JPanel form
- Add your desired components and code to it
- 创建一个新的 JPanel 表单
- 添加你想要的组件和代码
YourJPanelForm stuff = new YourJPanelForm();
JOptionPane.showMessageDialog(null,stuff,"Your title here bro",JOptionPane.PLAIN_MESSAGE);
Your modal dialog awaits...
您的模态对话框等待...
回答by Johan Rincon
What I've done in this case is, in the primary jframe that I want to keep visible (for example, a menu frame), I deselect the option focusableWindowState
in the property window so It will be FALSE
. Once that is done, the jframes I call don′t lose focus until I close them.
在这种情况下,我所做的是,在我希望保持可见的主 jframe(例如,菜单框架)中,我取消选择focusableWindowState
属性窗口中的选项,因此它将是FALSE
. 一旦完成,我调用的 jframe 不会失去焦点,直到我关闭它们。
回答by Ivan
just replace JFrame
to JDialog
in class
只需在课堂上替换JFrame
为JDialog
public class MyDialog extends JFrame // delete JFrame and write JDialog
and then write setModal(true);
in constructor
然后setModal(true);
在构造函数中写入
After that you will be able to construct your Form in netbeans and the form becomes modal
之后,您将能够在 netbeans 中构建您的表单,并且该表单将变为模态
回答by Dreamspace President
This static utility method shows a modal JFrame by secretly opening a modal JDialog, too. I used this successfully and with proper behavior on Windows 7, 8, and 10-with-multiple-desktops.
这个静态实用方法也通过秘密打开一个模态 JDialog 来显示一个模态 JFrame。我在 Windows 7、8 和 10-with-multiple-desktops 上成功地使用了它并且行为正确。
It's a nice example for the very rarely used feature of localclasses.
这是本地类很少使用的特性的一个很好的例子。
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);
}