java新建jframe_如何在Swing java中创建JFrame模型
回答(11)
2 years ago
下面是一些示例代码,它将在 JDialog 中显示 JPanel panel ,它是 Frame parentFrame 的模态 . 除了构造函数之外,它遵循与打开 JFrame 相同的模式 .
final JDialog frame = new JDialog(parentFrame, frameTitle, true);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
编辑:更新了Modality API链接并添加了教程链接(点头向@spork点头) .
2 years ago
您可以创建一个传递对父 JFrame 的引用的类,并将其保存在 JFrame 变量中 . 然后,您可以锁定创建新框架的框架 .
parentFrame.disable();
//Some actions
parentFrame.enable();
2 years ago
创建一个新的JPanel表单
将所需的组件和代码添加到其中
YourJPanelForm stuff = new YourJPanelForm();
JOptionPane.showMessageDialog(null,stuff,"Your title here bro",JOptionPane.PLAIN_MESSAGE);
您的模态对话框等待...
2 years ago
在课堂上将 JFrame 替换为 JDialog
public class MyDialog extends JFrame // delete JFrame and write JDialog
然后在构造函数中编写 setModal(true);
之后,您将能够在netbeans中构建表单,表单将变为模态
2 years ago
据我所知,JFrame无法进行模态模式 . 请改用JDialog并调用setModalityType(Dialog.ModalityType type)将其设置为模态(或非模态) .
2 years ago
如果您准备使用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();
}
}
2 years ago
这个静态实用程序方法也通过秘密打开模态JDialog来显示模态JFrame . 我在Windows 7,8和10-with-multiple-desktops上成功地使用了这个并且具有适当的行为 .
对于很少使用的本地类功能,这是一个很好的例子 .
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.
*
* A hidden modal JDialog is "shown" to effect the modality.
*
* 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);
}
2 years ago
有一些代码可能会有所帮助:
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);
}
}
2 years ago
我在这种情况下所做的是,在我希望保持可见的主jframe中(例如,菜单框),我在属性窗口中取消选择 focusableWindowState 选项,这样它将是 FALSE . 一旦完成,我调用的jframe不会失去焦点,直到我关闭它们 .
2 years ago
不确定你的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");
}
2 years ago
最简单的方法是在可视化JFrame对象之前使用pack()方法 . 这是一个例子:
myFrame frm = new myFrame();
frm.pack();
frm.setVisible(true);
java新建jframe_如何在Swing java中创建JFrame模型相关推荐
- react中纯函数_如何在纯React中创建电子邮件芯片
react中纯函数 by Andreas Remdt 由Andreas Remdt 如何在纯React中创建电子邮件芯片 (How to create email chips in pure Reac ...
- vfp生成菜单时文件不存在_如何在VFP项目中创建菜单
接上面两篇文章! 本文,我们来学习一下如何在VFP项目中创建菜单! 打开上文所讲的简单的项目文件,切换到"其他"选项卡! 如上图,选择"菜单"一项,之后点击&q ...
- [DT框架使用教程01]如何在DT框架中创建插件
[DT框架使用教程01]如何在DT框架中创建插件 DT框架代码地址: https://github.com/huifeng-kooboo/DT 由于国内访问速度的问题 也可以访问gitee的地址: h ...
- 如何在sqlite3连接中创建并调用自定义函数
#!/user/bin/env python # @Time :2018/6/8 14:44 # @Author :PGIDYSQ #@File :CreateFunTest.py '''如何在sql ...
- 如何在ORACLE CLOUD中创建和访问容器集群丨内附官方文档链接
墨墨导读:本文描述如何在Oracle Cloud中创建并访问容器服务.为了简单,所有的操作都是针对root隔离区. 创建允许容器运行的政策官方文档链接 这一步是必须的,否则可以增加容器容器. 官方文档 ...
- 如何在React Native中创建精美的动画加载器
by Vikrant Negi 通过Vikrant Negi 如何在React Native中创建精美的动画加载器 (How to create a beautifully animated load ...
- 如何在geth中创建genesis.json_Adobe XD 入门教程-如何在 Adobe XD 中创建交互式原型?...
完成屏幕设计后,您可以将各屏幕互相连接,以便直观地了解用户如何体验您的应用程序或网站.Adobe XD 允许您创建交互式原型,直观地展示如何在屏幕或线框之间导航.您可以预览交互,验证用户体验并对设计进 ...
- 如何安装html启动器,如何在Spring Boot中创建自己的启动器?
如何在Spring Boot中创建自己的启动器? 也许你有一个内部库包或一个在整个应用程序环境中常用的开源库,如果要在多个Spring Boot应用程序中使用它,为它创建一个Spring Boot启动 ...
- adobe xd_如何在Adobe XD中创建Finance App UI设计
adobe xd 在本教程中,您将学习如何在Adobe XD中创建财务应用程序设计,以及(更重要的是)如何轻松对其UI的各个部分进行动画处理. 您将在本Adobe XD教程中学到什么 如何在Adobe ...
最新文章
- python 错误之SyntaxError: Missing parentheses in call to 'print'
- Linux中Eclipse开发android的配置问题
- 文巾解题 620. 有趣的电影
- 状态开关(ToggleButton)
- 十八.多个SLAM框架(A-LOAM、Lego-loam、LIO-SAM、livox-loam)室外测试效果粗略对比分析
- VC++中CEdit控件实现回车换行
- python工程师干什么的_大数据开发工程师薪资待遇及招聘要求?
- Matter-JS constraint 约束
- 青县计算机学校,青县将建设新学校(中小学各一所)今年3月开工,明年即可完工!...
- Node.js下载及安装步骤
- 使用SQL替换指定字符串
- MSM8926处理器
- Thingworx自定义扩展开发(二)- Widget Demo Coding
- 2021第五届航交会暨全球物流博览会向航运物流产业链人士发出邀约!金秋十月,共聚宁波!
- osd 显示 png格式图片水印
- 大数据时代隐私泄露呈高发趋势 如何有效预防成关键
- 数据包结构(IVI,NID,CTL,TTL,SEQ,SRC,DST,TransPDU,NetMIC)
- 基于pyautogui实现原神砍树、原神木材收集脚本
- 二叉树练习题及答案解析
- 百度智慧城市白皮书 附下载