Implementation of interface jump in Java

xiaoxiao2021-03-06  39

source:

http://blog.9cbs.net/jiziba/archive/2005/01/05/241609.aspx

About the interface jump problem in the system platform: (Java) assumes that our system interface uses the javax.swing package to build, the foundation of the interface is Baseview, he is a container, of course he should provide the control element Function, such as getting a button, pull-down box, table, etc., of course, is just a container, and all of our interfaces are deployed on JPANel. Description: An interface is a baseview, he only contains a JPanel, this contains JPanel All our Swing controls, such as JButton, JLABLE, and more. The problem has appeared: We usually do an interface to complete an interface to automatically jump to the next interface, complete the next interface and jump back (external: due to Our operation is based on GUI, so it is often possible to save the session information, and the web is not able to do), and this is often a factor in the process of system implementation, I will see that someone in the system is 600 lines. The code determines which one should be to jump, because many interfaces can jump to the current interface. Of course, there is a way to include the variables that point the upper interface in the next interface class, we said that this is inconvenient, Also added dependence, this is unfavorable. Next, I give my solution, I hope to benefit from friends who use this interface structure.

(The following is described in the simplified model.) 1. Simple point, we assume that baseview inherits jwindow, of course, can be other containers (according to your implementation), is like this: public Abstract Class Baseview EXTENDS JWINDOW {... Some ways to get interface controls, and interface information).} 2. Each interface class is like this: public class myview extends baseview {jPanel mypanel; public void playoutpanel () {jButton mybutton = new jbutton ("ok"); MyPanel.add (MyButton); ... (add the controls you need and layout to myPanel)}} 3. Suppose there are other interface OneView, Twoview, threeview, you need to jump to MyView, When the OK button in MyView needs to go back to the original interface. The original bloated code needs to add a variable baseview anyyview in myView; the interface used to store it, AnyView, assignment in the jump code in the three Quote MyView to set. Jump code like this: public void jump () {myview myview = new myview (); myview.Anyview = this; this.remove (this.GetPanel); this.add (MyView.getPanel () ); This.Repaint ();} It looks not bad, although it needs to reference the MyView class, and call his variables and methods. But jump back is not so easy, otherwise how to use 600 lines! It is like this: (this It is already simplified by me) public void goback () {ifview .Remove (this.myView); OneView OV = (oneView) AnyView; AnyView.Add (Ov.getPanel ()); AnyView. Repaint ();} if (anyview instanceof twoview) {....} ...}

This kind of compilation dependence is really not a good thing without a large number of applications, not to mention a lot of inefficient instanceOf judgments and transformation operations.

