如何使JFrame模式在摆java
-
18-09-2019 - |
题
我已经创建了一个图形用户界面我使用一个JFrame.我应该如何使它模式?
解决方案
您最好的选择是,如果你想在窗口模式使用一个JDialog,而不是一个JFrame的。请查看引进形态API的rel="noreferrer">细节对于信息。还有教程。
下面是一些示例代码,这将在一个JPanel panel
这是模态到JDialog
显示Frame 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 和形式变为模式
- 创建一个新的JPanel形式
- 添加所需的部件和代码
YourJPanelForm stuff = new YourJPanelForm();
JOptionPane.showMessageDialog(null,stuff,"Your title here bro",JOptionPane.PLAIN_MESSAGE);
你的模式对话等待着...
据我所知,JFrame中不能做模态模式。使用的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();
}
}
此静态实用方法示出了通过打开偷偷一个模态的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);
}
}
我在这种情况下做什么,因为我想保持可见主要的JFrame(例如,菜单帧),我取消选择该选项focusableWindowState
在属性窗口,将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");
}
在最简单的方法是使用 pack()的可视化的的JFrame 对象之前方法。这里是一个例子:
myFrame frm = new myFrame();
frm.pack();
frm.setVisible(true);