Java?GUI實(shí)現(xiàn)多個(gè)窗口切換效果
本文實(shí)例為大家分享了Java GUI實(shí)現(xiàn)多個(gè)窗口切換效果的具體代碼,供大家參考,具體內(nèi)容如下
功能:
主要實(shí)現(xiàn)的功能為實(shí)現(xiàn)多個(gè)界面的切換,并且一個(gè)window的打開和關(guān)閉可以影響其他window。
不足:
①可以多次多開同一個(gè)界面(可以加一個(gè)變量控制)
②沒有實(shí)現(xiàn)一個(gè)的窗體關(guān)閉,它的子窗體也隨即關(guān)閉的效果
效果圖:




第一個(gè)界面(主界面)
package 多界面跳轉(zhuǎn);
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
class Frame1 extends JFrame implements WindowListener
{
?? ?JButton b2 = new JButton("界面2");
?? ?JButton b11 = new JButton("界面11");
?? ?
?? ?private class btListener implements ActionListener
?? ?{
?? ??? ?public void actionPerformed(ActionEvent e) {
?? ??? ??? ?if(e.getActionCommand().equals("界面2"))?
?? ??? ??? ?{
?? ??? ??? ??? ?setVisible(false);
?? ??? ??? ??? ?new Frame2();
?? ??? ??? ?}
?? ??? ??? ?else if(e.getActionCommand().equals("界面11"))
?? ??? ??? ?{
?? ??? ??? ??? ?new Frame11();
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?
?? ?public Frame1()
?? ?{
?? ??? ?this.setTitle("界面1");
?? ??? ?this.setSize(400, 300);
?? ??? ?this.setLayout(new FlowLayout());
?? ??? ?b11.addActionListener(new btListener());
?? ??? ?b2.addActionListener(new btListener());
?? ??? ?this.add(b11);
?? ??? ?this.add(b2);
?? ??? ?this.setDefaultCloseOperation(EXIT_ON_CLOSE);
?? ??? ?this.setLocationRelativeTo(null);
?? ?}
?? ?public void windowOpened(WindowEvent e) {
?? ?}
?? ?public void windowClosing(WindowEvent e) {
?? ??? ?setVisible(true);
?? ?}
?? ?public void windowClosed(WindowEvent e) {
?? ?}
?? ?public void windowIconified(WindowEvent e) {
?? ?}
?? ?public void windowDeiconified(WindowEvent e) {
?? ?}
?? ?public void windowActivated(WindowEvent e) {
?? ?}
?? ?public void windowDeactivated(WindowEvent e) {
?? ?}
?? ?public static void main(String[] args) {
?? ??? ?Frame1 f1 = new Frame1();
?? ??? ?f1.setVisible(true);
?? ?}
}第二個(gè)界面
package 多界面跳轉(zhuǎn);
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class Frame2 extends JFrame implements ActionListener
{
?? ?JButton bt = new JButton("界面21");
?? ?Frame2()
?? ?{
?? ??? ?this.setSize(350, 300);
?? ??? ?this.setLocationRelativeTo(null);
?? ??? ?this.setLayout(new FlowLayout());
?? ??? ?this.setTitle("界面2");
?? ??? ?this.add(bt);
?? ??? ?bt.addActionListener(this);
?? ??? ?this.addWindowListener(new Frame1());
?? ??? ?this.addWindowListener(new Frame11());?? ??? ?
?? ??? ?this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
?? ??? ?this.setVisible(true);
?? ?}
?? ?@Override
?? ?public void actionPerformed(ActionEvent e) {
?? ??? ?new Frame21();
?? ?}
}由第一個(gè)界面打開的界面
package 多界面跳轉(zhuǎn);
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
class Frame11 extends JFrame implements WindowListener
{
?? ?Frame11()
?? ?{
?? ??? ?this.setSize(300, 200);
?? ??? ?this.setLocationRelativeTo(null);
?? ??? ?this.setTitle("界面11");
?? ??? ?this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
?? ??? ?this.setVisible(true);
?? ?}
?? ?@Override
?? ?public void windowOpened(WindowEvent e) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?
?? ?}
?? ?@Override
?? ?public void windowClosing(WindowEvent e) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?if(this.isVisible()) {
//?? ??? ??? ?setSize(800,600);
?? ??? ?}
?? ?}
?? ??? ?
?? ?@Override
?? ?public void windowClosed(WindowEvent e) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?
?? ?}
?? ?@Override
?? ?public void windowIconified(WindowEvent e) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?
?? ?}
?? ?@Override
?? ?public void windowDeiconified(WindowEvent e) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?
?? ?}
?? ?@Override
?? ?public void windowActivated(WindowEvent e) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?
?? ?}
?? ?@Override
?? ?public void windowDeactivated(WindowEvent e) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?
?? ?}
}由第二個(gè)界面打開的界面
package 多界面跳轉(zhuǎn);
import javax.swing.*;
import java.awt.*;
class Frame21 extends JFrame
{
?? ?Frame21()
?? ?{
?? ??? ?this.setSize(150, 100);
?? ??? ?this.setLocationRelativeTo(null);
?? ??? ?this.setTitle("界面21");
?? ??? ?this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
?? ??? ?this.setVisible(true);
?? ?}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Spring事件機(jī)制實(shí)現(xiàn)異步的方法
這篇文章主要介紹了使用Spring事件機(jī)制實(shí)現(xiàn)異步的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
Java輕松使用工具類實(shí)現(xiàn)獲取MP3音頻時(shí)長
在Java中,工具類定義了一組公共方法,這篇文章將介紹Java中使用工具類來獲取一個(gè)MP3音頻文件的時(shí)間長度,感興趣的同學(xué)繼續(xù)往下閱讀吧2021-10-10
Java Thread多線程開發(fā)中Object類詳細(xì)講解
這篇文章主要介紹了Java Thread多線程開發(fā)中Object類,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03
springboot?@PostConstruct無效的解決
這篇文章主要介紹了springboot?@PostConstruct無效的解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11

