📄 chatwindow.java
字号:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.*;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import java.awt.Color;
public class ChatWindow extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTabbedPane jTabbedPane = null;
private JPanel jPanel = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JPanel jPanel1 = null;
private JPanel jPanel2 = null;
private JTextArea jt_content = null;
private JPanel jPanel3 = null;
private JButton jb_Send = null;
private JScrollPane jScrollPane = null;
private JPanel jPanel4 = null;
private JButton jb_Close = null; //关闭按钮
private ListCellInfo lci=null; //单个好友对象
private int target_port=0; //目标端口
private String target_addr=null; //目录地址
private JList list=null; //好友列表
private DefaultListModel dlm_Friend=null; //保存好友数据的列表模型
private Login login=null;
private String []strarray=null;
private boolean reply=false; //是否为回复窗口
/**
* This is the default constructor
*/
public ChatWindow(JList list,Login login) {
super();
this.login=login;
this.list=list;
this.lci=(ListCellInfo)this.list.getSelectedValue();
this.setTitle("向 "+lci.getName()+" 发送消息");
initialize();
}
public ChatWindow(String []strarray,Login login)
{
//读取好友更详细的信息
this.login=login;
this.strarray=strarray;
this.dlm_Friend=login.qqbar.dlm_Friend;
this.lci=getlci(strarray[3]); //获得更详细的信息
this.setTitle("收到 "+strarray[3]+" 发来的消息"); //设置窗口标题
initialize();
this.jt_content.setText(strarray[5]); //显示消息内容
this.jt_content.setEditable(false); //内容不可编辑
this.jt_content.setBackground(new Color(232, 237, 237));
this.jb_Send.setText("回复");
}
private ListCellInfo getlci(String username)
{
if(dlm_Friend!=null)
{
for(int i=0;i<dlm_Friend.getSize();i++)
{
ListCellInfo lci_temp=(ListCellInfo)dlm_Friend.get(i);
if(lci_temp.getUid().equals(username))
return lci_temp;
}
}
return null;
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(497, 381);
this.setContentPane(getJContentPane());
this.setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJTabbedPane(), BorderLayout.CENTER);
jContentPane.add(getJPanel(), BorderLayout.NORTH);
}
return jContentPane;
}
/**
* This method initializes jTabbedPane
*
* @return javax.swing.JTabbedPane
*/
private JTabbedPane getJTabbedPane() {
if (jTabbedPane == null) {
jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("文本聊天", null, getJPanel1(), null);
jTabbedPane.addTab("文件传输", null, getJPanel2(), null);
jTabbedPane.addTab("聊天记录", null, getJPanel4(), null);
}
return jTabbedPane;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel1 = new JLabel();
jLabel1.setText(lci.getName());
FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
flowLayout.setVgap(5);
flowLayout.setHgap(5);
jLabel = new JLabel();
jLabel.setText("");
jLabel.setIcon(Function.GetPixels(lci.getIcon(),40,40));
jPanel = new JPanel();
jPanel.setLayout(flowLayout);
jPanel.add(jLabel, null);
jPanel.add(jLabel1, null);
}
return jPanel;
}
/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new JPanel();
jPanel1.setLayout(new BorderLayout());
jPanel1.add(getJPanel3(), BorderLayout.SOUTH);
jPanel1.add(getJScrollPane(), BorderLayout.CENTER);
}
return jPanel1;
}
/**
* This method initializes jPanel2
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel2() {
if (jPanel2 == null) {
jPanel2 = new JPanel();
jPanel2.setLayout(new GridBagLayout());
}
return jPanel2;
}
/**
* This method initializes jt_content
*
* @return javax.swing.JTextArea
*/
private JTextArea getJt_content() {
if (jt_content == null) {
jt_content = new JTextArea();
}
return jt_content;
}
/**
* This method initializes jPanel3
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel3() {
if (jPanel3 == null) {
jPanel3 = new JPanel();
jPanel3.setLayout(new FlowLayout());
jPanel3.add(getJb_Send(), null);
jPanel3.add(getJb_Close(), null);
}
return jPanel3;
}
/**
* This method initializes jb_Send
*
* @return javax.swing.JButton
*/
private JButton getJb_Send() {
if (jb_Send == null) {
jb_Send = new JButton();
jb_Send.setText("发送");
jb_Send.addActionListener(this);
}
return jb_Send;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJt_content());
}
return jScrollPane;
}
/**
* This method initializes jPanel4
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel4() {
if (jPanel4 == null) {
jPanel4 = new JPanel();
jPanel4.setLayout(new GridBagLayout());
}
return jPanel4;
}
/**
* This method initializes jb_Close
*
* @return javax.swing.JButton
*/
private JButton getJb_Close() {
if (jb_Close == null) {
jb_Close = new JButton();
jb_Close.setText("关闭");
jb_Close.addActionListener(this);
}
return jb_Close;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb_Send) //发送消息
{
if(jb_Send.getText().equals("发送"))
sendMsg();
else if(jb_Send.getText().equals("回复"))
{
this.jt_content.setEditable(true);
this.jt_content.requestFocus();
reply=true;
this.jb_Send.setText("发送");
}
}
else if(e.getSource()==jb_Close)
{
this.setVisible(false);
this.dispose();
}
}
private void sendMsg()
{
String local_addr=login.local_addr; //本地地址
int local_port=login.local_port;
String local_name=login.local_username;
String msg="1|3|"+local_name+"|"+lci.getUid()+"|"+jt_content.getText();
if(!reply)
{
if(!lci.isOnline()) //如果目标不在线
{
target_addr=login.server_addr;
target_port=login.server_port;
}
else //如果目标在线(需要发送本机地址、端口)
{
target_addr=lci.getLogip();
target_port=lci.getPort();
}
this.setVisible(false);
}
else //回复
{
this.target_addr=strarray[7]; //回复信息所需数据
this.target_port=Integer.parseInt(strarray[6]);
}
msg=msg+"|"+local_port+"|"+local_addr;
login.sm.setMsg(msg, target_port, target_addr, false, true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -