Implement panel switching in java Singleton mode and panel in java sample mode

Source: Internet
Author: User

Implement panel switching in java Singleton mode and panel in java sample mode

1. First, we will introduce what is the singleton mode:

Java Singleton mode is a common design mode. Let's look at the lazy mode first:

Public class Singleton _ {// set as private method to prevent external class reference or instance private Singleton _ () {System. out. println ("lazy Singleton mode");} private static Singleton _ single = null; // only the getInstance () method is exposed to the outside. This is the only way to obtain an instance. Public static Singleton _ getInstance () {if (single = null) single = new Singleton _ (); return single ;}}

This does not take thread security into consideration, because it is easy to have multiple instances in the case of concurrent threads, so this is a thread unsafe mode. There are also the following patterns:

// Hunger type Singleton type. during class initialization, public class Singleton1 {private Singleton1 () {} private static final Singleton1 single = new Singleton1 (); // only the getInstance () method is exposed, this is the only way to obtain an instance. Public static Singleton1 getInstance () {return single ;}}

This mode has been instantiated during class initialization, ensuring the only instance, which is thread-safe.

2. After a simple understanding of the singleton mode, let's use a specific instance to see how to implement a JFrame interface switch with only one main form, without the need, it is constantly instantiated and destroyed.

The main class inherits JFrame, so you can directly instantiate this class and display it.

1 import javax. swing. JFrame; 2 3 public class Singleton _ extends JFrame {4 private static Singleton _ single = null; 5 // only the getInstance () method is exposed to the outside, which is the only method for obtaining an instance. 6 public static Singleton _ getInstance () {7 if (single = null) 8 single = new Singleton _ (); 9 return single; 10} 11 public static void main (String args []) {12 Singleton _ singleton _ = new Singleton _ (); // instantiate a unique window with 13 singleton _. setTitle ("singleton mode window"); 14 singleton _. setVisible (true); 15} 16 // set to private method to prevent being referenced by external classes or instance 17 private Singleton _ () {18 setBounds (100,100,461,286 ); 19 setContentPane (Panel_01.getInstance (this); 20} 21}

This is a JPanel class, which is also a singleton mode and returns a JPanel instance.

1 import java. awt. event. actionEvent; 2 import java. awt. event. actionListener; 3 4 import javax. swing. JButton; 5 import javax. swing. JFrame; 6 import javax. swing. JPanel; 7 8 public class Panel_01 extends JPanel {9 private JButton but_01; 10 private JPanel jPanel; 11 private Panel_01 (final JFrame jFrame) {12 setLayout (null); 13 System. out. println ("normal"); 14 but_01 = new JButton ("interface 1"); 15 // Click Event 16 but_01.addActionListener (new ActionListener () {17 18 @ Override19 public void actionreceivmed (ActionEvent e) {20 if (e. getSource () = but_01) {21 jFrame. setContentPane (Panel_02.getInstance (jFrame); 22 jFrame. validate (); // make the Panel take effect, refresh 23} 24} 25}); 26 but_01.setBounds (120, 45, 71, 28); 27 jPanel = new JPanel (); 28. jPanel. setLayout (null); 29 jPanel. setBounds (0, 76,450,224); 30 jPanel. add (but_01); 31 add (jPanel); 32} 33 private static Panel_01 panel_01 = null; 34 public static Panel_01 getInstance (JFrame jFrame) {35 panel_01 = new Panel_01 (jFrame ); 36 return panel_01; 37} 38}

The second JPanel class

1 import java. awt. event. actionEvent; 2 import java. awt. event. actionListener; 3 4 import javax. swing. JButton; 5 import javax. swing. JFrame; 6 import javax. swing. JLabel; 7 import javax. swing. JPanel; 8 9 public class Panel_02 extends JPanel {10 private JLabel jLabel = null; 11 private JButton but_02 = null; 12 // private Method 13 private Panel_02 (JFrame jFrame) {14 jLabel = new JLabel ("interface 1"); 15 jLabel. setBounds (0, 0,100,100); 16 but_02 = new JButton ("back"); 17 // Click Event 18 but_02.addActionListener (new ActionListener () {19 20 @ Override21 public void actionreceivmed (ActionEvent e) {22 if (e. getSource () = but_02) {23 jFrame. setContentPane (Panel_01.getInstance (jFrame); 24 jFrame. validate (); // refresh 25} 26} 27}); 28 jLabel. setBounds (100,100,100,100); 29 add (but_02); 30 add (jLabel); 31} 32 private static Panel_02 panel_02 = null; 33 // external interface 34 public static Panel_02 getInstance (JFrame jFrame) {35 panel_02 = new Panel_02 (jFrame); 36 return panel_02; 37} 38}

In this case, only one JFrame window is implemented for the class. When using the function, only the JPanel in the class is continuously switched without affecting the status of the main form. Generally, it is better to use the singleton mode for this type of interface. (This is my first blog. If you have any problems, please point out that I will keep improving and updating. Thank you !)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.