58,454
社区成员
发帖
与我相关
我的任务
分享
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
*
* @author zys59三仙半(QQ:597882752)<br>
* 创建时间:2015年8月13日 下午5:13:14
*/
public class Text2 {
private String NUM = null;
private String P_0 = null;
private String P_1 = null;
private String TEM = null;
private String HUM = null;
private String IR = null;
private String len = null;
private Socket s;
private BufferedReader br;
private String line = "";
private Frame nw = new Frame("家庭管理 ");
private Panel nn = new Panel();
private Button n1 = new Button("开始接收");
private TextField b1 = new TextField();
private TextField b2 = new TextField();
private TextField b3 = new TextField();
private TextField b4 = new TextField();
private TextField b5 = new TextField();
private TextField b6 = new TextField();
Label a1 = new Label("服务器IP:");
Label a2 = new Label("设 备IP:");
Label a3 = new Label("数 据1:");
Label a4 = new Label("数 据2:");
Label a6 = new Label("数 据3:");
Label a5 = new Label("数 据 报:");
public static void main(String[] args) {
//不要在main()方法中做那些界面处理什么的,应该在类的构造函数里面进行
new Text2();
}
public Text2() {
//初始化界面
nn.setLayout(null);
n1.setBounds(460, 210, 100, 40);
b1.setBounds(130, 20, 150, 30);
b2.setBounds(130, 70, 150, 30);
b3.setBounds(130, 120, 150, 30);
b4.setBounds(130, 170, 150, 30);
b5.setBounds(130, 220, 150, 30);
b6.setBounds(400, 20, 150, 30);
a1.setBounds(40, 70, 60, 30);
a2.setBounds(40, 120, 60, 30);
a3.setBounds(40, 170, 60, 30);
a4.setBounds(40, 220, 60, 30);
a5.setBounds(40, 20, 60, 30);
a6.setBounds(340, 20, 60, 30);
nn.add(a1);
nn.add(a2);
nn.add(a3);
nn.add(a4);
nn.add(a5);
nn.add(a6);
nn.add(b6);
nn.add(b1);
nn.add(b2);
nn.add(b3);
nn.add(b4);
nn.add(b5);
nn.add(n1);
nw.setSize(600, 320);
nw.setLocationRelativeTo(null);
nw.add(nn);
nw.setVisible(true);
nw.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//给“开始接收”按钮增加事件监听
n1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//用一个线程去执行接收任务,这样就不会死循环了
new Thread(new Runnable() {
public void run() {
while (true) {
NUM = Client();
len = NUM.substring(4, 6);
if (len.equals("11")) {
P_0 = NUM.substring(6, 8);
P_1 = NUM.substring(8, 10);
TEM = NUM.substring(10, 12);
HUM = NUM.substring(12, 14);
IR = NUM.substring(14, 15);
b1.setText(NUM);
b2.setText(P_0);
b3.setText(P_1);
b4.setText(TEM);
b5.setText(HUM);
b6.setText(IR);
} else {
System.exit(0);
}
}
}
}).start();
}
});
}
public String Client() {
try {
s = new Socket("192.168.2.1", 8000);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
line = br.readLine();
br.close();
} catch (IOException ie) {
ie.printStackTrace();
}
return line;
}
}
我这样处理以后,不会死循环了,窗口可以正常关闭了,但是,你的Socket接收的业务逻辑是有问题的,不应该是现在这样