//中央空调模拟器的客户端 import java.net.*;import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;import java.io.*;public class c{ public static void main(String args[]) { c_win w=new c_win(); w.setVisible(true); }}class c_win extends JFrame implements ActionListener{ Label label1=new Label("房间号:"); TextField tf1=new TextField(); Label label2=new Label("目标温度:"); TextField tf2=new TextField(); Label label3=new Label("当前温度:"); TextField tf3=new TextField(); Button button1=new Button("连接"); Button button2=new Button("取消"); Button button3=new Button("保持"); Button button4=new Button("改变"); String s1=null,s2=null; c_win() { Container c=this.getContentPane(); c.setLayout(new GridLayout(5,3)); c.add(label1); c.add(tf1); c.add(label2); c.add(tf2); c.add(label3); c.add(tf3); c.add(button1); button1.addActionListener(this); c.add(button2); button2.addActionListener(this); c.add(button3); button3.addActionListener(this); c.add(button4); button4.addActionListener(this); this.setBounds(300,300,300,200); this.setVisible(true); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); } public void actionPerformed(ActionEvent ae) { Socket sd=null; DataInputStream in=null; DataOutputStream out=null; s1=tf1.getText().trim(); s2=tf2.getText().trim(); try { sd=new Socket("localhost",9898); System.out.println("success..."); } catch(Exception e){System.out.println("false...");} try { in=new DataInputStream(sd.getInputStream()); out=new DataOutputStream(sd.getOutputStream()); } catch(Exception e){} if (ae.getSource()==button1) { try { out.writeUTF("#ROOM#"+tf1.getText()+","+tf2.getText()+","+tf3.getText()+","+"输热风"); } catch(Exception e){} } if (ae.getSource()==button2) { try { out.writeUTF("#LEAVE#"+tf1.getText().trim()); } catch(Exception e){} } if(ae.getSource()==button3) { try { out.writeUTF("#TEM#"+tf1.getText().trim()+","+tf3.getText().trim()); } catch(Exception e){} } if(ae.getSource()==button4) { try { out.writeUTF("#CHANGETEM#"+tf1.getText().trim()+","+tf2.getText().trim()+","+"吹热风"); } catch(Exception e){} } } }

评论