从J2SE应用程序转换到J2ME应用程序的全面指南
发布时间: 2025-08-17 01:03:27 阅读量: 1 订阅数: 3 

### 从 J2SE 应用程序转换到 J2ME 应用程序的全面指南
#### 1. J2SE 到 J2ME 转换概述
在软件开发中,有时需要将现有的 J2SE(Java 2 Standard Edition)应用程序转换为 J2ME(Java 2 Micro Edition)应用程序,以适应资源受限的设备,如 Palm 设备等。虽然 J2SE 和 J2ME 之间存在一些差异,但大多数 J2SE 应用程序的功能都可以移植到 J2ME 应用程序中。
#### 2. 转换前的准备
在进行转换之前,需要考虑以下几个方面:
- **API 差异**:J2SE 和 J2ME 的 API 有所不同,需要对代码进行相应的修改。
- **硬件差异**:目标设备的硬件资源有限,如内存、存储和处理能力等,需要对代码进行优化。
- **Java 语言支持**:J2ME 对 Java 语言的支持可能有限,需要确保代码符合 J2ME 的要求。
- **虚拟机器**:J2SE 和 J2ME 使用不同的虚拟机器,需要确保代码在 J2ME 的虚拟机器上能够正常运行。
#### 3. 代码修改示例
##### 3.1 XML 解析器的使用
在 J2ME 应用程序中,使用解析器的代码如下:
```java
// Call to a method startReading which will request the server for all
// the users connected at a particular time using the ASP.
startReading();
// The XMLParser will generate the Callbacks in the method parseData.
ip_addresses = parseData();
// Returns a Vector that contains the parsed results.
```
##### 3.2 GUI 的修改
在 J2SE 版本中,广泛使用了表格来显示文件/文件夹的列表、大小和扩展名。但在 J2ME 版本中,由于没有 JTable 类,Swing 包也不存在,因此使用 ScrollTextBox 来替代。
J2SE 中使用 JTable 类的代码如下:
```java
default_table = new AbstractTableModel()
{
// These methods always need to be implemented.
public int getColumnCount() { return names.length; }
public int getRowCount() { return data.length;}
public Object getValueAt(int row, int col) {return data[row][col];}
// The default implementations of these methods in
// AbstractTableModel would work, but we can refine them.
public String getColumnName(int column) {return names[column];}
public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
public boolean isCellEditable(int row, int col) {return (col==4);}
public void setValueAt(Object aValue, int row, int column) {
data[row][column] = aValue;
fireTableCellUpdated(row, column);
}
};
// Positioning and Initializing the GUI Component (Table)...
client_listing = new JTable(default_table);
```
J2ME 中使用 ScrollTextBox 的代码如下:
```java
userlist = new ScrollTextBox(text,10,40,130,100);
userlist.paint();
```
##### 3.3 事件处理的修改
由于 Palm 设备通常没有 QWERTY 键盘,使用手写笔输入文本,因此需要对文本输入部分和事件处理进行相应的修改。
J2SE 中的事件处理代码如下:
```java
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == file_open )// When Open Button is pressed...
{
// Create a socket connection to the desired user and browse the shared
// files / folders on his / her machine.
}
}
```
J2ME 中的等效代码如下:
```java
public void penDown(int x, int y)
{
if(browse_user.pressed(x,y)) // When the Browse Button is pressed.
{
// Create a socket connection to the desired user and browse the shared
// files / folders on his / her machine.
}
}
```
#### 4. 功能的调整
##### 4.1 下载和上传功能的移除
由于 Palm 设备没有硬盘来存储下载的文件,因此下载和上传文件的功能在 J2ME 版本中是不必要的,需要将其移除。
J2SE 中下载文件的代码如下:
```java
// Beginning of download class...
public class download_file extends Jframe (
...
...
// Identify the item to download...
row = file_listing.getSelectedRow();
// Get the selection of the user...
InetAddress inet = InetAddress.getByName(ip_address);
file_socket = new Socket(inet,7070);// Establish a socket connection
// with the Listener on the port 7070
// Get the output as well as the input Streams on that socket...
...
...
// if the selection is a folder then pop up a message for denial of download.
...
...
// else form an XML request consisting of the file to be downloaded
...
...
writer.requestFString("DOWNLOAD",information[row][0]); // requestFString...
...
...
// Popping up a file Dialog, get the loc
```
0
0
相关推荐










