📄 newsframe.java
字号:
/*
* 创建日期 2005-3-26
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
/**
* @author huihui
*
* TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.*;
public class NewsFrame implements CommandListener {
private List categorylist = new List("信息类别", Choice.IMPLICIT);
private String[] category = null;
private int categoryNum = 0;
private List titlelist = null;
private String[][] title = null;
private int[][] titleID = null;
private TextBox detail = null;
private MainMIDlet mainmidlet = null;
private String content = null;
public NewsFrame(MainMIDlet mainmidlet) {
this.mainmidlet = mainmidlet;
}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals("返回")) {
if (this.mainmidlet.display.getCurrent().equals(categorylist)) {
this.mainmidlet.ShowMain();
} else if (this.mainmidlet.display.getCurrent().equals(titlelist)) {
this.mainmidlet.ShowCategory();
} else if (this.mainmidlet.display.getCurrent().equals(detail)) {
this.mainmidlet.ShowTitle();
}
//}else if(label.equals("确定")){
} else {
if (this.mainmidlet.display.getCurrent().equals(categorylist)) {
this.mainmidlet.ShowTitle();
} else if (this.mainmidlet.display.getCurrent().equals(titlelist)) {
this.mainmidlet.ShowDetail();
}
}
//switch (.getSelectedIndex()) {
}
public Displayable ShowCategory() {
MainMIDlet.ticker.setString("昆宇系统--信息发布");
if (categorylist.size() == 0) { //size为0,需要连接,读取类别列表.
System.out.println("new List");
categorylist = new List("信息类别", Choice.IMPLICIT); //!!
new Thread(new HttpConnector(1)).start(); //type=1 category ,2
// title,3 detail
} else {
categorylist.setTicker(MainMIDlet.ticker);
categorylist.addCommand(MainMIDlet.backCommand);
categorylist.addCommand(MainMIDlet.okCommand);
categorylist.setCommandListener(this);
}
return categorylist;
}
public Displayable ShowTitle() {
int cat = categorylist.getSelectedIndex();
if (title[cat] == null) { //null 需要连接,读取类别列表.
titlelist = new List("", Choice.IMPLICIT); //
new Thread(new HttpConnector(2)).start(); //type=1 category ,2
// title,3 detail
} else {
titlelist = new List(category[cat], Choice.IMPLICIT);
for (int i = 0; i < title[cat].length; i++) {
titlelist.append(title[cat][i], null); //image ==null;
}
}
MainMIDlet.ticker.setString("昆宇系统--信息列表");
titlelist.setTicker(MainMIDlet.ticker);
titlelist.addCommand(MainMIDlet.backCommand);
titlelist.addCommand(MainMIDlet.okCommand);
titlelist.setCommandListener(this);
return titlelist;
}
public Displayable ShowDetail() { //可不返回displayable类型
/*
* if(detail==null){ detail=new TextBox( "", "", 400, TextField.ANY );
* //!!! new Thread(new HttpConnector(3)).start(); //type=1 category ,2
* title,3 detail }else{ MainMIDlet.ticker.setString("昆宇系统--信息内容");
* detail.setTicker(MainMIDlet.ticker);
* detail.addCommand(MainMIDlet.backCommand);
* detail.setCommandListener(this); }
*/
detail = new TextBox("", "", 400, TextField.ANY); //!!!
MainMIDlet.ticker.setString("昆宇系统--信息内容");
detail.setTicker(MainMIDlet.ticker);
detail.addCommand(MainMIDlet.backCommand);
detail.setCommandListener(this);
new Thread(new HttpConnector(3)).start(); //type=1 category ,2 title,3
// detail
return detail;
}
class HttpConnector implements java.lang.Runnable {
int type;
public HttpConnector(int type) {
this.type = type; //type=1 category ,2 title,3 detail
}
public void run() {
switch (type) {
case 1:
getCategory();
break;
case 2:
getTitle();
break;
case 3:
getDetail();
break;
default:
break;
}
}
}
private void getCategory() {
content = "读取信息类别";
try {
MainMIDlet.url = "http://" + MainMIDlet.serverip
+ "/CourseSelect/ProcessNews?type=1&target=0"; //1
// category,2
// title,3
// detail
System.out.println(MainMIDlet.url);
this.mainmidlet.imagescreen.show(3, "连接服务器……", 1); //3:load
// 错误返回
// 1:主菜单
HttpConnection connection = (HttpConnection) Connector
.open(MainMIDlet.url);
InputStream is = null;
is = connection.openInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
int errorcode = dis.readInt();
System.out.println(errorcode);
if (errorcode >= 0) { //没有错误则errorcode表示信息类别数。
category = new String[errorcode];
for (int i = 0; i < errorcode; i++) {
category[i] = dis.readUTF();
categorylist.append(category[i], null);
}
title = new String[errorcode][]; //分配第一维空间
titleID = new int[errorcode][];
dis.close();
is.close();
connection.close();
//this.mainmidlet.display.setCurrent(categorylist);
this.mainmidlet.ShowCategory();
//this.mainmidlet.imagescreen.show(3, "连接服务器成功...", 7);
// //3:load 7:信息类别
} else {
switch (errorcode) { //判断错误代码
case -3:
content = "与数据库连接错误";
break;
default:
//System.out.println(content);
content = "连接服务器失败";
break;
}
this.mainmidlet.imagescreen.show(2, content, 1); //错误返回 1:主菜单
// next:0-登录
// 1-主菜单,2-选课,3-查询,4-修改密码界面,5-选择班级,6-显示课表,7-信息类别,8-标题列表,9-详细信息
}
} catch (IOException ex) {
ex.printStackTrace();
this.mainmidlet.imagescreen.show(2, "连接服务器失败", 1);
}
}
private void getTitle() {
int cat = categorylist.getSelectedIndex() + 1;
try {
MainMIDlet.url = "http://" + MainMIDlet.serverip
+ "/CourseSelect/ProcessNews?type=2&target=" + cat;
System.out.println(MainMIDlet.url);
HttpConnection connection = (HttpConnection) Connector
.open(MainMIDlet.url);
InputStream is = null;
is = connection.openInputStream();
DataInputStream dis = new DataInputStream(is);
int errorcode = dis.readInt();
System.out.println(errorcode);
if (errorcode >= 0) { //没有错误则errorcode表示 标题数。
titleID[cat - 1] = new int[errorcode];
title[cat - 1] = new String[errorcode];
for (int i = 0; i < errorcode; i++) {
titleID[cat - 1][i] = dis.readInt();
title[cat - 1][i] = dis.readUTF();
}
dis.close();
is.close();
connection.close();
this.mainmidlet.ShowTitle();
//this.mainmidlet .display .setCurrent( this.titlelist);
} else {
switch (errorcode) { //判断错误代码
case -3:
content = "与数据库连接错误";
break;
default:
//System.out.println(content);
content = "连接服务器失败";
break;
}
this.mainmidlet.imagescreen.show(2, content, 7); //7-信息类别
}
} catch (IOException ex) {
ex.printStackTrace();
this.mainmidlet.imagescreen.show(2, "连接服务器失败", 7); //7-信息类别
}
}
private void getDetail() {
int ID = 0;
ID = titleID[categorylist.getSelectedIndex()][titlelist
.getSelectedIndex()];
System.out.println("ID=" + ID);
try {
MainMIDlet.url = "http://" + MainMIDlet.serverip
+ "/CourseSelect/ProcessNews?type=3&target=" + ID; //1
// category,2
// title,3
// detail
System.out.println(MainMIDlet.url);
HttpConnection connection = (HttpConnection) Connector
.open(MainMIDlet.url);
InputStream is = null;
is = connection.openInputStream();
DataInputStream dis = new DataInputStream(is);
int errorcode = dis.readInt();
System.out.println(errorcode);
if (errorcode > 0) { //没有错误则errorcode表示信息类别。
detail.setString(dis.readUTF()); //显示详细信息
detail
.setTitle(title[categorylist.getSelectedIndex()][titlelist
.getSelectedIndex()]);
dis.close();
is.close();
connection.close();
//this.mainmidlet.ShowDetail();
this.mainmidlet.display.setCurrent(detail); //??
} else {
switch (errorcode) { //判断错误代码
case -3:
content = "与数据库连接错误";
break;
default:
//System.out.println(content);
content = "连接服务器失败";
break;
}
this.mainmidlet.imagescreen.show(2, content, 8); //错误返回 7:信息类别
//next:0-登录
// 1-主菜单,2-选课,3-查询,4-修改密码界面,5-选择班级,6-显示课表,7-信息类别,8-标题列表,9-详细信息
}
} catch (IOException ex) {
ex.printStackTrace();
this.mainmidlet.imagescreen.show(2, "连接服务器失败", 1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -