Java?GUI實現(xiàn)多個窗口切換效果
本文實例為大家分享了Java GUI實現(xiàn)多個窗口切換效果的具體代碼,供大家參考,具體內(nèi)容如下
功能:
主要實現(xiàn)的功能為實現(xiàn)多個界面的切換,并且一個window的打開和關(guān)閉可以影響其他window。
不足:
①可以多次多開同一個界面(可以加一個變量控制)
②沒有實現(xiàn)一個的窗體關(guān)閉,它的子窗體也隨即關(guān)閉的效果
效果圖:
第一個界面(主界面)
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); ?? ?} }
第二個界面
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(); ?? ?} }
由第一個界面打開的界面
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 ?? ??? ? ?? ?} }
由第二個界面打開的界面
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實現(xiàn)Thymeleaf驗證碼生成
本文使用SpringBoot實現(xiàn)Thymeleaf驗證碼生成,使用后臺返回驗證碼圖片,驗證碼存到session中后端實現(xiàn)校驗,前端只展示驗證碼圖片。感興趣的可以了解下2021-05-05Java對象轉(zhuǎn)JSON時動態(tài)的增刪改查屬性詳解
這篇文章主要介紹了Java對象轉(zhuǎn)JSON時如何動態(tài)的增刪改查屬性的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11SpringBoot集成支付寶沙箱支付的實現(xiàn)示例
本文主要介紹了SpringBoot集成支付寶沙箱支付的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12