java代码详细注释_java代码详细注释
請將下面的java程序代碼給加詳細的注釋(部分已給出),謝謝了,回答滿意再追加20分publicclassChatUDPJFrameextendsJFrameimplementsActionListener{privateStringname;//網(wǎng)名privat...
請將下面的java程序代碼給加詳細的注釋(部分已給出),謝謝了,回答滿意再追加20分
public class ChatUDPJFrame extends JFrame implements ActionListener
{
private String name; //網(wǎng)名
private InetAddress destip; //目標主機名或IP地址
private int destport; //目標主機的端口號
private JTextArea text_receiver; //顯示對話內(nèi)容的文本區(qū)
private JTextField text_sender; //輸入發(fā)送內(nèi)容的文本行
public ChatUDPJFrame(String name, String host, int destport, int receiveport) throws Exception
{
super("聊天室 "+name+" "+InetAddress.getLocalHost()+" : "+receiveport);
this.setBounds(320,240,400,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.text_receiver = new JTextArea();
this.text_receiver.setEditable(false);
this.getContentPane().add(new JScrollPane(this.text_receiver));
JPanel panel = new JPanel();
this.getContentPane().add(panel,"South");
this.text_sender = new JTextField(20);
panel.add(this.text_sender);
JButton button_send = new JButton("發(fā)送");
panel.add(button_send);
button_send.addActionListener(this);
this.setVisible(true);
this.name = name;
this.destip = InetAddress.getByName(host);
this.destport= destport;
byte data[] = new byte[512]; //以下接收數(shù)據(jù)報包并顯示
DatagramPacket pack=new DatagramPacket(data,data.length); //創(chuàng)建待接收數(shù)據(jù)報包
DatagramSocket socket=new DatagramSocket(receiveport); //創(chuàng)建待接收Socket
while (socket!=null)
{
socket.receive(pack); //接收數(shù)據(jù)報包
int length=pack.getLength(); //獲得包長度
String message=new String(pack.getData(),0,length);//獲得包中字節(jié)數(shù)據(jù)并轉(zhuǎn)成字符串
text_receiver.append(message+"\r\n");
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand()=="發(fā)送")
{
byte buffer[]=(name+" 說:"+text_sender.getText()).getBytes(); //將字符串轉(zhuǎn)換成字節(jié)
try
{
DatagramPacket pack=new DatagramPacket(buffer, buffer.length, destip, destport);
new DatagramSocket().send(pack); //綁定一個可用端口發(fā)送數(shù)據(jù)報
}
catch(Exception ex)
{
ex.printStackTrace();
}
text_receiver.append("我說:"+text_sender.getText()+"\n");
text_sender.setText("");
}
}
public static void main(String args[]) throws Exception
{
new ChatUDPJFrame("玉公主", "127.0.0.1", 3001, 3002);
}
}
展開
總結(jié)
以上是生活随笔為你收集整理的java代码详细注释_java代码详细注释的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java redis服务_java链接r
- 下一篇: java 局部性原理_程序局部性原理