首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

怎把java代码变成android代码,该怎么解决

2012-05-28 
怎把java代码变成android代码将eclipse下的java代码换成android虚拟机下的代码,代码如下:JFrame frame n

怎把java代码变成android代码
将eclipse下的java代码换成android虚拟机下的代码,代码如下:
JFrame frame = new JFrame();
  JPanel contentPane;
  BorderLayout borderLayout = new BorderLayout();  
  GridLayout gridLayout = new GridLayout(3,1);  
  Panel panel0 = new Panel(); //存放panel1,panel2,panel3
  Panel panel1 = new Panel(); //存放输入的服务器地址
  Panel panel2 = new Panel(); //存放输入的姓名和连接两个按钮
  Panel panel3 = new Panel(); //存放发送信息区域
  Panel panel4 = new Panel(); //存放聊天信息和聊天成员
  Panel panel5 = new Panel(); 
  Label label1 = new Label();
  
  TextField add_txt = new TextField(36);
  Label label2 = new Label();
  TextField name_txt = new TextField(20);
  Button button1 = new Button();
  Button button2 = new Button();
  Label label3 = new Label();
  TextField msg_txt = new TextField(30);
  Button button3 = new Button();
  TextArea chat_txt = new TextArea(15,30);
  java.awt.List list1 = new java.awt.List(16);
  
 
  Socket soc=null; //定义连接套接字
  DataInputStream dis=null; //定义用来实现客户端接受服务器数据的输入流
  DataOutputStream dos=null; //定义用来实现从客户端发送数据到服务器的输出流
  Thread client=null; //定义一个客户端线程

  public ChatClient() //初始化图形界面
  {
  frame.setTitle("客户端");
  contentPane = (JPanel) frame.getContentPane();
  contentPane.setLayout(borderLayout);
  panel0.setLayout(gridLayout);
  label1.setText("服务器地址:");
  add_txt.setText("localhost");
  panel1.add(label1);
  panel1.add(add_txt);
  panel1.setBackground(Color.orange);
  panel0.add(panel1);
  label2.setText("\u8F66 \u8F86 \u540D \u79F0\uFF1A");  
  button1.setBackground(Color.GREEN);
  button1.setLabel("连接");
  button2.setBackground(Color.RED);
  button2.setLabel("断开连接");
  panel2.add(label2);
  name_txt.setText("");
  panel2.add(name_txt);
  panel2.add(button1);
  panel2.add(button2);
  panel2.setBackground(Color.orange);
  panel0.add(panel2);
  label3.setText("\u8C03 \u5EA6 \u5185 \u5BB9\uFF1A");
  button3.setBackground(Color.CYAN);
  button3.setLabel("发送");
  panel3.add(label3);
  panel3.add(msg_txt);
  panel3.add(button3);
  panel3.setBackground(Color.orange);
  panel0.add(panel3);
  panel0.setBackground(Color.ORANGE);
  contentPane.add(panel0, BorderLayout.NORTH);
  chat_txt.setBackground(Color.PINK);
  chat_txt.setEditable(false);
  panel4.add(chat_txt);
  list1.setBackground(Color.GRAY);
  panel4.add(list1);
  panel4.setBackground(Color.orange);
  contentPane.add(panel4, BorderLayout.SOUTH);
  panel5.setBackground(Color.orange);
  contentPane.add(panel5, BorderLayout.CENTER);
  ButtonAction buttonaction = new ButtonAction();
  button1.addActionListener(buttonaction);
  button2.addActionListener(buttonaction);
  button3.addActionListener(buttonaction);
  frame.setSize(450,380); 
  frame.setResizable(false); 
  frame.addWindowListener(new WindowAdapter()
  { public void windowClosing(WindowEvent e)
  { disconnect();
  //System.exit(0);
  dispose();
  }
  });


  frame.show();
  }

  public static void main(String[] args) 
  {
  ChatClient cc=new ChatClient();
  }

  public void connect()  
  {
  if(soc==null)
{
try
{
soc=new Socket(add_txt.getText(),21036); //使用端口21036实例化一个本地套接字
System.out.println(soc); //在控制台打印实例化的结果
dis=new DataInputStream(soc.getInputStream()); //将dis指向soc的输入流
  dos=new DataOutputStream(soc.getOutputStream()); //将dos指向soc的输出流
String infos=new String("INFO: "); //定义一个字符串存储发送信息,其中INFO为关键字让服务器识别为连接信息
//并将name和ip用":"分开;在服务器端用StringTokenizer类来读取数据
String userinfo=name_txt.getText()+":"+InetAddress.getLocalHost().toString();
dos.writeUTF(infos+userinfo);
client=new Thread(this); //将客户端线程实例化  
client.start(); //启动线程
}
catch(IOException e)
{
System.out.println("Error:"+e);
disconnect();
}
} //end of if
  }

  public void disconnect() 运行的方法
  {
  if(soc!=null)
{
try
{
client.suspend();
dos.writeUTF("QUIT"); //服务器断开此次通
soc.close(); //关闭套接字
soc=null;
  }
catch(IOException e)
{
System.out.println("Error:"+e);
}
}// end of if
  }

  public void send() //客户端点击发送要运行的方法
  {
  if(soc!=null)
{
String msgs=new String("MSG: "); //定义并实例化一个字符串存储发送的聊天信息
try
{
dos.writeUTF(msgs+msg_txt.getText()); //用输出流发送聊天信息
}
catch(IOException e)
  {
  System.out.println("Error:"+e);
  } 
}
  }
 
  public void run() //线程运行方法
  {
  String msg=null;
while(true)
{
try{msg=dis.readUTF();} //读取从服务器传来的信息
catch(IOException e)
{
System.out.println("Error:"+e);
disconnect();
}
if (msg==null) //如果从服务器传来的信息为空则断开此次连接
{
client=null;  
soc=null;
list1.clear();
}
StringTokenizer st=new StringTokenizer(msg,":"); //用StringTokenizer类来实现读取分段字符
String keyword=st.nextToken(); //读取信息头即关键字用来识别是何种信息

if(keyword.equals("PEOPLE")) //如果是PEOPLE则是服务器发来的客户连接信息
//主要用来刷新客户端的用户列表
{
list1.clear();
while(st.hasMoreTokens()) //遍历st取得目前所连接的客户
{
String str=st.nextToken();
list1.addItem(str);
}
}
else if(keyword.equals("MSG")) //如果关键字是MSG则是服务器传来的聊天信息
//主要用来刷新客户端聊天信息区将每个客户的聊天内容显示出来
{
String usr=st.nextToken();
chat_txt.appendText(usr);
chat_txt.appendText(st.nextToken("\0"));
chat_txt.appendText("\n\n");
}
else if(keyword.equals("QUIT")) //如果关键字是QUIT则是服务器关闭的信息
//用来切断此次连接
{
System.out.println("Quit");
try


{
  client.stop();
client=null;
soc.close();
soc=null;
  }catch(IOException e)
  {
  System.out.println("Error:"+e);
  }
list1.clear(); 
}
}
  } //end of run method

  class ButtonAction extends JDialog implements ActionListener
  {
  public void actionPerformed(ActionEvent event) //事件触发
  {  
  String cmd = event.getActionCommand();
  //Object source=event.getSource();
if(cmd.equals("连接")) //如果点击连接按钮则运行connect()
{
connect();  
}//end of if
else if(cmd.equals("断开连接")) //如果点击断开连接按钮则运行disconnect()
{
disconnect();
}
else if(cmd.equals("发送")) //如果点击发送按钮则运行send()
{
send();
msg_txt.setText("");
}
else 
{
dispose();
}
  }
  }

[解决办法]
…… 

你会java却不愿意去花1天时间熟悉一下android开发框架
[解决办法]
额。。。。ls好犀利。。。。。
[解决办法]

探讨
……

你会java却不愿意去花1天时间熟悉一下android开发框架

[解决办法]
这个自己看看就行了,一会就搞定,求人不如求己,尤其是这么简单的东西

热点排行
Bad Request.