基于JavaGUI的网络通信编程(需使用Socket、ServerSocket类)

Endless_Void 2016-10-13 11:12:37
客户端:
package Chat;

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.awt.event.*;

class Client extends JFrame implements ActionListener {
JTextField tf1 = new JTextField("127.0.0.1");
JTextField tf2 = new JTextField("");
JTextField tf3 = new JTextField("4000");
JTextArea ta1 = new JTextArea("");
JButton button1 = new JButton("Connect");
JButton button2 = new JButton("Say");
Socket mysocket = null;
DataInputStream in = null;
DataOutputStream out = null ;

Client (){

JFrame frame=new JFrame("客户端");

Container contentPane = frame.getContentPane();
contentPane.setLayout( null );

JPanel panel = new JPanel();
panel.setBounds( 10, 10, 560 , 50 );
panel.setBorder(BorderFactory.createEtchedBorder() );
panel.setLayout( null );

JLabel label1 = new JLabel("客户机设置:");
label1.setBounds(10,0,80,25);
JLabel label2 = new JLabel("Server IP:");
label2.setBounds(10,10,60,25);
JLabel label3 = new JLabel("Say:");
label3.setBounds(10,165,80,25);
JLabel label4 = new JLabel("Server Port:");
label4.setBounds(230,10,85,25);

tf1.setBounds(80,10,140,25);
tf2.setBounds(50,165,420,25);
tf3.setBounds(320,10,140,25);

ta1.setBounds(10,65,557,100);

button1.setBounds(470,10,85,25);
button2.setBounds(480,165,85,25);

contentPane.add( label1 );
panel.add( label2 );
panel.add( tf1 );
panel.add( label4 );
panel.add( tf3 );
panel.add( button1 );
contentPane.add( panel );
contentPane.add( ta1 );
contentPane.add( label3 );
contentPane.add( tf2 );
contentPane.add( button2 );

button1.addActionListener(this);
button2.addActionListener(this);


frame.setBounds(650,300,600,250);
frame.setResizable(true);
frame.setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button1){
ta1.append("Connect to server…");
}
try{
mysocket = new Socket("127.0.0.1",4000);
out = new DataOutputStream(mysocket.getOutputStream());
in = new DataInputStream(mysocket.getInputStream());
}catch(IOException e2){ }
while(true){
try{
if(e.getSource()==button2){
out.writeUTF(tf2.getText());
}
String ic = in.readLine();
ta1.append("\n"+ic);
}catch(Exception e1){}
}
}
public static void main(String[] args){
new Client();
}
}

服务端:
package Chat;

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.awt.event.*;

class Server extends JFrame implements ActionListener {
JTextField tf1 = new JTextField("4000");
JTextField tf2 = new JTextField("");
JTextArea ta1 = new JTextArea("");
JButton button1 = new JButton("Start");
JButton button2 = new JButton("Say");
ServerSocket server = null;
Socket you = null;
DataInputStream in = null;
DataOutputStream out = null ;

Server (){
JFrame frame=new JFrame("服务器");

Container contentPane = frame.getContentPane();
contentPane.setLayout( null );

JPanel panel = new JPanel();
panel.setBounds( 10, 10, 560 , 50 );
panel.setBorder(BorderFactory.createEtchedBorder() );
panel.setLayout( null );

JLabel label1 = new JLabel("服务器设置:");
label1.setBounds(10,0,80,25);
JLabel label2 = new JLabel("Port:");
label2.setBounds(10,10,80,25);
JLabel label3 = new JLabel("Say:");
label3.setBounds(10,165,80,25);

tf1.setBounds(50,10,410,25);
tf2.setBounds(50,165,420,25);
ta1.setBounds(10,65,557,100);

button1.setBounds(470,10,80,25);
button2.setBounds(480,165,85,25);

contentPane.add( label1 );
panel.add( label2 );
panel.add( tf1 );
panel.add( button1 );
contentPane.add( panel );
contentPane.add( ta1 );
contentPane.add( label3 );
contentPane.add( tf2 );
contentPane.add( button2 );

button1.addActionListener(this);
button2.addActionListener(this);

frame.setBounds(650,300,600,250);
frame.setResizable(true);
frame.setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void actionPerformed(ActionEvent event){
if(event.getSource()==button1){
ta1.append("Server starting…");
}
try{
server = new ServerSocket(4000);
out = new DataOutputStream(you.getOutputStream());
in = new DataInputStream(you.getInputStream());
you = server.accept();
ta1.append("Client connected…");
}catch(IOException e2){}
while(true){
try{
if(event.getSource()==button2) {
out.writeUTF(tf2.getText());
}
String ip = in.readLine();
ta1.append("\n"+ip);
}catch(Exception e1){}
}
}
public static void main(String[] args){
new Server();
}
}

但不知哪出的问题,程序无法正常运行,刚学GUI,希望各位指正下错误。
...全文
315 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
computerclass 2016-10-14
  • 打赏
  • 举报
回复
java socket swing 你百度下看看
computerclass 2016-10-14
  • 打赏
  • 举报
回复
http://www.cnblogs.com/linzheng/archive/2011/01/23/1942328.html 你那个写的有点乱,你将这个调试成功了,改成你那个就行啦
computerclass 2016-10-14
  • 打赏
  • 举报
回复
我来看看。。。。
Endless_Void 2016-10-14
  • 打赏
  • 举报
回复
没人吗,自己顶顶
Endless_Void 2016-10-14
  • 打赏
  • 举报
回复
嗯,这样到网上找到蛮多资料学习了,谢谢。

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