client.java
来自「一个简单的socket聊天工具,实现在发送文件和语音聊天等功能,还能聊天的同时听」· Java 代码 · 共 597 行 · 第 1/2 页
JAVA
597 行
output.flush();
input=new ObjectInputStream(connect.getInputStream());
}
private void processConnection() throws IOException{
do{
try{
message=(String)input.readObject();
if(message.equals("send")){
receiveButton.setVisible(true);
cancelButton.setVisible(true);
}
else if(message.equals("talk")){
retalkButton.setVisible(true);
}
else{
displayArea.append("\n"+message);
displayArea.setCaretPosition(displayArea.getText().length());
}
if(state)
sendData("对方暂时离开",1);
}
catch(ClassNotFoundException e){
displayArea.append("\n接收到不明信息");
}
}
while(!message.equals("对方>>>终止"));
}
private void closeConnection() throws IOException{
displayArea.append("\n对方终止连接");
enterArea.setEnabled(false);
sendButton.setEnabled(false);
output.close();
input.close();
connect.close();
}
private void sendData(String message,int choose){
if(choose==0){ //0为正常聊天信息
try{
Date today = new Date();
output.writeObject(nickName+" "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds()+":\n "+message);
output.flush();
displayArea.append("\n"+nickName+" "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds()+":\n "+message);
}
catch(IOException e){
displayArea.append("\n传送出错");
}
}
if(choose==1){
try{
output.writeObject(message);
output.flush();
}
catch(IOException e){
displayArea.append("\n传送出错");
}
}
}
/**
private void sendData(String message,int choose){
if(!message.equals("stop")){
try{
if(choose==1){
output.writeObject("send");
output.flush();
}
if(choose==2){
Date today = new Date();
output.writeBytes(nickName+" "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds()+"对你说:\n "+message);
output.flush();
displayArea.append("\n"+nickName+" "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds()+"对你说:\n "+message);
}
}
catch(IOException e){
displayArea.append("\n传输出错!");
}
}
else{
try{
output.writeObject("stop");
output.flush();
}
catch(IOException e){
displayArea.append("\n传输出错!");
}
}
}*/
/****************************************/
public void ini(){
MyDialog dlg = new MyDialog(myframe,true);
dlg.show();
}
/**************************重写接口*******************/
public void actionPerformed (ActionEvent e){
String str = e.getActionCommand();
//处理事件
if (str.equals("退出")){
sendData("stop",1);
}
if (str.equals("红色")){
displayArea.setForeground(Color.red);
}
if (str.equals("绿色")){
displayArea.setForeground(Color.green);
}
if (str.equals("蓝色")){
displayArea.setForeground(Color.blue);
}
if(str.equals("保存聊天记录")){
File file = null;
JFileChooser filechooser = new JFileChooser();;
if(file!=null) filechooser.setSelectedFile(file);
int returnVal=filechooser.showSaveDialog(myframe);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=filechooser.getSelectedFile();
try{
FileWriter fw=new FileWriter(file);
fw.write(displayArea.getText());
fw.close();
}
catch(Exception ex){ex.printStackTrace();}
}
}
if(str.equals("发送文件")){
OpenFile openFile = new OpenFile(myframe);
if(openFile.sendBack()!=null){
sendData("send",1);
sendData("即将向你发送文件:"+openFile.sendBack(),1);
}
openFile.start();
}
if(str.equals("取消")){
sendData("cancel",1);
receiveButton.setVisible(false);
cancelButton.setVisible(false);
}
if(str.equals("发送聊天")){
Vox vox = new Vox(new String(""));
vox.start();
sendData("talk",1);
sendData("正在呼叫中……",0);
}
if(str.equals("接受聊天")){
Vox vox = new Vox(client);
vox.start();
retalkButton.setVisible(false);
}
if(str.equals("接收文件")){
Save save = new Save(myframe,1,client);
save.start();
receiveButton.setVisible(false);
}
if(str.equals("聊天模式")){
displayArea.setVisible(true);
chat.setVisible(false);
mess.setVisible(true);
}
if(str.equals("消息模式")){
displayArea.setVisible(false);
chat.setVisible(true);
mess.setVisible(false);
}
}
public void itemStateChanged (ItemEvent e){
int st = e.getStateChange();
JCheckBoxMenuItem source = (JCheckBoxMenuItem) e.getSource();
int style = displayArea.getFont().getStyle();
if (source == boldItem){
if (st == e.SELECTED){
displayArea.setFont(new Font("SansSerif",style+Font.BOLD,14));
}
else {
displayArea.setFont(new Font("SansSerif",style-Font.BOLD,14));
}
}
else if(source == italicItem){
if (st == e.SELECTED){
displayArea.setFont(new Font("SansSerif",style+Font.ITALIC,14));
}
else {
displayArea.setFont(new Font("SansSerif",style-Font.ITALIC,14));
}
}
else if(source == mouseItem1){
state =!state;
}
}
public void mouseReleased(MouseEvent e){
if(e.isPopupTrigger())
popupMenu.show(displayArea,e.getX(),e.getY());
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
/**public boolean keyDown (Event evt, int key) {
if (evt.controlDown()){
……… //显示大写字母
else
……… //显示小写字母
}
}*/
class MyDialog extends JDialog implements ActionListener{
JLabel label = new JLabel("请输入对方的IP");
JTextField text = new JTextField(15);
JButton ok = new JButton("确定");
JButton cancel = new JButton("取消");
MyDialog(JFrame demo,boolean modal){
super(demo,modal);
setSize(300,150);
Container con = getContentPane();
con.add(label);
con.add(text);
text.setText("59.64.232.196");
Box box = Box.createHorizontalBox();
box.add(ok);
box.add(cancel);
con.add(box);
label.setBounds(30,30,100,20);
text.setBounds(150,30,90,20);
ok.addActionListener(this);
cancel.addActionListener(this);
setLocation(300,180);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ok){
client = text.getText();
}
dispose();
}
}
/****************************************/
/** private void prepareSplash()
{
Toolkit toolkit = Toolkit.getDefaultToolkit();
windowSplash = new Window(myframe);
Image image = toolkit.getImage( "images" + File.separator + "splash.gif" );
//ImageCanvas canvas = (ImageCanvas)image;
//windowSplash.add( canvas, "Center" );
Dimension scmSize = toolkit.getScreenSize();
int imgWidth = image.getWidth(myframe);
int imgHeight = image.getHeight(myframe);
windowSplash.setLocation( scmSize.width/2 - (imgWidth/2), scmSize.height/2 - (imgHeight/2));
windowSplash.setSize( imgWidth, imgHeight );
}
private void startSplash()
{
windowSplash.setVisible( true );
windowSplash.toFront();
}
private void stopSplash()
{
windowSplash.dispose();
}
*/
public static void main(String args[]){
Client client = new Client();
client.ini();
client.run();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?