public class GUI_Chat extends Frame {
private TextField tf;
private Button send;
private Button log;
private Button clear;
private TextArea viewText;
private TextArea sendText;
private DatagramSocket datagramSocket;
private BufferedWriter bw;
private Button shake;
public GUI_Chat() {
init();
southPanel();
centerPanel();
event();
}
public void event() {
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
try {
bw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
datagramSocket.close();
System.exit(0);
}
});
log.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
logFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
send();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
viewText.setText("");
}
});
shake.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
send (new byte[]{-1},tf.getText());
}
});
sendText.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
//if(e.getKeyCode() == KeyEvent.VK_ENTER && e.isControlDown())
if(e.getKeyCode() == KeyEvent.VK_ENTER)
try {
send();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
private void shake() {
int x = this.getLocation().x;
int y = this.getLocation().y;
for(int i = 1; i < 20 ;i++) {
this.setLocation(x+20, y+20);
try {
Thread.sleep(20);
this.setLocation(x-20, y+20);
Thread.sleep(20);
this.setLocation(x+20, y-20);
Thread.sleep(20);
this.setLocation(x-20, y-20);
Thread.sleep(20);
this.setLocation(x, y);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void logFile() throws IOException {
bw.flush();
FileInputStream fis = new FileInputStream("config.txt");
ByteArrayOutputStream baos = new ByteArrayOutputStream();//在内存中创建缓存区
int len;
byte[] arr = new byte[8192];
while((len = fis.read(arr)) != -1) {
baos.write(arr,0,len);
}
String string = baos.toString();
viewText.setText(string);
fis.close();
}
private void send (byte[]arr, String ip) {
DatagramPacket datagramPacket;
try {
datagramPacket = new DatagramPacket
(arr, arr.length, InetAddress.getByName(ip), 9999);
datagramSocket.send(datagramPacket);//发送
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void send() throws IOException {
String message = sendText.getText();//获取发送区域的内容
String ip = tf.getText();
ip = ip.trim().length() == 0 ? "255.255.255.255":ip;
send(message.getBytes(),ip);
String time = getCurrentTime();
String str = time + "我对:"+(ip.equals("255.255.255.255") ? "所有人":ip)+"说"+"\r\n"+message+"\r\n";//alt+shift+L抽取局部变量
viewText.append(str);//将信息添加到显示区域
bw.write(str);//将数据写出
sendText.setText("");
}
private String getCurrentTime() {
Date s = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
return sdf.format(s);//将时间格式化
}
public 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);//设置背景颜色
sendText.setFont(new Font("fff",Font.PLAIN,20));
viewText.setFont(new Font("fff",Font.PLAIN,20));
this.add(center,BorderLayout.CENTER);
}
public void southPanel() {
Panel south = new 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);//将panel 放在Frame的南边;
}
public void init() {
this.setLocation(500, 50);
this.setSize(400, 600);
new Receive().start();
try {
datagramSocket = new DatagramSocket();
bw = new BufferedWriter(new FileWriter("config.txt",true));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setVisible(true);
}
private class Receive extends Thread {//接收和发送需要同时进行,所以需要定义成多线程
public void run () {
try {
DatagramSocket datagramSocket = new DatagramSocket(9999);
DatagramPacket datagramPacket = new DatagramPacket(new byte[8192], 8192);
while(true) {
datagramSocket.receive(datagramPacket);
byte[] arr = datagramPacket.getData();
String ip = datagramPacket.getAddress().getHostAddress();
int len = datagramPacket.getLength();
if(arr[0] == -1 && len == 1) {//因为数组长度为1,且存储值为-1,震动
shake();
continue;//终止本次循环,进入下一次循环
}
int port = datagramPacket.getPort();
String time = getCurrentTime();
String str = time + " " + ip + " 对我说:\r\n" + new String(arr,0,len) + "\r\n";
viewText.append( str);
bw.write(str);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new GUI_Chat();
}
}
|
|