好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java GUI实现多个窗口切换效果

本文实例为大家分享了Java GUI实现多个窗口切换效果的具体代码,供大家参考,具体内容如下

功能:

主要实现的功能为实现多个界面的切换,并且一个window的打开和关闭可以影响其他window。

不足:

①可以多次多开同一个界面(可以加一个变量控制)
②没有实现一个的窗体关闭,它的子窗体也随即关闭的效果

效果图:

第一个界面(主界面)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

package 多界面跳转;

 

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 );

     }

}

第二个界面

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

package 多界面跳转;

 

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();

     }

}

由第一个界面打开的界面

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

package 多界面跳转;

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

        

     }

}

由第二个界面打开的界面

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package 多界面跳转;

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 );

     }

}

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

原文链接:https://blog.csdn.net/qq_44846324/article/details/104509238

查看更多关于Java GUI实现多个窗口切换效果的详细内容...

  阅读:28次