运行后关闭不掉 求帮助

动天丶 2015-08-08 03:28:03
package com.server;

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;

public class Text {
static String NUM = null;
static String P_0 = null;
static String P_1 = null;
static String TEM = null;
static String HUM = null;
static String IR = null;
static String len = null;
private static Socket s;
private static BufferedReader br;
private static String line = "";
final static Frame nw = new Frame("家庭管理 ");
static Panel nn = new Panel();
static Button n1 = new Button("开始接收");
final static TextField b1 = new TextField();
final static TextField b2 = new TextField();
final static TextField b3 = new TextField();
final static TextField b4 = new TextField();
final static TextField b5 = new TextField();
final static TextField b6 = new TextField();
static Label a1 = new Label("服务器IP:");
static Label a2 = new Label("设 备IP:");
static Label a3 = new Label("数 据1:");
static Label a4 = new Label("数 据2:");
static Label a6 = new Label("数 据3:");
static Label a5 = new Label("数 据 报:");

public static 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;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
public void run() {
// call method 1
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);
}
});
}
}).start();

new Thread(new Runnable() {
public void run() {
// call method 2
n1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
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();
}
}
...全文
110 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
三仙半 2015-08-13
  • 打赏
  • 举报
回复


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接收的业务逻辑是有问题的,不应该是现在这样
三仙半 2015-08-13
  • 打赏
  • 举报
回复
点完"开始接收"按钮后,进入死循环了,还怎么响应用户操作啊。你这是要干嘛?现在的逻辑是有问题的。
晴天_ccc 2015-08-09
  • 打赏
  • 举报
回复
调用jframe对象的.setdefaultclose...(EXIST_close)。不太记得,只是大概是这样。
动天丶 2015-08-08
  • 打赏
  • 举报
回复
就没一个人愿意帮帮我?

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