sumscreen.java

来自「kjava编写的手机联网程序 客户端和服务器的演示代码」· Java 代码 · 共 72 行

JAVA
72
字号
package example.sum; 

import javax.microedition.lcdui.*; 
import java.io.IOException; 

class SumScreen 
    extends Form 
    implements CommandListener, HttpPosterListener 
{ 
    private final SumMIDlet midlet; 
    private final HttpPoster httpPoster; 
    private final TextField inputField; 
    private final StringItem outputField; 
    private final Command sendCommand; 
    private final Command quitCommand; 
    private volatile boolean readyForInput = true; 
    
    SumScreen(SumMIDlet midlet, 
              HttpPoster httpPoster) 
    { 
    	super("Sum"); 
    	this.midlet = midlet; 
    	this.httpPoster = httpPoster;
    	
    	inputField = new TextField("Input:", "1", 8, TextField.NUMERIC); 
    	append(inputField); 
    	outputField = new StringItem("Output:", "0"); 
    	append(outputField); 
    	sendCommand = new Command("Send", Command.SCREEN, 2); 
    	addCommand(sendCommand); 
    	quitCommand = new Command("Quit", Command.EXIT, 2);
    	addCommand(quitCommand); 
    	setCommandListener(this); 
    }
    
    public void commandAction(Command c, Displayable d) 
    { 
    	if (!readyForInput) 
    	{ 
    	    return; 
    	} 
    	if (c == sendCommand) 
    	{ 
            try 
            { 
            	String requestStr = inputField.getString(); 
            	httpPoster.sendRequest(requestStr, this); 
            	readyForInput = false;
            } 
            catch (IOException e) 
            { 
            	outputField.setText("Error"); 
            } 
        } 
        else if (c == quitCommand) 
        { 
            midlet.sumScreenQuit(); 
        } 
    } 
    
    public void receiveHttpResponse(String response) 
    { 
    	outputField.setText(response); 
    	readyForInput = true; 
    } 
    
    public void handleHttpError(String errorStr) 
    { 
    	outputField.setText("Error"); 
    	readyForInput = true; 
    } 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?