To optimize this situation, completely solve this problem, I want to design a third-party class to eliminate this dependency, and let the interface jump to be so hard. This third party is designed in this class, in this class, There must be a variable to save a path of a interface, such as A-> B-> c. Once the path is saved, you have the right to control any one of the interfaces. In this chain, the first location The interface should be the first stop of this jump, the last position is the current station. There is a causal relationship here: Only jump can jump back. This allows us to save this path with an array. Reality, jump The path should not exceed 10 times, so we set the path length to 10 (of course you can change as needed). This class looks a probably like this: class viewpath {jPanel [] pnlpath = null; // jump Interface path, interface jump up to 10 levels !!! int index = 0; // The current subscript of the current subscription = null; // The same view of the current path

/ / Looking for the target in the path PUBLIC INT FIND (JPANEL PNL) {// Is there a panel under this path, there is a message to the subscript, no words return -1 if (BSView == null) Return -1 ; // Nothing, there is no Panel for (int i = 0; i

// Constructor ViewPath (JPanel MyPanel, Baseview MyView) {PNLPath = New JPanel [10]; // Set the maximum length of 10

BSView = myview; // Set the view pnlpath [0] = myPanel; // set up the start station index = 0; // set up the starter index}}

Such a class has completed the role of saving a jump path. (Of course, if you should set up a target location in the Find method, if we use such a path? We set a secondary class to complete this work, we Named viewjump, we know that as a secondary class is best not to have instances, especially the classes like this, only a static method. Its framework is like this:

Public class viewjump {private static viewpath [] viewpath = null; // path pool, system multiple use, static but private, because

Private viewjump () {} // Private constructor, the auxiliary class only provides a static method Private Static int Find (JPanel PNL); / / Find a given Panel in an existing path, private private static int newpath (JPanel MyPanel, BaseView myview); // Establish a new path, private / ** * Every class requires the first step to register yourself, then you can do other operations * Return a registration code ID, the auxiliary class needs this Registration code for other operations * / public static int registerpath (JPanel MyPanel, BaseView MyView); / ** * Set up the next interface. * / Public static void setnext (int ID, jPanel aim); / ** * Back to previous Interface * / public static void back (int id); / ** * Back to the first interface * / public static void backhome (int id); / ** * Jump to the next interface * / public static void jump (PUBLIC Static Void JUMP) INT ID);} There are not many code volumes that complete such a class, but it makes the user completely detached from the troubles of processing different interfaces. The source code of the class will be attached, it is worth mentioning. This type of implementation can be used in a similar implementation, but if the user's interface structure is not so built, you need to change the parameter type. If you can get these abstract, get an abstract class or interface, parameter Object Type. Users to implement these methods according to their needs! Use this class, you can easily complete the tasks such as above: OneView: public void jump () {MyView myview = new myView (); INT ID = viewjump.registerpath (this.xxpanel, this); viewjump.setnext (ID, myview.getPanel ()); viewjump.jump (ID);} Net part of the return: protected void goback () {int ID = Viewjump.registe Rpath (this.mypanel, this); viewjump.back (ID);

God, this is not magical, the 600 line code is only used in two lines!

Ok, I said so much, everything is in your hands, use your wisdom to optimize our redundant code, because it looks quite good.

Attachment: Complete code: (I put the viewpath class in the same file for viewjump.java, the code has been given above)

Public class viewjump {private static viewpath [] viewpath = null;

// Private constructor private viewjump () {}

// Looking for this Panel is not in the path / ** * found the subscript in the instance array * Did not find the return -1 * @Param PNL * @Return * / private static int found (jPanel PNL) {// System.out.println ("Perform Find () in viewjump"); if (ViewPath == Null || viewPath.Length == 0) Return -1; for (int i = 0; i

ViewPath [] vjarr = new viewpath [viewpath.length]; int count = 0; for (int i = 0; i

// Get an instance method / ** * must check that the Panel is already in the path, if in the path, * returns the number of registered numbers, use this number flat can access the correct type * If not in the path In order to start a new path * When the path is newly created, there is no need to check the first element of the path, * Because an element is not possible to start, but in order to prevent the user from continuous two registerpath errors * please An element also gives checking * MyView parameters only need when the interface is the starting point of the jump, otherwise keep the original View * @Param me * @Param Other * Return to the number of instances, * / public static int int RegisterPath (JPanel MyPanel, Baseview MyView) {System.out.println ("Performs REGISTERPATH () in viewjump"); int idx = find (myPanel); system.out.println ("idx =" idx); if (IDX) == - 1) {// Return -1 means not found, establish a new path system.out.println ("New a path"); idx = newpath (mypanel, myview);} system.out.println (" Perform the registration path .. "); return idx; // Return to instance subscript} // Set the next target to jump public static void setnext (int id, jpanel aim) {if (ID <0 || ID > = viewpath.length) {return;} ViewPath vp = viewpath [id]; // Set the target, see here, this is the security vulnerability, if the user passes the id comes in jpanel [] Path = VP. PNLPath; Path [VP.Index 1] = AIM;

/ / Go back to the previous public static void back (int id) {if (id <0 || ID> = viewpath.length) {return;} viewPath vp = viewpath [id]; // Back to the previous interface IF ( vp.index> 0) {// only vp.bsview.remove (vp.pnlpath [vp.index]) when there is a path in front; // Remove the current vp.index -; // cursor forward Take a step vp.bsview.add (vp.pnlpath [vp.index], borderlayout.center); // Add current to the interface

vp.bsview.validate (); vp.bsview.repaint ();}}

/ / Go back to the origin PUBLIC Static Void Backhome (INT ID) {if (id <0 || ID> = viewpath.length) {return;} viewpath vp = viewpath [id]; // directly back to the first step, Do you need to clear the path? What should I do if the midway break? The method is to check if the view has been empty // selection is not cleared. Every time I create a new path, the check path is not valid VP.BsView.Remove (vp.pnlpath [vp.index]); // Remove the current vp.index = 0; // Take a walk forward a step vp.bsview.add (vp.pnlpath [vp.index], borderlayout.center); // Add current To the interface vp.bsview.validate (); vp.bsview.repaint ();

// Jump to the next place {if (id <0 || id> = viewpath.length) {return;} Viewpath vp = viewpath [id]; if (vp.pnlpath [VP .INDEX 1] == null) {return; // Next step is not set at all}

vp.bsview.remove (vp.pnlpath [vp.index]); // Remove the current VP.Index ; vp.bsview.add (vp.pnlpath [vp.index], borderlayout.center); vp.bsview. Validate (); vp.bsview.repaint ();}}

Cao Xihua 2005/01/05 23:49:15

转载请注明原文地址:https://www.9cbs.com/read-72623.html

New Post(0)