java实现点击按钮事件弹出子窗口

所属分类: 软件编程 / java 阅读数: 52
收藏 0 赞 0 分享

本文实例为大家分享了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);//子窗口添加到父窗口中 
 }
 }); 
 }
}

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java基于反射机制实现全部注解获取的方法示例

这篇文章主要介绍了Java基于反射机制实现全部注解获取的方法,结合实例形式分析了java反射机制获取注解的具体实现方法与操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java 信号量Semaphore的实现

这篇文章主要介绍了Java 信号量Semaphore的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

eclipse+maven+spring mvc项目基本搭建过程

这篇文章主要介绍了eclipse+maven+spring mvc项目基本搭建过程,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring boot集成swagger2生成接口文档的全过程

这篇文章主要给大家介绍了关于Spring boot集成swagger2生成接口文档的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Java冒泡排序法和选择排序法的实现

这篇文章主要介绍了Java冒泡排序法和选择排序法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Cloud Alibaba教程之Sentinel的使用

这篇文章主要介绍了Spring Cloud Alibaba教程之Sentinel的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Josephus环的四种解法(约瑟夫环)基于java详解

这篇文章主要介绍了Josephus环的四种解法(约瑟夫环)基于java详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java继承Thread类创建线程类示例

这篇文章主要介绍了Java继承Thread类创建线程类,结合实例形式分析了java线程操作相关使用技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java使用Callable和Future创建线程操作示例

这篇文章主要介绍了Java使用Callable和Future创建线程操作,结合实例形式分析了java使用Callable接口和Future类创建线程的相关操作技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

springBoot使用JdbcTemplate代码实例

这篇文章主要介绍了springBoot使用JdbcTemplate代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多