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); fileReceiver.start(); this.client.send("SPECIAL:"+list1.getSelectedItem()+"$SPECIAL$我已经准备好了,你开始发送文件吧"); } fileTransFlag=null; } /* * 发送文件 */ public void jButton9_actionPerformed(ActionEvent e) { filelength=0; if(list1.getSelectedItem()==null) { JOptionPane.showMessageDialog(this,"请选择要发送文件的用户,不能对所有人同时发送文件!","提示",JOptionPane.ERROR_MESSAGE); } else { bar.setVisible(true);//设置进度条可见 if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { filelength=fileChooser.getSelectedFile().length(); filedir=fileChooser.getSelectedFile().getAbsolutePath(); fileSender=new FileSender(list1.getSelectedItem(),filedir,list2); this.client.getListener().setFileSender(fileSender); this.client.send("FILE:"+list1.getSelectedItem()+"$FILE$"+fileChooser.getSelectedFile().getName()+"("+fileChooser.getSelectedFile().length()+"字节)"); fileTransFlag="SENDER"; list2.add("你想给 "+list1.getSelectedItem()+" 发送文件 "+filedir+" ,正在等待对方确认.."); } } } /* * 接收文件 */ public void jButton11_actionPerformed(ActionEvent e) { if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { jLabel5.setText(fileChooser.getSelectedFile().getName()); filedir=fileChooser.getSelectedFile().getAbsolutePath(); fileReceiver=new FileReceiver(filedir,list2); this.client.getListener().setFileReceiver(fileReceiver); fileTransFlag="RECEIVER"; list2.add("你准备将文件保存到 "+filedir+" ,按“开始”按钮接收文件.."); } } public void jButton12_actionPerformed(ActionEvent e) { if(list1.getSelectedItem()==null) { JOptionPane.showMessageDialog(this,"请选择语音聊天的对象!","提示",JOptionPane.ERROR_MESSAGE); } else { try { voiceSend=new VoiceSend(); voiceReceive=new VoiceReceive(); this.client.send("VOICE:"+list1.getSelectedItem()); this.client.getListener().setVoiceSend(voiceSend); } catch (SecurityException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (LineUnavailableException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } public void jButton13_actionPerformed(ActionEvent e) { if(list1.getSelectedItem()==null) { JOptionPane.showMessageDialog(this,"请选择拒绝的对象!","提示",JOptionPane.ERROR_MESSAGE); } else { jButton12.setVisible(true); jButton13.setVisible(false); jButton14.setVisible(false); jButton13.setText("拒绝语音"); this.client.send("VOICEEND:"+list1.getSelectedItem()); voiceReceive.stop(); voiceSend.stop(); } } /* * 接受语音 */ public void jButton14_actionPerformed(ActionEvent e) { try { voiceSend=new VoiceSend(); voiceReceive=new VoiceReceive(); voiceSend.setIP(VSip); voiceReceive.start(); voiceSend.start(); } catch (SecurityException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (LineUnavailableException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } class ClientFrame_jButton11_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton11_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton11_actionPerformed(e); } } class ClientFrame_jButton9_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton9_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton9_actionPerformed(e); } } class ClientFrame_jButton10_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton10_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton10_actionPerformed(e); } } class ClientFrame_jButton6_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton6_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton6_actionPerformed(e); } } class ClientFrame_jButton5_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton5_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton5_actionPerformed(e); } } class ClientFrame_jButton3_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton3_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton3_actionPerformed(e); } } class ClientFrame_jButton8_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton8_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton8_actionPerformed(e); } } class ClientFrame_jButton2_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton2_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed(e); } } class ClientFrame_jButton4_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton4_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton4_actionPerformed(e); } } class ClientFrame_jButton1_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton1_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); } } class ClientFrame_jButton12_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton12_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton12_actionPerformed(e); } } class ClientFrame_jButton13_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton13_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton13_actionPerformed(e); } } class ClientFrame_jButton14_actionAdapter implements ActionListener { private ClientFrame adaptee; ClientFrame_jButton14_actionAdapter(ClientFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton14_actionPerformed(e); } } }
最近下载更多
游弋time1105 LV1
5月15日
微信网友_6444139264921600 LV6
2023年4月29日
Sopuding LV1
2022年7月16日
tomtom113 LV2
2022年6月9日
ewan007 LV30
2022年4月21日
tdcq123 LV14
2022年3月13日
双方各何必呢 LV13
2021年12月20日
曹思辰 LV6
2021年11月21日
阳光正好 LV2
2021年10月29日
1005948011 LV7
2021年6月1日
最近浏览更多
yzj880427
10月12日
暂无贡献等级
444105047 LV6
6月25日
taoshen95 LV15
6月18日
暂无贡献等级
ntboss2002 LV3
5月3日
1112WHQ LV7
2023年11月3日
yuchunxing LV1
2023年9月2日
2017143155 LV12
2023年6月24日
lynn_zhou LV1
2023年5月30日
微信网友_6444139264921600 LV6
2023年4月29日