本文介绍了如何获取/设置另一个类的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将IP和端口转移到MyCanvasClass上,或者在Canvas启动时以某种方式获取它.
请帮忙.

MyMidlet.java的内容:

I need to transfer IP and Port to MyCanvasClass or somehow get it when Canvas starting.
Please, help.

Contents of MyMidlet.java:

package pak;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


public class MyMidlet extends MIDlet implements CommandListener{
private Command exitCommand;
private Command connectCommand;
public Form form;
    private TextField textField;
    private TextField textField1;
    MyCanvas myCanvas = new MyCanvas(this);

    public void startApp() {
        getDisplay().setCurrent(getForm());
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    public Form getForm(){
         form = new Form("My Form",  new Item[] { getTextField(), getTextField1() });
         form.addCommand(getExitCommand());
         form.addCommand(getConnectCommand());
         form.setCommandListener(this);
         return form;
    }
        public Command getExitCommand() {
        if (exitCommand == null) {
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);
            // write post-init user code here
        }
        return exitCommand;
    }

        public Command getConnectCommand() {
        if (connectCommand == null) {
            // write pre-init user code here
            connectCommand = new Command("Connect", Command.ITEM, 0);
            // write post-init user code here
        }
        return connectCommand;
    }

    public void commandAction(Command c, Displayable d) {
        if (d == form) {
            if (c == exitCommand) {
               exitMIDlet() ;
            }
            else{
                if(c == connectCommand){

                   //ip =  textField.getString();
                   //port = textField1.getString();
         //myCanvas.start();
        //getDisplay().setCurrent(myCanvas);
                }
 }
           }
    }
        public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        // write pre-switch user code here
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
        // write post-switch user code here
    }
   public TextField getTextField() {
        if (textField == null) {
            // write pre-init user code here
            textField = new TextField("IP", null, 15, TextField.ANY);
            textField.setInitialInputMode("UCB_BASIC_LATIN");
            // write post-init user code here
        }
        return textField;
    }
    public TextField getTextField1() {
        if (textField1 == null) {
            // write pre-init user code here
            textField1 = new TextField("Port", null, 16, TextField.NUMERIC);
            // write post-init user code here
        }
        return textField1;
    }
}




MyCanvas.java的内容:




Contents of MyCanvas.java:

package pak;

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.*;
///////
class MyCanvas extends GameCanvas implements Runnable{
    MyMidlet midlet;
    public String ip;
    public String port;// i know that need INT, but now it''s doesn''t matter
    private boolean isRunning = true;
        private Graphics g;
    public MyCanvas(MyMidlet midlet){
        super(true);
        this.midlet = midlet;
    }
        public void start(){

        Thread runner = new Thread(this);
        runner.start();
    }

public void run(){
Connect();
        int iKey = 0;
        while (isRunning) {
            iKey = getKeyStates();

        if ((iKey & GameCanvas.FIRE_PRESSED) != 0)
        {
            midlet.getDisplay().setCurrent(midlet.getForm());
        }
            try{
        Thread.sleep(30);
            }
        catch(Exception ex){}

}
    }
public void Connect()
    {
///using ip and port
}
}

推荐答案

package pak;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;


public class MyMidlet extends MIDlet implements CommandListener{
public String ip;
public String port;
private Command exitCommand;
private Command connectCommand;
public Form form;
    private TextField textField;
    private TextField textField1;
    MyCanvas myCanvas = new MyCanvas(this);

    public void startApp() {
        getDisplay().setCurrent(getForm());
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    public Form getForm(){
         form = new Form("My Form",  new Item[] { getTextField(), getTextField1() });
         form.addCommand(getExitCommand());
         form.addCommand(getConnectCommand());
         form.setCommandListener(this);
         return form;
    }
        public Command getExitCommand() {
        if (exitCommand == null) {
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);
            // write post-init user code here
        }
        return exitCommand;
    }

        public Command getConnectCommand() {
        if (connectCommand == null) {
            // write pre-init user code here
            connectCommand = new Command("Connect", Command.ITEM, 0);
            // write post-init user code here
        }
        return connectCommand;
    }

    public void commandAction(Command c, Displayable d) {
        if (d == form) {
            if (c == exitCommand) {
               exitMIDlet() ;
            }
            else{
                if(c == connectCommand){

                   ip =  textField.getString();
                   port = textField1.getString();
         myCanvas.start();
        getDisplay().setCurrent(myCanvas);
                }
 }
           }
    }
        public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        // write pre-switch user code here
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
        // write post-switch user code here
    }
   public TextField getTextField() {
        if (textField == null) {
            // write pre-init user code here
            textField = new TextField("IP", null, 15, TextField.ANY);
            textField.setInitialInputMode("UCB_BASIC_LATIN");
            // write post-init user code here
        }
        return textField;
    }
    public TextField getTextField1() {
        if (textField1 == null) {
            // write pre-init user code here
            textField1 = new TextField("Port", null, 16, TextField.NUMERIC);
            // write post-init user code here
        }
        return textField1;
    }

class MyCanvas extends GameCanvas implements Runnable{
    MyMidlet midlet;
    private boolean isRunning = true;
        private Graphics g;
    public MyCanvas(MyMidlet midlet){
        super(true);
        this.midlet = midlet;
    }
        public void start(){

        Thread runner = new Thread(this);
        runner.start();
    }

public void run(){
    g = getGraphics();
Connect();
        int iKey = 0;
        while (isRunning) {
            iKey = getKeyStates();

        if ((iKey & GameCanvas.FIRE_PRESSED) != 0)
        {
            midlet.getDisplay().setCurrent(midlet.getForm());
        }
            try{
        Thread.sleep(30);
            }
        catch(Exception ex){}

}
    }
public void Connect()
    {
    ///using ip and port
    g.setColor(10, 20, 30);
    g.drawString("IP:" + ip + "Port:" + port ,0 , 0, 20);
    flushGraphics();
}
}


}


这篇关于如何获取/设置另一个类的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:14