javaMe如何在其他类中引用主类的非成员方法,且改类也不能被实例化


import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.sun.lwuit.Display;
public class Main extends MIDlet {
 public static Main disp; // 引用MIDlet的Display 对象
 
 public Main() {  
  Display.init(this); 
  disp=this;
 }
 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }
 protected void pauseApp() {
 }
 protected void startApp() throws MIDletStateChangeException {
  ControlContainer.getInstance().getLoadForm().show();
    }
 
   public void close(){
 try {        
         destroyApp(false);
      notifyDestroyed();
  }
  catch (MIDletStateChangeException e) {    
  e.printStackTrace();
  }
 }
}
 
调用的类如下:
 
package com.peptalk.kaikai.client.ui;
import javax.microedition.midlet.MIDletStateChangeException;
import com.sun.lwuit.Button;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.GridLayout;
public class ExitMessageDialog extends Dialog{
 
  public ExitMessageDialog(){
   this.setLayout(new BorderLayout()); 
   Label titleLabel=new Label("是否退出系统!");
   titleLabel.setAlignment(Label.CENTER);
   this.addComponent("North",titleLabel);
   Container centerContainer=new Container(new GridLayout(2,1));
   Button subButton=getButton("确定");
   subButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent evt) {  
     Main.disp.close();           
   }      
      });
  
   Button  resetButton=getButton("取消");   
      resetButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent evt) {    
    ExitMessageDialog.this.dispose();
   }      
      });
     centerContainer.addComponent(subButton);
     centerContainer.addComponent(resetButton);  
   this.addComponent("Center",centerContainer);    
   this.setTransitionInAnimator(BaseUiData.getInstance().getSlideDown());
   this.setTransitionOutAnimator(BaseUiData.getInstance().getSlideDown());
  }
  private Button getButton(String title){
   Button b=new Button(title);
   b.getSelectedStyle().setBgColor(0x54321);
   b.setAlignment(Button.CENTER);
   return b;
  } 
  public void setShow(){
     int LeftSpace=(Display.getInstance().getDisplayWidth()-100)/2;
     int TopSpace=(Display.getInstance().getDisplayHeight()-100)/2;
  this.show(TopSpace,TopSpace,LeftSpace,LeftSpace, true);  
  }
}

这个很重要:  disp=this;
通过这种方法引用:   Main.disp.close();            非常巧妙!很佩服自己
 

你可能感兴趣的:(java,职场,休闲)