该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package Test04;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.net.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
@SuppressWarnings("serial")
public class EasyQQ extends JFrame {
private JPanel jContentPane = null;
private JTextField message = null;
private JButton sendButton = null;
private JTextArea showMsg = null;
private int personalPort;
private JLabel ipLabel = new JLabel("QQ-01");
private JLabel portLabel = new JLabel("QQ-02");
private JTextField IP = new JTextField();
private JTextField PORT = new JTextField(""+personalPort);//没用。表达连接的端口一样的意思。
private JButton testConn = new JButton("连接");
private JButton cleaner = new JButton("清空消息框");
{
ipLabel.setBounds(100,50,50,30);
IP.setBounds(150, 50, 80, 30);
portLabel.setBounds(250, 50, 50, 30);
PORT.setBounds(300, 50, 80, 30);
testConn.setBounds(400, 50, 100, 30);
cleaner.setBounds(100,250,150,30);
testConn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String url = IP.getText();
try {
int port = Integer.parseInt(PORT.getText());
if(openClient(url, port))showMsg.setText(showMsg.getText()+"\n连接成功");
else showMsg.setText(showMsg.getText()+"\n连接失败");
} catch (NumberFormatException e1) {
showMsg.setText(showMsg.getText()+"\n请输入数字型端口号!");
}
}
});
cleaner.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showMsg.setText("");
}
});
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (message == null) {
message = new JTextField();
message.setBounds(100,300,300,30);
}
return message;
}
private JTextArea getJTextArea() {
if (showMsg == null) {
showMsg = new JTextArea();
showMsg.setBackground(Color.orange);
showMsg.setBounds(100,80,400,150);
}
return showMsg;
}
/**
* This method initializes send()Button
*
* @return javax.swing.JButton
*/
ServerSocket ss;
Socket get;
PrintWriter out ;
Socket send1() {
return null;
}
BufferedReader in;
private JButton getsendButton() {
if (sendButton == null) {
sendButton = new JButton();
sendButton.setBounds(400,300,100,30);
sendButton.setText("发送");
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
send1();
}
});
message.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)send1();
}
});
}
return sendButton;
}
/**
* @param args
*/
public static void main(String[] args) {
new EasyQQ(6);
}
/**
* This is the default constructor
*/
public EasyQQ(int personalPort) {
this.personalPort = personalPort;
new Thread(){
public void run(){
for (int i = EasyQQ.this.personalPort; i < 9999; i++) {
try {
ss = new ServerSocket(i);
IP.setText("1414798079");
PORT.setText("694909285"+i);
EasyQQ.this.setTitle("重庆邮电大学专用QQ客户端"+i);
Socket send = ss.accept();
break;
} catch (IOException e) {
}
}
}
}.start();
this.setContentPane(getJContentPane());
this.setBounds(400,300,600,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
private boolean openClient(String serverURL,int serverPort){
try {
get = new Socket(serverURL,serverPort);
new Thread(){
public void run(){
try{
in = new BufferedReader(new InputStreamReader(get.getInputStream()));
while(true){
String msg;
if((msg=in.readLine()).length() != 0)
showMsg.append("\n对方:"+msg);
Thread.sleep(500);
}
}catch(Exception ew){
}
}
}.start();
return true;
} catch (Exception e) {
showMsg.setText( showMsg.getText()+"失败IP:"+serverURL);
return false;
}
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(this.ipLabel);
jContentPane.add(this.IP);
jContentPane.add(this.portLabel);
jContentPane.add(this.PORT);
jContentPane.add(this.testConn);
jContentPane.add(this.cleaner);
JScrollPane jsp = new JScrollPane(getJTextArea());
jsp.setBounds(100,80,400,150);
jContentPane.add(jsp);
jContentPane.add(getJTextField());
jContentPane.add(getsendButton());
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="122,25"
本文介绍了一个简易QQ客户端的设计与实现,使用Java Swing进行图形界面开发,并通过Socket编程实现了客户端与服务器之间的基本通信功能。

被折叠的 条评论
为什么被折叠?



