java實(shí)現(xiàn)點(diǎn)擊按鈕事件彈出子窗口
本文實(shí)例為大家分享了java實(shí)現(xiàn)點(diǎn)擊按鈕事件彈出子窗口的具體代碼,供大家參考,具體內(nèi)容如下
要求:
1、在父窗口中添加一個按鈕
2、點(diǎn)擊按鈕彈出子窗口
注意:這是JDK1.7版本
在JDK1.7之前,JFrame是不能直接添加子窗口的,要先將JInternalFrame添加到JDesktopPane中,再將JDesktopPane添加到父窗口內(nèi),完成這個操作。
(一)建立父類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");//標(biāo)題
setBounds(400, 300, 800, 600);//父窗口的坐標(biāo)和大小
getContentPane().setLayout(null);//自由布局
JButton bt = new JButton("\u6309\u94AE");//按鈕的變量名為bt
bt.setBounds(0, 0, 93, 23);//按鈕的位置坐標(biāo)和大小
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");//標(biāo)題
setBounds(400, 300, 800, 600);//父窗口的坐標(biāo)和大小
getContentPane().setLayout(null);//自由布局
JButton bt = new JButton("\u6309\u94AE");//按鈕的變量名為bt
bt.setBounds(0, 0, 93, 23);//按鈕的位置坐標(biāo)和大小
getContentPane().add(bt);//按鈕添加到窗口中
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Testinterfrm testinterfrm=new Testinterfrm();//新建子窗口對象
testinterfrm.setVisible(true);//子窗口可見
getContentPane().add(testinterfrm);//子窗口添加到父窗口中
}
});
}
}
運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于json解析多層嵌套并轉(zhuǎn)為對應(yīng)類(List)
在進(jìn)行JSON解析時,遇到多層嵌套結(jié)構(gòu)可通過遞歸或?qū)S脦靵韺?shí)現(xiàn),重要的是將嵌套的JSON對象準(zhǔn)確轉(zhuǎn)化為對應(yīng)的Java類,通常需要依賴如Gson或Jackson等庫,將JSONObject轉(zhuǎn)為JavaBean時,關(guān)注字段匹配與數(shù)據(jù)類型轉(zhuǎn)換2024-10-10
Java開發(fā)者就業(yè)需要掌握的9大專業(yè)技能
這篇文章主要為大家詳細(xì)介紹了java就業(yè)前需要掌握的專業(yè)技能,感興趣的小伙伴們可以參考一下2016-09-09
SpringCloud-Hystrix實(shí)現(xiàn)原理總結(jié)
通過hystrix可以解決雪崩效應(yīng)問題,它提供了資源隔離、降級機(jī)制、融斷、緩存等功能。接下來通過本文給大家分享SpringCloud-Hystrix實(shí)現(xiàn)原理,感興趣的朋友一起看看吧2021-05-05
MyBatis中的@SelectProvider注解源碼分析
這篇文章主要介紹了MyBatis中的@SelectProvider注解源碼分析,@SelectProvider功能就是用來單獨(dú)寫一個class類與方法,用來提供一些xml或者注解中不好寫的sql,今天就來說下這個注解的具體用法與源碼,需要的朋友可以參考下2024-01-01
Java中類轉(zhuǎn)json的基類實(shí)現(xiàn)
這篇文章主要介紹了Java中類轉(zhuǎn)json的基類實(shí)現(xiàn),需要的朋友可以參考下2021-01-01
SpringBoot添加SSL證書,開啟HTTPS方式(單向認(rèn)證服務(wù)端)
這篇文章主要介紹了SpringBoot添加SSL證書,開啟HTTPS方式(單向認(rèn)證服務(wù)端),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Java 線程的生命周期詳細(xì)介紹及實(shí)例代碼
這篇文章主要介紹了Java 線程的生命周期的相關(guān)資料,并附簡單實(shí)例代碼,幫助大家理解,需要的朋友可以參考下2016-10-10

