张生工作室

一切皆有可能

  语源科技BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  24 随笔 :: 3 文章 :: 11 评论 :: 0 Trackbacks

 

/*
 * TextBoxTest.java
 *
 * Created on 2007年9月9日, 下午12:28
 
*/


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 *
 * 
@author  Administrator
 * 
@version
 
*/

public class TextBoxTest extends MIDlet implements javax.microedition.lcdui.CommandListener,javax.microedition.lcdui.ItemStateListener {
    
private Form form;
    
private Display display;
    
private TextBox textbox;
    
private Command exitCmd;
    
private Command backCmd;
    
private Command menuCmd;
    
private Command copyCmd;
    
private Command cutCmd;
    
private Command selectCmd;
    
private Command pasteCmd;
    
private Command saveCmd;
    
private Command openCmd;
    
private Command okCmd;
    
private Command cancelCmd;
    TextField tf;
    Form tempform;
    
boolean selectFlag=false;
//    boolean firstClick=true;
    String selectText="";
    
int selectPosition,endPosition=0;
    
    
public TextBoxTest()
    
{   
        form 
=new Form("TextBox测度");
        display
=Display.getDisplay(this);
        tempform
=new Form("");
        textbox
=new TextBox("TextBox","这是一个关于TextBox应用,实现在复制制,剪切功能",500,TextField.ANY);
        tf
=new javax.microedition.lcdui.TextField("请输入文本文件名","",20,TextField.ANY);
        exitCmd
=new Command("退出",Command.EXIT,1);
        backCmd
=new Command("退出",Command.BACK,1);
        selectCmd
=new Command("选定",Command.SCREEN,1); 
        copyCmd
=new Command("复制",Command.SCREEN,1);
        cutCmd
=new Command("剪切",Command.SCREEN,1);
        pasteCmd
=new Command("粘贴",Command.SCREEN,1);
        saveCmd
=new Command("保存",Command.SCREEN,1);
        openCmd
=new Command("打开",Command.SCREEN,1);
        okCmd
=new Command("确定",Command.OK,1); 
        cancelCmd
=new Command("取消",Command.CANCEL,1);
        textbox.addCommand(selectCmd);
        textbox.addCommand(copyCmd);
        textbox.addCommand(cutCmd);
        textbox.addCommand(pasteCmd);
        textbox.addCommand(saveCmd);
        textbox.addCommand(openCmd);
        textbox.addCommand(exitCmd);
        textbox.addCommand(backCmd);
        textbox.setCommandListener(
this);
    }

    
    
public void startApp() {
        
        display.setCurrent(textbox);
        

        
    }

    
    
public void pauseApp() {
    }

    
    
public void destroyApp(boolean unconditional) {
    }


    
public void commandAction(Command command, Displayable displayable) {
        
if(command==selectCmd)
        
{
            selectFlag
=true;
            selectPosition
=textbox.getCaretPosition();
        }

        
else if((command==copyCmd)&&selectFlag)
        
{
            
int temp=textbox.size();
            
char[] chr=new char[temp];
           textbox.getChars(chr);
           
if (textbox.getCaretPosition()>selectPosition)
            selectText
=String.valueOf(chr,selectPosition,textbox.getCaretPosition()-selectPosition);
           
else
              selectText
=String.valueOf(chr,selectPosition,selectPosition-textbox.getCaretPosition());
            selectFlag
=false;
            
        }

        
else if(command==cutCmd)
        
{
            
int temp=textbox.size();
            
char[] chr=new char[temp];
            textbox.getChars(chr);
            selectText
=String.valueOf(chr,selectPosition,textbox.getCaretPosition()-selectPosition);
            
if (textbox.getCaretPosition()>selectPosition)
               textbox.delete(selectPosition,textbox.getCaretPosition()
-selectPosition);
            
else
               textbox.delete(selectPosition,selectPosition
-textbox.getCaretPosition()); 
            selectFlag
=false;
        }

            
        
else if(command==exitCmd)
        
{
            destroyApp(
false);
            notifyDestroyed();
        }

        
else if(selectText.length()>0&&command==pasteCmd)
        
{
            
/*the first way*/
          
//  textbox.insert(selectText,textbox.getCaretPosition());
            
            
/*the second way*/
            
char[] chr=new char[selectText.length()];
            selectText.getChars(
0,selectText.length(),chr,0);
            textbox.insert(chr,
0,selectText.length(),textbox.getCaretPosition());
            
        }

        
else if(command==saveCmd)
        
{
           
            tempform.append(tf);
            display.setCurrent(tempform);
            tempform.addCommand(okCmd);
            tempform.addCommand(cancelCmd);
            tempform.setCommandListener(
this);
        }

        
else if(command==openCmd)
        
{
            
            tempform.append(tf);
            display.setCurrent(tempform);
            tempform.addCommand(okCmd);
            tempform.addCommand(cancelCmd);
            tempform.setCommandListener(
this);
        }

        
else if(command==okCmd)
        
{
            textbox.setString(myReadLine (tf.getString()));
            display.setCurrent(textbox);
        }

        
else if(command==cancelCmd)
        
{
            display.setCurrent(textbox);
        }

        
if(command==backCmd)
        
{
             destroyApp(
false);
             notifyDestroyed();
             
        }

        
    }


    
public void itemStateChanged(Item item) {
             
    }


           
private String myReadLine (String testFileName) {
       InputStream  in 
= this.getClass().getResourceAsStream(testFileName);
          ByteArrayOutputStream  s;
    s 
= new ByteArrayOutputStream();      try{
        
int ch=0;
        ch
=in.read();  
        
while ( ch!= -1 ) {
                      s.write( ch );
                ch
=in.read();
            }

            in.close();  
        }
  catch (IOException ioe){
           System.out.println(ioe.toString());
        }

        String str 
= s.toString();  
        
try{
            s.close(); 
        }
  catch (IOException ioe){
           System.out.println(ioe.toString());
        }

        
return  str.trim(); 
    }

}




张生工作室
posted on 2007-09-09 21:33 张生工作室 阅读(366) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: