jxmemidlet.java

来自「This Source.zip has three application co」· Java 代码 · 共 116 行

JAVA
116
字号

import java.io.IOException;

import javax.microedition.rms.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;


public class JXMEMIDlet extends MIDlet implements ItemCommandListener
{
    private Display display;
    
    private final Command CMD_SEND = new Command ( "Send", Command.ITEM, 1 );

    private TextField tfRecipientName = null;
    private TextField tfMessageForRecipient = null;
    private TextField tfMessageFromSender = null;

    private StringItem sendButton = null;
    private JXTA4JMSMessagingClient jxmeMessagingClient = null;
    
    private String relayURL = "http://localhost:9700";
    private String sender = "Sender: ";    

    public JXMEMIDlet()
    {
        try {
            tfMessageFromSender = new TextField ( "", "", 40, TextField.UNEDITABLE);            
            jxmeMessagingClient = new JXTA4JMSMessagingClient(
                                                    relayURL, 
                                                    "Alice", 
                                                    15000, 
                                                    tfMessageFromSender);

            System.out.println ("Got connection with relay...");
            showMessagingForm();
        }//try
        catch ( Exception e ){
            e.printStackTrace();
        }//catch
        
    }//JXMEMIDlet


    public void startApp(){
        Thread thread = new Thread (jxmeMessagingClient );
        thread.start();
    }//startApp()


    public void pauseApp(){
    }//pauseApp()


    public void destroyApp (boolean b){
    }//destroyApp()


    private void showMessagingForm() {
        Form msgForm = new Form ("JXTA4JMS Messaging");

        tfRecipientName = new TextField ( "Message To:", "", 10, TextField.ANY );
        tfMessageForRecipient = new TextField ( "Message", "", 40, TextField.ANY );
        
        sendButton = new StringItem ( "Send", "", Item.BUTTON );
        sendButton.setDefaultCommand ( CMD_SEND );
        sendButton.setItemCommandListener ( this );
        sendButton.setLayout( Item.LAYOUT_2 );

        msgForm.append ( tfMessageFromSender );

        msgForm.append ( tfRecipientName );
        msgForm.append ( tfMessageForRecipient );
        msgForm.append ( sendButton );

        display = Display.getDisplay (this);
        display.setCurrent ( msgForm );

    }//showMessagingForm()
    
    
    public void commandAction (Command c, Item item)
    {
        if ( c == CMD_SEND )
        {
            try
            {
                if ( tfRecipientName.getString().equals( "" ) )
                {
                    Alert alert = new Alert(
                          "Error", 
                          "Please enter recipient name for sending message", 
                          null, 
                          AlertType.ERROR
                          );
                    alert.setTimeout( 1000 );
                    display.setCurrent ((Screen) alert);
                
                }//if (tfRecipientName.getString().equals( "" ))
                else
                {
                   JXTA4JMSMessage message = new JXTA4JMSMessage( (tfRecipientName.getString()), 
                                                          tfMessageForRecipient.getString() 
                                                           );
                   jxmeMessagingClient.sendMessage (message);
                }
            }//try
            
            catch ( Exception e )
            {
                e.printStackTrace();
            }
        }//if (c == CMD_SEND)
        
    }//commandAction()
}

⌨️ 快捷键说明

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