简单的聊天室,包含UI

来源:互联网 发布:云计算平台技术 编辑:程序博客网 时间:2024/06/05 15:15

此文本为客户端的聊天室,包括两种通许方式的代码,如下:

package liaotianshi;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JEditorPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


import javax.swing.DefaultComboBoxModel;


public class QQ extends JFrame {


private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private static Socket s;
private static  int port;
private static PrintWriter pw=null;
private static BufferedReader br=null;



/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
QQ frame = new QQ();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


/**
* Create the frame.
*/
public QQ() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 750, 450);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);

JLabel lblNewLabel = new JLabel("\u804A\u5929\u8F6F\u4EF6V1.0.0");

JTextArea textArea = new JTextArea();
textArea.setEditable(false);

JLabel lblNewLabel_1 = new JLabel("IP\u5730\u5740");

textField = new JTextField("175.3.15.9");
textField.setColumns(10);

JLabel lblNewLabel_2 = new JLabel("\u7AEF\u53E3\u53F7");

textField_1 = new JTextField("8528");
textField_1.setColumns(10);

JTextArea textArea_1 = new JTextArea();

JButton btnNewButton = new JButton("\u53D1\u9001");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
InetAddress name=InetAddress.getLocalHost();   //获取当前主机IP
String str = textArea_1.getText();
textArea_1.setText("");
Date date1=new Date();                   //获取当前时间
DateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
String time=format.format(date1);
ArrayList lis=new ArrayList();            //文件用集合打包
lis.add(name);
lis.add(str);
lis.add(time);


pw.println(lis);
pw.flush();

String string=br.readLine();
textArea.setText(textArea.getText()+"\n"+string);





} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}



}


});

JButton button = new JButton("\u53D6\u6D88");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea_1.setText("");
}
});
button.setFont(new Font("宋体", Font.PLAIN, 10));

JLabel lblNewLabel_3 = new JLabel("\u5B57\u4F53");

JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u5B8B\u4F53", "\u9ED1\u4F53", "\u4EFF\u5B8B"}));

JLabel lblNewLabel_4 = new JLabel("\u8868\u60C5");

JComboBox comboBox_1 = new JComboBox();
comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"@_@", "~_~", "(^_^)", "O_O", ""}));

JButton button_1 = new JButton("\u6E05\u5C4F");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText("");
}
});
button_1.setFont(new Font("宋体", Font.PLAIN, 10));

JButton button_2 = new JButton("\u8FDE\u63A5");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
s=new Socket(textField.getText(),Integer.parseInt(textField_1.getText()));
br=new BufferedReader(new InputStreamReader(s.getInputStream()));




} catch (NumberFormatException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
button_2.setFont(new Font("宋体", Font.PLAIN, 10));

JButton button_3 = new JButton("\u79BB\u7EBF\u53D1\u9001");
button_3.addActionListener(new ActionListener() {
/*
* 本方法使用UDP协议进行离线发送,只要指定地址及对方端口,无论对方事后作出回应都可已发送消息
*/
public void actionPerformed(ActionEvent arg0) {
try {
DatagramSocket sendSocket = new DatagramSocket();
String string = textArea_1.getText();
byte[] databyte = new byte[1000];
databyte = string.getBytes();
DatagramPacket sendPacket = new DatagramPacket(databyte,
string.length(), 
    InetAddress.getByName(textField.getText()),Integer.parseInt(textField_1.getText()));
sendSocket.send(sendPacket);
textArea.setText(textArea.getText()+"\n"+string);
textArea_1.setText("");
} catch (SocketException | UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});
button_3.setFont(new Font("宋体", Font.PLAIN, 10));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(textArea_1, GroupLayout.DEFAULT_SIZE, 714, Short.MAX_VALUE)
.addComponent(textArea, GroupLayout.DEFAULT_SIZE, 714, Short.MAX_VALUE))
.addContainerGap())
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap(590, Short.MAX_VALUE)
.addComponent(lblNewLabel)
.addGap(60))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(10)
.addComponent(lblNewLabel_1)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(lblNewLabel_2)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
.addGap(39)
.addComponent(lblNewLabel_3)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(lblNewLabel_4)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
.addGap(89))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(440)
.addComponent(button_3)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(button, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
.addGap(83))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 251, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(button_1, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
.addComponent(button_2, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textArea_1, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(button)
.addComponent(button_3, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
.addContainerGap())
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(282)
.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(109))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(285)
.addComponent(lblNewLabel_4)
.addGap(112))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(282)
.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(109))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(285)
.addComponent(lblNewLabel_3)
.addGap(112))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(282)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(109))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(285)
.addComponent(lblNewLabel_2)
.addGap(112))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(282)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(109))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(285)
.addComponent(lblNewLabel_1)
.addGap(112))
);
contentPane.setLayout(gl_contentPane);
}
}

原创粉丝点击