⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientframe.java

📁 使用java语言写的聊天工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.BorderLayout;

import javax.sound.sampled.LineUnavailableException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class ClientFrame extends JFrame 
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Client client=null;
    FileSender fileSender=null;
    FileReceiver fileReceiver=null;
    static VoiceReceive voiceReceive=null;
    static VoiceSend voiceSend=null;
    String fileTransFlag=null;//文件发送接收标记
    static String VSip="";
    static long filelength=0;
    String filedir="";
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel1 = new JPanel();
    JPanel jPanel2 = new JPanel();
    JPanel jPanel3 = new JPanel();
    JPanel jPanel4 = new JPanel();
    JPanel jPanel5 = new JPanel();
    JLabel jLabel1 = new JLabel();
    JTextField jip = new JTextField();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JLabel jLabel3 = new JLabel();
    List list1 = new List();
    BorderLayout borderLayout2 = new BorderLayout();
    JLabel jnick=new JLabel();
    JLabel jnickname = new JLabel();
    JButton jButton3 = new JButton();
    JTextArea jsend = new JTextArea(6,40);
    JButton jButton4 = new JButton();
    BorderLayout borderLayout3 = new BorderLayout();
    JPanel jPanel6 = new JPanel();
    JButton jButton5 = new JButton();
    JButton jButton6 = new JButton();
    List list2 = new List();
    BorderLayout borderLayout4 = new BorderLayout();
    JButton jButton7 = new JButton();
    JButton jButton8 = new JButton();
    JCheckBox jCheckBox1 = new JCheckBox();
    JPanel jPanel7 = new JPanel();
    JButton jButton9 = new JButton();
    JButton jButton10 = new JButton();
    JLabel jLabel4 = new JLabel();
    JButton jButton11 = new JButton();
    JFileChooser fileChooser=new JFileChooser();
    JPanel jPanel9 = new JPanel();
    static JProgressBar bar = new JProgressBar ();
    static JLabel jLabel5 = new JLabel();
    static JButton jButton12 = new JButton();
    static JButton jButton13 = new JButton();
    static JButton jButton14 = new JButton();  
    public ClientFrame() 
    {
        try 
        {
            jbInit();            
        } 
        catch (Exception exception) 
        {
            JOptionPane.showMessageDialog(null,"对不起,有错误,请重启程序。","错误",JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private void jbInit() throws Exception 
    {
        getContentPane().setLayout(borderLayout1);
        jButton1.addActionListener(new ClientFrame_jButton1_actionAdapter(this));
        jButton2.addActionListener(new ClientFrame_jButton2_actionAdapter(this));
        jButton6.addActionListener(new ClientFrame_jButton6_actionAdapter(this));
        jButton5.addActionListener(new ClientFrame_jButton5_actionAdapter(this));
        jButton4.addActionListener(new ClientFrame_jButton4_actionAdapter(this));
        jButton3.addActionListener(new ClientFrame_jButton3_actionAdapter(this));
        jButton12.addActionListener(new ClientFrame_jButton12_actionAdapter(this));
        jButton13.addActionListener(new ClientFrame_jButton13_actionAdapter(this));
        jButton14.addActionListener(new ClientFrame_jButton14_actionAdapter(this));
        jButton7.setText("jButton7");
        jButton8.setText("清空消息");
        jButton8.addActionListener(new ClientFrame_jButton8_actionAdapter(this));
        jCheckBox1.setSelected(true);
        jCheckBox1.setText("对所有人");
        jPanel7.setLayout(new GridLayout(5,1));
        jButton9.setText("发送");
        jButton9.addActionListener(new ClientFrame_jButton9_actionAdapter(this));
        jButton10.setText("开始");
        jButton10.addActionListener(new ClientFrame_jButton10_actionAdapter(this));
        jLabel4.setText("传输文件:");
        jButton11.setText("接收");
        jButton11.addActionListener(new ClientFrame_jButton11_actionAdapter(this));
        this.getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
        jip.setText("218.194.129.133");
        jip.setColumns(15);
        jButton1.setText("登陆");
        jButton2.setText("退出");
        jLabel3.setText("在线用户列表:");
        jPanel2.setLayout(borderLayout2);
        jnick.setText("昵称:");
        jnickname.setText("昵称已然存在");
        jButton3.setText("修改昵称");
        jsend.setLineWrap(true);
        jsend.setWrapStyleWord(true);
        jButton4.setText("发送");
        jPanel4.setLayout(borderLayout3);
        jButton5.setText("查看历史消息");
        jButton6.setText("保存消息");
        jPanel6.setLayout(new GridLayout(3,1));
        jPanel5.setLayout(borderLayout4);
        jPanel1.add(jLabel1);
        jPanel1.add(jip);
        jPanel1.add(jButton1);
        jPanel1.add(jButton2);
        jPanel1.add(jnick);
        jPanel1.add(jnickname);
        jPanel1.add(jButton3);
        jLabel1.setText("服务器IP地址:");
        this.getContentPane().add(jPanel2, java.awt.BorderLayout.WEST);
        jPanel2.add(list1, java.awt.BorderLayout.CENTER);
        this.getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);
        jPanel3.add(jsend);
        jPanel3.add(jButton4);
        this.getContentPane().add(jPanel4, java.awt.BorderLayout.EAST);
        jPanel6.add(jButton6, null);
        jPanel6.add(jButton8);
        jPanel6.add(jButton5, null);
        this.getContentPane().add(jPanel5, java.awt.BorderLayout.CENTER);
        jPanel5.add(list2, java.awt.BorderLayout.CENTER);
        jPanel2.add(jLabel3, java.awt.BorderLayout.NORTH);
        jPanel2.add(jCheckBox1, java.awt.BorderLayout.SOUTH);
        jPanel4.add(jPanel6, java.awt.BorderLayout.NORTH);
        jPanel4.add(jPanel7, java.awt.BorderLayout.SOUTH);
        jPanel7.add(jLabel4);
        jPanel7.add(jButton9);
        jPanel7.add(jButton11);
        jPanel7.add(jButton10);
        this.setTitle("聊天室客户端");
        this.setSize(800,600);
        this.setLocation(100,100);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        /*
         * 进度条处定义
         */
        jPanel9.setLayout(new GridLayout(6,1));
        jPanel4.add(jPanel9, java.awt.BorderLayout.CENTER);        
        bar.setForeground(new java.awt.Color(102, 255, 102));
        //bar.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)), "总进度", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 13), new java.awt.Color(0, 0, 255)));
        bar.setDoubleBuffered(true);
        bar.setStringPainted ( true ) ;
        bar.setSize (320,20);
        jPanel9.add(jLabel5);
        jPanel9.add(bar);
        bar.setVisible(false);
        jPanel9.add(jButton12);
        jPanel9.add(jButton14);
        jPanel9.add(jButton13);
        jButton14.setText("接受语音");
        jButton14.setVisible(false);
        jButton12.setText("语音聊天");
        jButton13.setText("拒绝语音");
        jButton13.setVisible(false);
    }

    public static void main(String[] args) 
    {
        ClientFrame clientframe = new ClientFrame();
    }

    public void jButton1_actionPerformed(ActionEvent e) 
    {
        try 
        {
            client = new Client(jip.getText(),9999, list1,list2);
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(this,"服务器不存在或者没有启动!","提示",JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
        if(jnickname.getText().length()>0)
        {
            this.client.send("NICKNAME:"+jnickname.getText());
        }
        else
        {
        	JOptionPane.showMessageDialog(this,"为了便于区分,请及时添加你的昵称.","提示",JOptionPane.INFORMATION_MESSAGE);
        }
     }

    public void jButton4_actionPerformed(ActionEvent e) 
    {
        if(jsend.getText().length()>0)
        {
        	//判断是给所有人发还是只给指定的客户端发
            if(jCheckBox1.isSelected())
            {
                this.client.send(jsend.getText());
            }
            else
            {
                if(list1.getSelectedItem()==null)
                {
                    JOptionPane.showMessageDialog(this,"请选择要发送信息的用户,或者选中“对所有人”按钮!","提示",JOptionPane.ERROR_MESSAGE);
                }
                else
                {
                    this.client.send("SPECIAL:"+list1.getSelectedItem()+"$SPECIAL$"+jsend.getText());
                    this.list2.add("你对 "+list1.getSelectedItem()+" 悄悄地说:"+jsend.getText());
                }
            }
            jsend.setText("");
        }
    }
    public void jButton2_actionPerformed(ActionEvent e) 
    {
        this.client.destroy();
        this.client=null;
        ClientFrame.bar.setVisible(false);
        ClientFrame.jLabel5.setText("");
        ClientFrame.filelength=0;
    }

    public void jButton8_actionPerformed(ActionEvent e) 
    {
        if(JOptionPane.showConfirmDialog(this,"确定要清空所有信息吗?","清空信息",JOptionPane.YES_NO_OPTION)==0)
        {
            this.list2.removeAll();
        }
    }
/*
 * 修改昵称
 */
    public void jButton3_actionPerformed(ActionEvent e) 
    {
    	jnickname.setText(JOptionPane.showInputDialog("请输入昵称:"));    	
        if(jnickname.getText().length()>0)
        {
            this.client.send("NICKNAME:"+jnickname.getText());
        }
    }

    public void jButton5_actionPerformed(ActionEvent e) 
    {
    	/*
    	 * 此处添加代码实现历史消息的查看。
    	 */
    }
/*
 * 保存消息
 */
    public void jButton6_actionPerformed(ActionEvent e) 
    {
        if(fileChooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
        {
            File file = fileChooser.getSelectedFile();
            try 
            {
                PrintStream ps = new PrintStream(new FileOutputStream(file));
                for (int i = 0; i < list2.getItemCount(); i++) 
                {
                    ps.println(list2.getItem(i));
                }
                ps.close();
            } 
            catch (FileNotFoundException ex) 
            {
                JOptionPane.showMessageDialog(this, "保存失败!", "提示",JOptionPane.ERROR_MESSAGE);
            }
            JOptionPane.showMessageDialog(this, "保存成功!", "提示",JOptionPane.INFORMATION_MESSAGE);
        }
    }
/*
 *文件传输开始
 */
    public void jButton10_actionPerformed(ActionEvent e)
    {
        if(fileTransFlag==null)
        {
        	JOptionPane.showMessageDialog(this, "请选择是接受或者是发送文件!", "提示",JOptionPane.ERROR_MESSAGE);
        }
        else if(fileTransFlag.equalsIgnoreCase("sender"))
        {
        	bar.setVisible(true);
            fileSender.start();
            this.client.send("FLENGTH:"+list1.getSelectedItem()+"$SPECIAL$"+filelength);
        }
        else if(fileTransFlag.equalsIgnoreCase("receiver"))
        {
        	bar.setVisible(true);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -