1. about java Of GUI Simple switching interface

At the beginning of learning java Of gui Interface design because the textbook does not have relevant switching interface introduction , So how to make a simple interface switch has puzzled me for a long time . So I read some code on the Internet , It turns out that most of the code on the Internet is cumbersome , If you look at it carefully, there is a lot of redundancy , Although those codes achieve the purpose of switching the interface , But there are other features that I can't use for a while .
In short , This part of the online code is not concise enough . So I started to separate this simple function . If you don't say much nonsense, you'd better code it first , Everything you want to say is in the notes . View part code :
import java.awt.BorderLayout; import javax.swing.*; public class View extends
JFrame { public void view() { ViewAction exwpAction=new
ViewAction(this);// Define the action object in advance setLayout(new BorderLayout());// use BorderLayout layout
setSize(400,400);// Set interface size this.setTitle(" view 1 Interface ");// Set interface name JButton eb=new
JButton(" view 2");// Set the switch button eb JLabel tip_l=new
JLabel(" view 1",0);// Content of interface , A label is used instead of the specific content , hinder “0” Is to center the label
eb.addActionListener(exwpAction);// by eb This button adds key monitoring add(tip_l,
BorderLayout.CENTER);// take tip_l This tag is added to the middle of the layout add(eb,
BorderLayout.SOUTH);// Press the button eb Add to the bottom of the layout setVisible(true);// Visualize the view } public void
view2() { ViewAction exwpAction=new ViewAction(this); setLayout(new
BorderLayout()); setSize(400,400); this.setTitle(" view 2 Interface "); JButton eb=new
JButton(" view 1"); JLabel tip_l=new JLabel(" view 2",0);
eb.addActionListener(exwpAction); add(tip_l, BorderLayout.CENTER); add(eb,
BorderLayout.SOUTH); setVisible(true); } }
Action part code :
import java.awt.event.ActionEvent; public class ViewAction implements
java.awt.event.ActionListener { /* *
This part is because I divide the interface and the action into two classes to write, so I need to pass the object. If the action and interface are written in one object, there is no need to pass in the object */ View v=new
View();// First define a View Object will be used when switching the interface public ViewAction(View v)// Will the outside world View In this class {
this.v=v; } public void actionPerformed(ActionEvent event) { Object
object=event.getSource();// Create event source object switch(event.getActionCommand()){ // If you press view 1
case " view 1": // The reason for creating a new view before closing the old view here is that, on the other hand, we find that the delay seems a little uncomfortable new
View().view();//new One View Class and call the view function v.setVisible(false);// Close the view of the penetrated class
break; // If you press view 2 case " view 2": new View().view2(); v.setVisible(false); } } }
Main class part code :
public class Main { public static void main(String[] args) { View v=new
View(); v.view(); } }
Operation effect :
After running the main class :

Click on the view 2 after

Click on the view 1 after

Technology