Java语言,使用Swing插件实现基于UDP的文字传输程序

要求:使用swing插件中的JFrame实现程序使用基于UDP的套接字来实现文字传输填补服务器端程序的空缺处,实现客户端... 要求:
使用swing插件中的JFrame实现程序
使用基于UDP的套接字来实现文字传输
填补服务器端程序的空缺处,实现客户端
展开
 我来答
kanellsj
2008-11-01 · TA获得超过1942个赞
知道小有建树答主
回答量:795
采纳率:0%
帮助的人:707万
展开全部
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
public class HostComputer1 extends JFrame implements ActionListener,Runnable{
DatagramSocket socket;
byte[]buf=new byte[256];
InetAddress address;
JTextArea t1=new JTextArea();
JTextField t2=new JTextField(20);
DatagramPacket dp=new DatagramPacket(buf,buf.length);
public HostComputer1(){
JScrollPane jsp=new JScrollPane(t1);
this.getContentPane().add(jsp,"Center");
JPanel p1=new JPanel();
p1.add(new JLabel("发送的信息"));
p1.add(t2);
t2.addActionListener(this);
this.getContentPane().add(p1,"South");
setTitle("差改我是主机1");
setSize(350,200);
setVisible(true);
(new Thread(this)).start();
}
public void actionPerformed(ActionEvent e){
try{
byte [] b=t2.getText().getBytes();
DatagramSocket socket=new DatagramSocket();
InetAddress address=InetAddress.getByName("衫亏localhost");
DatagramPacket packet=new DatagramPacket (b,b.length,address,6666);
socket.send(packet);
t1.append("发送的数据:"+t2.getText()+"\n");
t1.append("数据发送到:"+address+"\n");
t1.append("数据长度为:"+packet.getLength()+"\n");
}catch(Exception ee){ee.printStackTrace();}
}
public void run(){
try{
DatagramSocket socket=new DatagramSocket(8888);
while(true){
socket.receive(dp) ;
String received=new String(dp.getData(),0,dp.getLength());
t1.append("收到的数据:"+received+"\n");
t1.append("数据来自于:"+dp.getAddress()+"\n");
t1.append("数据长度为:"+dp.getLength()+"\n");
}
}catch(Exception e){System.out.println("Error"+e);}
}
public static void main(String [] args){
JFrame.setDefaultLookAndFeelDecorated(true);
Font font=new Font("JFrame",Font.PLAIN,14);
Enumeration key=UIManager.getLookAndFeelDefaults().keys();
while(key.hasMoreElements()) {
Object keys=key.nextElement();
if(UIManager.get(keys) instanceof Font)
UIManager.put(keys,font);
}
new HostComputer1();
}
}

若虚塌判要生成一个名为 cal.jar 的可执行jar文件:(文件名可以是任意合法名字)
(这是我认为简单实用的一种方法,还有很多别的方法在此就不介绍了)
第一 把程序生成的所有字节码文件(即.class文件)放在同一个目录下(如:D:/chat/).
第二 在该目录下新建一个manifest.mf文件,文件内容格式如下(划线中内容):
manifest.mf文件中的格式:
--------------------------------
Main-Class: calDemo

--------------------------------
注意: calDemo代表主类名(即要运行的类名,只能有一个,不要文件扩展名)
Main与Class中间不是下划线,而是短横线
Main-Class:与calDemo中间必须要有空格
Main-Class: calDemo之后必须要回车

文件中还可以加入一些其他信息如:(先不要加,以免出错)

--------------------------------------------
Manifest-Version: 1.0
Created-By: 1.4.1_02 (Sun Microsystems Inc.)
--------------------------------------------

然后用jar命令生成可执行的jar文件,例如:
(执行该命令前先要转到该目录下 D:/chat/ )
jar cvfm cal.jar manifest.mf *.class

如果要生成EXE格式的,可以去网上下个exe4j
趣味英语启蒙
2008-11-01 · TA获得超过164个赞
知道小有建树答主
回答量:204
采纳率:0%
帮助的人:87.9万
展开全部
package socket;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import javax.swing.*;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Color;
import javax.swing.JLabel;

