java实现点击按钮事件弹出子窗口
更新时间:2019年07月31日 09:28:13 作者:NPC27149
这篇文章主要为大家详细介绍了java实现点击按钮事件弹出子窗口,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了java实现点击按钮事件弹出子窗口的具体代码,供大家参考,具体内容如下
要求:
1、在父窗口中添加一个按钮
2、点击按钮弹出子窗口
注意:这是JDK1.7版本
在JDK1.7之前,JFrame是不能直接添加子窗口的,要先将JInternalFrame添加到JDesktopPane中,再将JDesktopPane添加到父窗口内,完成这个操作。
(一)建立父类JFrame
package com.java.view; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JDesktopPane; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JMenuBar; public class Testfrm extends JFrame { /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Testfrm frame = new Testfrm(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Testfrm() { setTitle("\u7236\u7A97\u53E3");//标题 setBounds(400, 300, 800, 600);//父窗口的坐标和大小 getContentPane().setLayout(null);//自由布局 JButton bt = new JButton("\u6309\u94AE");//按钮的变量名为bt bt.setBounds(0, 0, 93, 23);//按钮的位置坐标和大小 getContentPane().add(bt);//按钮添加到窗口中 bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Testinterfrm testinterfrm=new Testinterfrm();//新建子窗口对象 testinterfrm.setVisible(true);//子窗口可见 getContentPane().add(testinterfrm);//子窗口添加到父窗口中 } }); } }
(二)建立子类JInternalFrame
package com.java.view; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JDesktopPane; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JMenuBar; public class Testfrm extends JFrame { /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Testfrm frame = new Testfrm(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Testfrm() { setTitle("\u7236\u7A97\u53E3");//标题 setBounds(400, 300, 800, 600);//父窗口的坐标和大小 getContentPane().setLayout(null);//自由布局 JButton bt = new JButton("\u6309\u94AE");//按钮的变量名为bt bt.setBounds(0, 0, 93, 23);//按钮的位置坐标和大小 getContentPane().add(bt);//按钮添加到窗口中 bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Testinterfrm testinterfrm=new Testinterfrm();//新建子窗口对象 testinterfrm.setVisible(true);//子窗口可见 getContentPane().add(testinterfrm);//子窗口添加到父窗口中 } }); } }
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Swagger2不被SpringSecurity框架拦截的配置及说明
这篇文章主要介绍了Swagger2不被SpringSecurity框架拦截的配置及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-03-03教你从头开始用JAVA创建一个自己的简单API并实现第三方调用
在日常开发的时候,经常会遇到需要调用别人的接口的场景,下面这篇文章主要给大家介绍了关于如何从头开始用JAVA创建一个自己的简单API并实现第三方调用的相关资料,需要的朋友可以参考下2023-12-12Springboot实现人脸识别与WebSocket长连接的实现代码
这篇文章主要介绍了Springboot实现人脸识别与WebSocket长连接的实现,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧2023-11-11
最新评论