Easily implement interface jump in Java

Source: Internet
Author: User

Suppose in such a case, the interface of our system is built using Javax.swing package, the interface is based on Baseview, he is a container, of course he should provide the function of getting control elements, such as getting buttons, dropdown boxes, tables, etc., of course, just a container, And the elements of our interface are all deployed on the JPanel.

Described as:

An interface is a baseview, and he contains only one JPanel, which contains jpanel all our swing controls, such as jbutton,jlable, and so on.

The problem arises: we usually complete an interface for business needs to automatically jump to the next interface, complete the next interface can jump back (digression: Because our operation is based on GUI, so often can save session information, and the web is not), And this is often a factor in the inefficiency of the system implementation process, I have seen my current system someone with 600 lines of code to determine which one should be the previous interface to jump over, because many interfaces can jump to the current interface.

Of course, one way to do this is to include variables that point to the previous interface in the next interface class, which we say is inconvenient and adds dependencies, which is bad for the software.

Next, I give my solution, hoping to benefit friends who use this interface structure.

(All of the following are described in simplified models.)

1. Simply, we assume that Baseview inherits JWindow, of course it can be another container (based on your implementation), probably like this:

public abstract class BaseView extends JWindow{
  ...
  (实现一些取得界面控件,和界面信息的方法).
}

2. Each interface class is defined as this:

public class MyView extends BaseView{
  JPanel myPanel;
  public void playoutPanel(){
   JButton myButton = new JButton("OK");
   myPanel.add(myButton);
   ......
   (添加你需要的控件和布局到myPanel上)
  }
}

3. Assume that there are other interfaces Oneview,twoview,threeview processing after the operation will need to jump to MyView, in the MyView in the OK button pressed when the need to return to the original interface.

The original bloated code needs to add a variable Baseview anyview to the MyView, which is used to store the transferred interface Anyview, the assignment in the jump code of the three refers to MyView to set. Jump code like this:

public void jump(){
  MyView myView = new MyView();
  myView.anyView = this;
  this.remove(this.xxPanel);
  this.add(myView.getPanel());
  this.repaint();
}

It looks good, although you need to refer to the MyView class and call his variables and methods. But the jump back is not so easy, otherwise how can use 600 lines!

Probably like this: (It's been simplified by me)

public void goBack(){
  if(anyView instanceof OneView){
   anyView.remove(this.myView);
   OneView ov = (OneView)anyView;
   anyView.add(ov.getPanel());
   anyView.repaint();
  }
  if(anyView instanceof TwoView){
   ....
  }
  ...
}

Related Article

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.