⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readingboard.java

📁 本程序用JavaME语言描述了一个运行在手机上的电子书系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	        		 this.removeCommand(autoline);
	        		 this.addCommand(autolineStop);
	        		 autolineorpage = new ChoiceGroup("翻转方式:", ChoiceGroup.EXCLUSIVE, lineorpage, null);
	        		 frm = new Form("翻转方式选择");
	        		 Display.getDisplay(bookReader).setCurrent(frm);
	        		 frm.append(autolineorpage);
	        		 frm.addCommand(next);
	        		 frm.addCommand(back);
	        		 frm.setCommandListener(this);
	        		 isEditMode = true;
	        	 }
	        	 else{
	        		 timer.cancel();
	        		 timer = new Timer();
	        		 isRunning = false;
	        		 this.removeCommand(autolineStop);
	        		 this.addCommand(autoline);
	        	 }
	        	 break;
	         case Canvas.KEY_POUND:                                        //NUM#全屏开关
	        	 if(isFullMode){
	        		 isFullMode = false;
	        		 setFullScreenMode(false);  							//关闭全屏模式
		        	 repaint();
	        	 }
	        	 else{
	        		 isFullMode = true;
	        		 setFullScreenMode(true);  								//开启全屏模式
		        	 repaint();
	        	 }
	        	 break;
	         case Canvas.KEY_STAR:                                         //进入搜索
	        	 this.searchBox();
	        	 break;
	         case Canvas.KEY_NUM5:
	        	 this.showIntro();                                         //进入使用说明
	    	 }
	    	 break;
	     }
    }
    
    private void addMark(){                                               //书签方法
    	int left = 0;
		for(int j = 0;j <= lineUp;j ++){
        	left += linecount[j]; 
        }
		String fs;
		if(linecount[lineUp] < 8&&lineUp!=0)
			fs = txt.substring(left, left + linecount[lineUp]);
		else if (linecount[lineUp]<8 && lineUp==0)
			fs = txt.substring(left, left+8);
		else
			fs = txt.substring(left, left + 5);
		bml.add(lineUp, fs);
    }
    
    private void addNote(){                                               //标注方法
    	isEditMode = true;
		tb = new TextBox("编辑标注", "", 2560, TextField.ANY);
		tb.addCommand(ok_label);
		tb.addCommand(back);
		tb.setCommandListener(this);
		Display.getDisplay(bookReader).setCurrent(tb);
    }
    
    private void showIntro(){
		instrumentbox = new TextBox("使用说明", "", 400, TextField.ANY|TextField.UNEDITABLE);
		instrumentbox.setString(
				"    手机按键'上''下''左''右'分别与按键'2''8''4''6'对应,代表上翻行、下翻行、上翻页、下翻页\n" +
				"    按键'0'代表进入自动翻页及控制自动翻页的停止,当处在自动翻页的时候使用翻行、翻页键也会关闭自动翻页\n" +
				"    按键'1'代表新增当前页面为书签\n" +
				"    按键'3'代表新增当前页面为标注\n" +
				"    按键'7'代表进入书签列表\n" +
				"    按键'9'代表进入注释列表\n" +
				"    按键'5'代表代表进入该使用说明\n" +
				"    按键'#'代表全屏模式的开关\n" +
				"    按键'*'代表进入搜索页面\n"+
				"    标题栏中显示部分标题和当前页面数与总页面数,当标题中的最右端显示'#'时,说明该页面有相应的标注。");
		instrumentbox.addCommand(back);
		Display.getDisplay(bookReader).setCurrent(instrumentbox);
		instrumentbox.setCommandListener(this);
		isEditMode = true;
    }
    
    public void commandAction(Command c, Displayable d) {				//命令响应
    	if(c == turnto){												//进入跳转页面
    		isEditMode = true;
    		turnbox = new TextBox("请输入跳转百分比:0-100",null, 3, TextField.NUMERIC);
    		Display.getDisplay(bookReader).setCurrent(turnbox);
    		turnbox.addCommand(turnstart);
    		turnbox.addCommand(back);
    		turnbox.setCommandListener(this);
    	}else if(c == fullScreen ){
    		if (isFullMode) {
				isFullMode = false;
				setFullScreenMode(false); // 关闭全屏模式
				repaint();
			} else {
				isFullMode = true;
				setFullScreenMode(true); // 开启全屏模式
				repaint();
			}
    	}
    	else if(c == turnstart){										// 跳转页面选择跳转数
    		if(Integer.parseInt(turnbox.getString()) < 0 ||
    				Integer.parseInt(turnbox.getString()) > 100){
    			error.setString("跳转范围为0-100");
    			Display.getDisplay(bookReader).setCurrent(error);
    			turnbox.setString("");
    		}
    		else{
    			turnTo(Integer.parseInt(turnbox.getString()));
    			Display.getDisplay(bookReader).setCurrent(this);
    			isEditMode = false;
    			turnbox = null;
    		}
    	}
    	else if(c == newmark){											//新建书签
    		this.addMark();
    	}
    	else if(c == entermark){										//显示书签列表
    		Display.getDisplay(bookReader).setCurrent(bml);
    	}
    	else if(c == newlabel){											//添加标注
    		this.addNote();
    	}
    	else if(c == enterlabel){										//显示标注列表
    		nrl.showList();
    	}
    	else if(c == instrument){										//使用说明界面
    		showIntro();
    	}
    	else if(c == back){												//返回上级界面
    		if (!isEditMode){
    			if (isRunning)
    				keyPressed(Canvas.KEY_NUM0);
    			bml.whenQuit(lineUp);
    			Display.getDisplay(bookReader).setCurrent(bookReader.getDisplayable(TxtReader.MAINMANU));
    			linecount = null;
        		txt = null;
    		}
    		else{
    			isEditMode = false;
    			tb = null;
    			turnbox = null;
       		    isRunning = false;
       		    frm = null;
       		    this.removeCommand(autolineStop);
    		    this.addCommand(autoline);
    			Display.getDisplay(bookReader).setCurrent(this);
    		}
    	}
    	else if(c == next){                                           //确定翻页方式
    		selectIndex = autolineorpage.getSelectedIndex();
    		tb = new TextBox("请输入翻行速度:1-9999ms/行",null, 4, TextField.NUMERIC);
    		if(1 == selectIndex) tb.setTitle("请输入翻页速度:1-9999ms/页");
    		Display.getDisplay(bookReader).setCurrent(tb);
    		tb.addCommand(autostart);
    		tb.addCommand(back);
   		    tb.setCommandListener(this);
    	}
    	else if(c == autostart){										//确定自动翻页
    		int speed = Integer.parseInt(tb.getString());
    		if(speed == 0){
    			error.setString("翻页速度为1到9999");
    			Display.getDisplay(bookReader).setCurrent(error);
    		}
    		else{
	    		Display.getDisplay(bookReader).setCurrent(this);
	    		if(0 == selectIndex) autoRunline(speed);
    			else autoRunpage(speed);
	    		tb = null;
	    		turnbox = null;
	    		frm = null;
	   		 	isRunning = true;
    		}
    	}
    	else if(c == autoline || c == autolineStop){					//自动翻页或者停止自动翻页
    		keyPressed(Canvas.KEY_NUM0);
    	}
    	else if (c == ok_label){										//确定添加标注
    		NoteRecord nr = new NoteRecord();
    		nr.setLine(lineUp);
    		nr.setNote(tb.getString());
    		nrl.addRecord(nr);
    		Display.getDisplay(bookReader).setCurrent(this);
    		nr_array = null;
    		nr_array = nrl.getAll();
    	}
    	else if (c == search){
    		searchBox();
    	}
    	else if (c == goSearch){                                     //搜索命令
    		String str = strBox.getString();
    		if (!str.equals("")){
    			int pos = 0;
    			for(int i = 0 ;i <= lineUp;i ++)
    				pos += linecount[i];
    			int preIndex[] = null;
    			preIndex = getPreIndex(str, preIndex);
    			int ans = KMP(str, pos, preIndex);
    			if (-1!=ans){
    			int eans = 0;
	    			for(int i = 0 ;;i ++){
	    				eans += linecount[i];
	    				if(eans >= ans) {
	    					lineUp = i-1;
	    					commandAction(back, strBox);
	    					repaint();
	    					break;
	    				}
	    			}
    			}else{
    				Display.getDisplay(bookReader).setCurrent(new Alert("搜索失败", "找不到搜索内容", null, AlertType.ERROR), this);
    			}
    		}else{
    			Display.getDisplay(bookReader).setCurrent(new Alert("错误", "搜索内容不能为空", null, AlertType.ERROR));
    		}
    	}
	}
    
    private int[] getPreIndex(String str, int[] preIndex){             //搜索方法
    	if (str.length()>=2){
	    	preIndex = new int[str.length()];
	    	int pos = 2;
	    	int cnd = 0;
	    	preIndex[0] = -1;
	    	preIndex[1] = 0;
	    	while (pos<str.length()){
	    		if (str.charAt(pos-1)==str.charAt(cnd)){
	    			preIndex[pos] = cnd+1;
	    			pos++;
	    			cnd++;
	    		}
	    		else if (cnd>0){
	    			cnd = preIndex[cnd];
	    		}
	    		else{
	    			preIndex[pos] = 0;
	    			pos++;
	    		}
	    	}
    	}else if (str.length()==1){
    		preIndex = new int[1];
    		preIndex[0] = -1;
    	}
    	return preIndex;
    }
    
    private int KMP(String str, int pos, int[] preIndex){             //搜索方法
    	int upperBound = txt.length();
    	int currentPos = 0;
    	while (pos+currentPos<upperBound){
    		if (txt.charAt(pos+currentPos)==str.charAt(currentPos)){
    			currentPos++;
    			if (currentPos==str.length())
    				return pos;
    		}else{
    			pos = pos + currentPos - preIndex[currentPos];
    			if (currentPos>0)
    				currentPos = preIndex[currentPos];
    		}
    	}
    	return -1;
    }
    
    private void searchBox(){
    	isEditMode = true;
		Display.getDisplay(bookReader).setCurrent(strBox);
    }
    
    public void setColorOfBackground(int color){ 						//设置背景颜色
    	colorOfBackground = color;
    }
    
    public void setColorOfChar(int color){       						//设置字体颜色
    	colorOfChar = color;
    }
    
    public void setShapeOfChar(int style){                            //设置字体形状
    	font = Font.getFont(Font.FACE_SYSTEM, style, sizeOfChar);
    	styleOfChar = style;
    }
    
    public void setSizeOfChar(int size){                              //设置字体大小
    	font = Font.getFont(Font.FACE_SYSTEM, styleOfChar,size);
    	sizeOfChar = size;
    }
    
    public void encode(String code){                                   //得到字符编码
    	encode = code;
    }
    
    private void autoRunline(long period){          						//自动翻页的实现
    	timer.schedule(new TimerTask(){
    		public void run(){
    			nextline();
    			repaint();
    		}}, 100, period);
    }

    private void autoRunpage(long period){          						//自动翻页的实现
    	timer.schedule(new TimerTask(){
    		public void run(){
    			nextpage();
    			repaint();
    		}}, 100, period);
    }
    
    private boolean isThereNote(){             						//判断当前页面是否有标注
    	for (int i=0; i<nr_array.length; ++i){
    		if (lineUp-nr_array[i].getLine()>=0 && lineUp-nr_array[i].getLine()<pagelines)
    			return true;
    	}
    	return false;
    }
    
    public NoteRecordList getNRL(){
    	return this.nrl;
    }
}

⌨️ 快捷键说明

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