public class Client extends JFrame implements Runnable {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JPanel btnPanel = null;

private JButton sendButton = null;

private JButton closeButton = null;

private JButton connButton = null;

private JTextArea outTextArea = null;

private List dialist = null;

Socket sock;

Thread thread;

BufferedReader in;

PrintWriter out;

public final static int DEFAULT_PORT = 8808;

boolean bConnected;

private JLabel imgLabel = null;

private JLabel img2Label = null;

private JLabel img3Label = null;

public Client() {
super();
initialize();
}

private void initialize() {
this.setSize(355, 267);
this.setContentPane(getJContentPane());
this.setTitle("客户端");
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setResizable(false);
}

private JPanel getBtnPanel() {
if (btnPanel == null) {
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.gridx = 3;
gridBagConstraints3.gridy = 1;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 1;
gridBagConstraints2.gridy = 1;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 1;
btnPanel = new JPanel();
btnPanel.setLayout(new GridBagLayout());
btnPanel.setBounds(new Rectangle(1, 187, 347, 47));
btnPanel.add(getSendButton(), gridBagConstraints1);
btnPanel.add(getCloseButton(), gridBagConstraints2);
btnPanel.add(getConnButton(), gridBagConstraints3);
}
return btnPanel;
}

private JButton getSendButton() {
if (sendButton == null) {
sendButton = new JButton();
sendButton.setText("发送");
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String msg = outTextArea.getText();

if (msg!=null) { //此句无效???
processmsg(msg);
outTextArea.setText(null);
try {
sendmsg(msg);
} catch (IOException e2) {
processmsg(e2.toString());
}
}
}
});
}
return sendButton;
}

private JButton getCloseButton() {
if (closeButton == null) {
closeButton = new JButton();
closeButton.setText("关闭");
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(EXIT_ON_CLOSE);
}
});
closeButton.setText("关闭");
}
return closeButton;
}

private JButton getConnButton() {
if (connButton == null) {
connButton = new JButton();
connButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startConnect();
}

});
connButton.setText("链接");
}
return connButton;
}

private JTextArea getOutTextArea() {
outTextArea = new JTextArea();
outTextArea.setLocation(new Point(0, 119));
outTextArea.setSize(new Dimension(269, 68));
return outTextArea;
}

public void processmsg(String str) {
this.dialist.add(str);
}

private List getDialist() {
dialist = new List();
dialist.setLocation(new Point(-1, 1));
dialist.setSize(new Dimension(271, 94));
return dialist;
}

public void run() {
while (true) {
try {
String msg = receivemsg();
Thread.sleep(100L);
if (msg != null) {
processmsg(msg);
}
} catch (IOException e) {
e.printStackTrace();
}catch (InterruptedException ei) {}
}
}

public void sendmsg(String msg)throws IOException {
out.println(msg);
out.flush();
}

public String receivemsg() throws IOException{
String msg = new String();
try {
msg = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return msg;
}

public void startConnect() {
bConnected = false;
try {
sock = new Socket("127.0.0.1", DEFAULT_PORT);

bConnected = true;
processmsg("connect ok!");
in =new BufferedReader(new InputStreamReader(sock.getInputStream()));
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())));
} catch (IOException e) {
e.printStackTrace();
processmsg("connection failed");
}
if(thread==null){
thread=new Thread(this);
thread.start();
}
}

private JPanel getJContentPane() {
if (jContentPane == null) {
img3Label = new JLabel();
img3Label.setText("");
img3Label.setSize(new Dimension(269, 23));
img3Label.setLocation(new Point(1, 94));
img2Label = new JLabel();
img2Label.setBounds(new Rectangle(270, 95, 81, 89));
img2Label.setIcon(new ImageIcon(getClass().getResource("/socket/10.jpg")));
img2Label.setText("");
imgLabel = new JLabel();
imgLabel.setBounds(new Rectangle(270, 0, 78, 94));
imgLabel.setIcon(new ImageIcon(getClass().getResource("/socket/3.jpg")));
imgLabel.setText("");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getBtnPanel(), null);
jContentPane.add(getOutTextArea(), null);
jContentPane.add(getDialist(), null);
jContentPane.add(imgLabel, null);
jContentPane.add(img2Label, null);
jContentPane.add(img3Label, null);
}
return jContentPane;
}

public static void main(String[] args) {
Client cl = new Client();

}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式