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

📄 showform.java

📁 一个客户端和数据库服务器之间进行无线通信的例子
💻 JAVA
字号:
package system.dept;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.*;

import com.jufeng.MidpTable;


/**
 * 部门报表的显示
 * @author FOX
 *
 */
public class ShowForm implements CommandListener{
	
	private Display display ;//MIDLET显示对象传进来,便于返回
	private List parentList ;//父界面也要传递进来便于返回上一个界面
	
	private Form showForm = null;
	 
	

	
	
	
	/**确认删除Form*/
	private Form SureDelForm;
	
	 /**删除Form中的确定提交删除命令*/
	private  Command cmdisDel;

	 /**删除的后退命令*/
	  Command cmdBack;
	
	
	  
	private Command cmdExitDept ;		  
	  /**主Form中的添加命令*/
	  
	private Command cmdAddDept;
	  /**主Form中的删除命令*/
	 
	private Command cmdDelDept;
	  /**主Form中的修改命令*/
	  
	private Command cmdModDept;
	  /**主Form中的查询命令*/
	  
	private Command cmdFindDept;
	
	 /** 表格控件 */
    private static MidpTable table = null;
	  
	String defaultURL = "http://127.0.0.1:8088/ServerProiims/servlet/ServletShowDept";
	
	/**
	 * 构造方法,必须加PUBLIC,否则只在包内可见
	 * @param cmd
	 *           ,把要监听对象传递进来
	 */
	public ShowForm(Display display,List parentList){//把显示对象和从哪个界面过来(即父界面)的那个界面传进来,方便返回父界面
		this.display = display;//把外面的显示对象传递进来 
		this.parentList = parentList;//把父界面也传进来
		
		showForm = new Form("部门信息维护");//CUSTOM不能直接显示
		//mainForm.append("测试");
		
		cmdExitDept = new Command ("返回",Command.EXIT,2);
		cmdAddDept = new Command("添加",Command.SCREEN,1);
		cmdDelDept = new Command ("删除",Command.SCREEN,1);
		cmdModDept = new Command ("修改",Command.SCREEN,1);
		cmdFindDept = new Command ("查询",Command.SCREEN,1);
		
		showForm.addCommand(cmdExitDept);
		showForm.addCommand(cmdAddDept);
		showForm.addCommand(cmdDelDept);
		showForm.addCommand(cmdModDept);
		showForm.addCommand(cmdFindDept);
		showForm.setCommandListener(this);
		
		
		/**开销时间大的操作,新开一个线程来做*/
		new Thread(){
			 public void run(){
		showAllInvokeServlet(defaultURL);
			  }}.start();
	}

	

	public void setShowForm(Form showForm) {
		this.showForm = showForm;
	}




	/**得到ShowForm类对象*/
	public Form getShowForm() {
		return showForm;
	}
	
/*****************get和set方法结束*************************/
	
	
	 
	
	/**调用显示全部记录的方法*/
	public void showAllInvokeServlet(String url){ 
		  
		   HttpConnection hc = null; 
		   DataOutputStream dos = null; 
		   DataInputStream dis = null; 
		   try{ 
		   
		   hc = (HttpConnection)Connector.open(url,Connector.READ_WRITE); //设置请求属性 
		   hc.setRequestMethod(HttpConnection.POST); //设置为POST请求方式,默认的请求方式是GET 
		   hc.setRequestProperty("Connection","Keep-Alive"); 
			
		   dos = hc.openDataOutputStream(); //写到输出流中去,以便服务器端从里面取数据

		   dos.writeUTF("SHOW"); 
		 
         dos.flush();
		   dos.close(); 
		   
		   //接收servlet响应数据 
		    dis = new DataInputStream(hc.openInputStream()); //用于接受服务器端放在流中的东西,这个过程很象SOCKET程序
		   
		    
		    
		   /*把所有内容显示在表格里面*/
		    /*把数据库中所有数据显示在手机主界面上*/
			  Vector ptitle = new Vector();//表头标题
			  ptitle.addElement("部门编号"); ptitle.addElement("部门名称"); ptitle.addElement("负责人");
			  
			  Vector pweiba = new Vector();//表尾
			  pweiba.addElement(" "); pweiba.addElement(" "); pweiba.addElement(" ");
			  
			  Vector pdata= new Vector();
		      
			  system.dept.ClientUtil cUtil = new system.dept.ClientUtil();//建立工具类对象
			  pdata = cUtil.distreamToVector(dis);
			  
			 
		     pdata.addElement(pweiba);
		    
		    table = new MidpTable("部门信息",ptitle,pdata);
		    table.setLayout(Item.LAYOUT_CENTER);
		    table.setDx(73);//设置一下单元格长度
		    

		    showForm.append(table);
		  
		    //display.setCurrent(mainForm);	
	
		    
		    }
		   catch(IOException e){System.out.print("showAllInvokeServlet(String url)错误"+e);}
		   finally{ 
			   try{
		    if (dis != null) {dis.close();}
		    if (dos != null) {dos.close();} 
		    if (hc != null) {hc.close();} 
			   }catch(IOException e){System.out.print("showAllInvokeServlet(String url)错误"+e);}
		  } 
		  //display.setCurrent(returnForm); //设置当前窗口
		  }

	
	  public void sureDelForm(){
		  SureDelForm = new Form("");
		  
		  cmdisDel = new Command ("是",Command.SCREEN,1);
		  cmdBack = new Command ("否",Command.BACK,1);
		  
		  SureDelForm.addCommand(cmdisDel);
		  SureDelForm.addCommand(cmdBack);
		  SureDelForm.setCommandListener(this);
		  
		  SureDelForm.append("如果选择删除,该记录将从数据库中消失,下次将无法找到!你确定要删除吗?");
	  }
	
	
	
	public void commandAction(Command c, Displayable d) {
		 if (c == cmdExitDept) {//返回上一个父界面
			    display.setCurrent(parentList);//对象是引用传递,所以执行的是其对象本身
	          
	        }
		 
		 if (c == cmdAddDept) {//返回上一个父界面
			   AddForm addForm = new AddForm(display,showForm,table);
			    display.setCurrent(addForm.getAddForm());//对象是引用传递,所以执行的是其对象本身
	          
	        }
		 
		 if (c == cmdModDept) {//返回上一个父界面
			   ModForm modForm = new ModForm(display,showForm,table);
			    display.setCurrent(modForm.getModForm());//对象是引用传递,所以执行的是其对象本身
	          
	        }
		 
		 
		 if(c == cmdDelDept){
			  sureDelForm();
			  display.setCurrent(SureDelForm);
			  }
		 
		 if (c == cmdisDel){				  				 
			  new Thread(){
				  public void run(){
					  System.out.println("删除数据"+table.getSelectRow());						  
					  table.delRow(table.getSelectRow());
				  }
			  }.start();
			  display.setCurrent(showForm);
		  }
		 
		 
		 if(c == cmdBack){
				display.setCurrent(showForm);
		 
		 
	}
	
	
	}	
	
}

⌨️ 快捷键说明

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