📄 singlechatgui.java
字号:
package edu.sccp.chat.frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.swtdesigner.SwingResourceManager;
import edu.sccp.chat.action.ToServerProcess;
import edu.sccp.chat.jarClass.JarAllege;
import edu.sccp.chat.tools.FileStream;
public class SingleChatGUI {
/**
* @param args
*/
JFrame frame;
public static JTextArea receiveField;
JTextArea sendField;
private String name;
String deptName;//自己部门名字
String bieId;//对方ID
String bieDept;//对方部门名字
String bieName;//对方名字
String pic;//对方头像
String zipic;//自己头像
String idstr;//自己ID
boolean fals=false;
ImageIcon image1;
ImageIcon image2;
SimpleDateFormat sim=new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat sim1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public SingleChatGUI(String pic1,String dept,String nameStr,String id,String ziname,String zidept,String pic11,String ziid,boolean fal)
//(对方头像,对方部门名字,对方名字,对方ID,自己的名字,自己部门名字,自己的头像,自己ID)
{
this.name=ziname;
this.deptName=zidept;
bieDept=dept;
bieName=nameStr;
bieId=id;
pic=pic1;
zipic=pic11;
idstr=ziid;
fals=fal;
image1=new ImageIcon(System.getProperty("user.dir")+pic);
image2=new ImageIcon(System.getProperty("user.dir")+zipic);
JarAllege.bai();
initGUI();
}
private void initGUI()
{
frame=new JFrame("与"+bieDept+" "+bieName+" 聊天中");
frame.setIconImage(SwingResourceManager.getImage(SingleChatGUI.class, "/edu/sccp/chat/image/icon.png"));
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent e) {
MainGUI.siliao.remove(bieId);
frame.dispose();
}
});
frame.getContentPane().setLayout(null);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBorder(BorderFactory.createTitledBorder("聊天内容"));
scrollPane.setBounds(10, 10, 305, 242);
frame.getContentPane().add(scrollPane);
receiveField = new JTextArea();
receiveField.setEditable(false);
receiveField.setLineWrap(true);
scrollPane.setViewportView(receiveField);
final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBorder(BorderFactory.createTitledBorder("发送内容"));
scrollPane_1.setBounds(10, 249, 307, 116);
frame.getContentPane().add(scrollPane_1);
sendField = new JTextArea();
sendField.setLineWrap(true);
sendField.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
int i=e.getKeyCode();//快捷键Shift
if(i==KeyEvent.VK_ENTER)
{
String str=deptName+" "+name+sim1.format(new Date(System.currentTimeMillis()))+"说:\n"+sendField.getText()+"\n";
receiveField.append(str);
try {
ToServerProcess.oos.writeObject("私聊");
ToServerProcess.oos.writeObject(bieId);//对方ID
ToServerProcess.oos.writeObject(deptName);//自己部门
ToServerProcess.oos.writeObject(name);//自己名字
ToServerProcess.oos.writeObject(zipic);//自己头像
ToServerProcess.oos.writeObject(idstr);//自己ID
ToServerProcess.oos.writeObject(str);//聊天内容
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileStream.writeFile(sim.format(new Date(System.currentTimeMillis())),str);
receiveField.setCaretPosition(receiveField.getText().length());
sendField.setText("");
}
}
});
scrollPane_1.setViewportView(sendField);
final JLabel label = new JLabel();
label.setText("对方形象");
label.setBounds(321, 25, 57, 16);
frame.getContentPane().add(label);
final JButton button = new JButton();
button.setBounds(321, 47, 55, 52);
button.setIcon(image1);
frame.getContentPane().add(button);
final JLabel label_1 = new JLabel();
label_1.setText("自身形象");
label_1.setBounds(321, 246, 57, 16);
frame.getContentPane().add(label_1);
final JButton button_1 = new JButton();
button_1.setIcon(image2);
button_1.setBounds(325, 268, 53, 51);
frame.getContentPane().add(button_1);
final JButton sendBtn = new JButton();
sendBtn.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
String str=deptName+" "+name+sim1.format(new Date(System.currentTimeMillis()))+"说:\n"+sendField.getText()+"\n";
receiveField.append(str);
try {
ToServerProcess.oos.writeObject("私聊");
ToServerProcess.oos.writeObject(bieId);
ToServerProcess.oos.writeObject(bieDept);
ToServerProcess.oos.writeObject(bieName);
ToServerProcess.oos.writeObject(zipic);
ToServerProcess.oos.writeObject(idstr);
ToServerProcess.oos.writeObject(str);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileStream.writeFile(sim.format(new Date(System.currentTimeMillis())),str);
receiveField.setCaretPosition(receiveField.getText().length());
sendField.setText("");
}
});
sendBtn.setText("发送");
sendBtn.setBounds(281, 371, 77, 25);
frame.getContentPane().add(sendBtn);
final JButton close = new JButton();
close.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
MainGUI.siliao.remove(bieId);
frame.dispose();
}
});
close.setText("关闭");
close.setBounds(190, 371, 77, 25);
frame.getContentPane().add(close);
}
public void showGUI()
{
frame.setResizable(false);
frame.setSize(392,433);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//new SingleChatGUI("张三").showGUI();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -