自己想要的是(图一)这种效果的。运行时有时是(图一)有时是(图二)的样子 ,概率都到50%,自己看不出那儿错了 ,代码在下面。希望谁给我指点下啊。
(图一)
(图二)
代码在下面:
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
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.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Send_Receive {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Windows_chat wc = new Windows_chat();
wc.winChat();
wc.southPanel();
wc.centerPanel();
wc.WindowsExit();
wc.sendButton();
}
}
class Windows_chat extends Frame {
private Button send;
private Button log;
private Button shake;
private Button clear;
private TextArea viewText;
private TextArea sendText;
private TextField tf;
public void winChat() {
setLocation(500, 50 );
setSize(400, 600);
setVisible(true);
}
public void pannel() {
}
public 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); //将Panel放在Frame的南边
}
public void centerPanel() {
Panel center = new Panel(); //创建中间的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("xxx", Font.PLAIN, 15));
viewText.setFont(new Font("xxx", Font.PLAIN, 15));
this.add(center,BorderLayout.CENTER);
}
public void WindowsExit() {
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void sendButton() {
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
sendwindow();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public void sendwindow() throws IOException {
String message = sendText.getText();
String ip = tf.getText();
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet =
new DatagramPacket(message.getBytes(),message.getBytes().length,InetAddress.getByName(ip),9999);
String time = getCurrentTime();
String view = time + " 我对:" + (ip.equals("255.255.255.255") ? "所有人" : ip) + "说\r\n" + message + "\r\n\r\n";;
viewText.append(view);
}
private String getCurrentTime() {
// TODO Auto-generated method stub
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
return sdf.format(d);
}
}