編寫一數據報通信程序
import java
import java
import java
import java
import
import
import
import
import javax
import javax
import javax
import javax
import javax
import javax
import javax
public class Frame extends JFrame implements WindowListener{
private JTextArea text;
private JTextField ipText;
private JTextField sendText;
private JButton button;
private JButton button
private JButton button
private DatagramSocket socket;
private JScrollBar vsBar;
public Frame(){
setTitle(
setBounds(
text=new JTextArea();
text
setLayout(null);
JScrollPane textPanel = new JScrollPane(text);
vsBar = textPanel
textPanel
getContentPane()
JLabel label=new JLabel(
label
label
getContentPane()
ipText = new JTextField();
ipText
getContentPane()
button=new JButton();
button
button
button
getContentPane()
button
button
button
button
getContentPane()
button
button
button
button
getContentPane()
button
this
sendText = new JTextField();
sendText
getContentPane()
//server();
pack();
setVisible(true);
}
class send implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
String ip=ipText
InetAddress address=InetAddress
byte[] data=sendText
DatagramPacket dp=new DatagramPacket(data
String myip=InetAddress
text
socket
sendText
}catch(Exception e
System
}
}
}
class clear implements ActionListener{
public void actionPerformed(ActionEvent e) {
text
}
}
class exit implements ActionListener{
public void actionPerformed(ActionEvent e) {
System
}
}
private void server() {
try {
socket=new DatagramSocket(
byte[] buf=new byte[
final DatagramPacket dp
Runnable runnable=new Runnable(){
public void run(){
while(true){
try{
Thread
socket
String message=new String(dp
String ip=dp
if(!InetAddress
text
}catch(Exception e){
System
}
}
}
};
new Thread(runnable)
} catch (SocketException e
e
}
}
public static void main(String[] args) {
Frame frame=new Frame();
}
public void windowActivated(WindowEvent e) {
// TODO Auto
}
public void windowClosed(WindowEvent e) {
// TODO Auto
}
public void windowClosing(WindowEvent e) {
// TODO Auto
System
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto
}
public void windowIconified(WindowEvent e) {
// TODO Auto
}
public void windowOpened(WindowEvent e) {
// TODO Auto
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25580.html