本文實(shí)例為大家分享了java單例模式實(shí)現(xiàn)面板切換的具體代碼,供大家參考,具體內(nèi)容如下
1、首先介紹一下什么是單例模式:
java單例模式是一種常見(jiàn)的設(shè)計(jì)模式,那么我們先看看懶漢模式:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class Singleton_ { //設(shè)為私有方法,防止被外部類引用或?qū)嵗?/code> private Singleton_(){ System.out.println( "懶漢單例模式" ); } private static Singleton_ single = null ; //并對(duì)外只暴露getInstance()方法,這是獲取唯一實(shí)例方法。 public static Singleton_ getInstance(){ if (single== null ) single = new Singleton_(); return single; } } |
這是沒(méi)有考慮線程安全問(wèn)題的,因?yàn)樵诰€程并發(fā)的情況下,容易有多個(gè)實(shí)例,所以這是個(gè)線程不安全的模式。還有像餓漢模式這樣的:
1
2
3
4
5
6
7
8
9
|
//餓漢式單例類.在類初始化時(shí),已經(jīng)自行實(shí)例化 public class Singleton1 { private Singleton1() {} private static final Singleton1 single = new Singleton1(); //對(duì)外只暴露getInstance()方法,這是獲取唯一實(shí)例方法。 public static Singleton1 getInstance() { return single; } } |
這個(gè)模式在類初始化的時(shí)候就已經(jīng)實(shí)例化了,保證了唯一一個(gè)實(shí)例,這是線程安全的。
2、簡(jiǎn)單了解了什么是單例模式后,我們來(lái)用一個(gè)具體實(shí)例看看,怎么樣實(shí)現(xiàn)在JFrame界面切換的時(shí)候可以只有一個(gè)主窗體,而不需要,不停地實(shí)例化和銷毀它。
主類繼承JFrame,所以直接實(shí)例化本類并顯示就可以了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import javax.swing.JFrame; public class Singleton_ extends JFrame{ private static Singleton_ single = null ; //對(duì)外只暴露getInstance()方法,這是獲取唯一實(shí)例方法。 public static Singleton_ getInstance(){ if (single== null ) single = new Singleton_(); return single; } public static void main(String args[]){ Singleton_ singleton_ = new Singleton_(); //實(shí)例化唯一窗口與 singleton_.setTitle( "單例模式窗口" ); singleton_.setVisible( true ); } //設(shè)為私有方法,防止被外部類引用或?qū)嵗?/code> private Singleton_(){ setBounds( 100 , 100 , 461 , 286 ); setContentPane(Panel_01.getInstance( this )); } } |
這是一個(gè)JPanel面板類,他也是一個(gè)單例模式,返回的是一個(gè)JPanel實(shí)例。
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
|
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Panel_01 extends JPanel{ private JButton but_01; private JPanel jPanel; private Panel_01( final JFrame jFrame){ setLayout( null ); System.out.println( "正常" ); but_01 = new JButton( "界面1" ); //點(diǎn)擊事件 but_01.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (e.getSource()==but_01){ jFrame.setContentPane(Panel_02.getInstance(jFrame)); jFrame.validate(); //使面板生效,刷新 } } }); but_01.setBounds( 120 , 45 , 71 , 28 ); jPanel = new JPanel(); jPanel.setLayout( null ); jPanel.setBounds( 0 , 76 , 450 , 224 ); jPanel.add(but_01); add(jPanel); } private static Panel_01 panel_01 = null ; public static Panel_01 getInstance(JFrame jFrame){ panel_01 = new Panel_01(jFrame); return panel_01; } } |
第二個(gè)JPanel類
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
|
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Panel_02 extends JPanel{ private JLabel jLabel = null ; private JButton but_02 = null ; //私有方法 private Panel_02(JFrame jFrame){ jLabel = new JLabel( "界面1" ); jLabel.setBounds( 0 , 0 , 100 , 100 ); but_02 = new JButton( "返回" ); //點(diǎn)擊事件 but_02.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (e.getSource()==but_02){ jFrame.setContentPane(Panel_01.getInstance(jFrame)); jFrame.validate(); //刷新 } } }); jLabel.setBounds( 100 , 100 , 100 , 100 ); add(but_02); add(jLabel); } private static Panel_02 panel_02= null ; //對(duì)外接口 public static Panel_02 getInstance(JFrame jFrame){ panel_02 = new Panel_02(jFrame); return panel_02; } } |
本個(gè)案例主要實(shí)現(xiàn)了類只有一個(gè)JFrame窗口,而在使用功能的時(shí)候,只有里面的JPanel面板不斷的切換,不影響主窗體的狀態(tài)。而且一般這種類型的界面最好都是采用單例模式會(huì)好一點(diǎn)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/caijh/p/6096240.html