package Thread03;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
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.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Thread05 extends Frame{
private TextField tf;
private Button send;
private Button log;
private Button clear;
private Button shake;
private TextArea viewText;
private TextArea sendText;
private DatagramSocket socket;
private BufferedWriter bw;
public Thread05() throws IOException{
init();
southPanel();
centerPanel();
event();
}
public void event() {
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
socket.close();
bw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
}
});
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
send();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
log.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
logFile();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
private void logFile() throws IOException {
bw.flush();
}
private void send() throws IOException {
String message=sendText.getText();
String ip=tf.getText();
DatagramPacket packet=new DatagramPacket(message.getBytes(), message.getBytes().length, InetAddress.getByName(ip), 9999);
socket.send(packet);
String time=getCurrentTime();
String string = time+"我对"+ip+"说:"+message+"\n";
viewText.append(string);
bw.write(string);
sendText.setText("");
}
private String getCurrentTime() {
Date d = new Date(); //创建当前日期对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
return sdf.format(d); //将时间格式化
}
private void centerPanel() {
Panel center=new Panel();
viewText = new TextArea();
sendText = new TextArea(5,1);
center.setLayout(new BorderLayout());
center.add(sendText,BorderLayout.SOUTH);
center.add(viewText,BorderLayout.CENTER);
viewText.setEditable(false);
viewText.setBackground(Color.WHITE);
viewText.setFont(new Font("xxx", Font.PLAIN, 23));
sendText.setFont(new Font("xxx", Font.PLAIN, 23));
this.add(center,BorderLayout.CENTER);
//this.show();
this.setVisible(true);//用上面一行的代码也能解决bug很郁闷为什么会出现这种异常
}
private void southPanel() {
Panel south=new Panel(); //新建南边的Panel
tf = new TextField(15);
tf.setText("127.0.0.1");
send = new Button("发送");
log = new Button("记录");
clear = new Button("清屏");
shake = new Button("震动");
south.add(tf);
south.add(send);
south.add(log);
south.add(clear);
south.add(shake);
this.add(south,BorderLayout.SOUTH);
}
public void init() throws IOException {
this.setLocation(500, 60); //设置位置
this.setSize(400, 600); //设置大小
this.setVisible(true); //设置是否可见
new Receive().start();
socket = new DatagramSocket();
bw=new BufferedWriter(new FileWriter("config1.txt"));
}
private class Receive extends Thread{
public void run(){
try {
DatagramSocket socket=new DatagramSocket(9999);
DatagramPacket packet=new DatagramPacket(new byte[1024], 1024);
while (true) {
socket.receive(packet);
byte[]arr=packet.getData();
int len=packet.getLength();
String time=getCurrentTime();
String message=new String(arr,0,len);
String ip=packet.getAddress().getHostAddress();
String string = time+""+ip+"对我说:"+message;
viewText.append(string);
bw.write(string);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
new Thread05();
}
}
异常就是如果不加this.show();或者this.setVisible(true);会导致运行10有7次出现界面不能正确显示很郁闷 |
|