java类似QQ通信

时间:2014-06-30 15:14:08
【文件属性】:

文件名称:java类似QQ通信

文件大小:4KB

文件格式:ZIP

更新时间:2014-06-30 15:14:08

java类似QQ通信

服务器与客户端应用 package test; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import javax.swing.*; public class EchoServerTest extends JFrame implements Runnable{ JTextArea textArea; JTextArea textArea1; ServerSocket serverSocket; Socket socket; SenderButton sender; JButton button1; JButton button2; JButton button3; JScrollPane jScrollPane1; JPanel panel1; JPanel panel2; public EchoServerTest(){ super("服务器窗口"); this.setJMenuBar(createMenuBar()); button1=new JButton("发送"); button2=new JButton("保存"); button3=new JButton("退出"); jScrollPane1=new JScrollPane(); textArea=new JTextArea(5,20); textArea1=new JTextArea(1,20); textArea.setEditable(false); textArea.setForeground(Color.pink); jScrollPane1.add(textArea); jScrollPane1.setViewportView(textArea); panel1=new JPanel(new FlowLayout()); panel1.add(button1); panel1.add(button2); panel1.add(button3); panel2=new JPanel(new BorderLayout()); panel2.add("Center",textArea1); panel2.add("South",panel1); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add("Center",jScrollPane1); this.getContentPane().add("South",panel2); this.setVisible(true); this.pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { sender.GetString(textArea1.getText()); textArea.append("EchoServerTest:"+textArea1.getText()+"\n"); textArea1.setText(null); } }); button2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { try { JFileChooser fileChooser=new JFileChooser("C:\\"); fileChooser.setDialogTitle("请选择您要保存的路径"); if(fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){ File file=fileChooser.getSelectedFile(); } BufferedWriter b=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileChooser.getSelectedFile(),true))); b.write(textArea.getText()); b.flush(); b.close(); } catch (Exception e1) { e1.printStackTrace(); } } }); button3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); } }); Thread thread=new Thread(this); thread.start(); } public JMenuBar createMenuBar() { JMenuBar menuBar; JMenu menu, submenu; JMenuItem menuItem; JRadioButtonMenuItem rbMenuItem; JCheckBoxMenuItem cbMenuItem; menuBar = new JMenuBar(); menu = new JMenu("文件(F)"); menu.setMnemonic(KeyEvent.VK_F); menuBar.add(menu); submenu = new JMenu("新建(N)"); submenu.setMnemonic(KeyEvent.VK_N); menuItem = new JMenuItem("新建文件(D)"); submenu.add(menuItem); menuItem = new JMenuItem("复制当前页(C)"); submenu.add(menuItem); menu.add(submenu); menu.addSeparator(); menuItem = new JMenuItem("退出(x)"); menu.add(menuItem); menu = new JMenu("查看(V)"); menu.setMnemonic(KeyEvent.VK_V); menuBar.add(menu); submenu = new JMenu("工具栏"); cbMenuItem = new JCheckBoxMenuItem("菜单栏(M)"); cbMenuItem.setMnemonic(KeyEvent.VK_M); submenu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("地址栏(A)"); cbMenuItem.setMnemonic(KeyEvent.VK_A); submenu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("搜索栏(S)"); cbMenuItem.setMnemonic(KeyEvent.VK_S); submenu.add(cbMenuItem); menu.add(submenu); menu.addSeparator(); submenu=new JMenu("文字大小(X)"); submenu.setMnemonic(KeyEvent.VK_X); ButtonGroup group = new ButtonGroup(); rbMenuItem = new JRadioButtonMenuItem("大(L)"); rbMenuItem.setSelected(true); rbMenuItem.setMnemonic(KeyEvent.VK_L); group.add(rbMenuItem); submenu.add(rbMenuItem); rbMenuItem = new JRadioButtonMenuItem("中(M)"); rbMenuItem.setMnemonic(KeyEvent.VK_M); group.add(rbMenuItem); submenu.add(rbMenuItem); rbMenuItem = new JRadioButtonMenuItem("小(S)"); rbMenuItem.setMnemonic(KeyEvent.VK_S); group.add(rbMenuItem); submenu.add(rbMenuItem); menu.add(submenu); return menuBar; } public void run() { try { textArea.append(" 等待客户端连接"+"\n"); serverSocket = new ServerSocket(7658); socket=serverSocket.accept(); textArea.append("连接成功"+"\n"); BufferedReader in=new BufferedReader( new InputStreamReader(socket.getInputStream())); PrintWriter out=new PrintWriter( new OutputStreamWriter(socket.getOutputStream())); sender=new SenderButton(out); boolean done=false; while(true){ String str=in.readLine(); textArea.append("Client:"+str+"\n"); } } catch (Exception ex) { ex.printStackTrace(); } } public static void main(String[] args) { EchoServerTest test=new EchoServerTest(); } }


【文件预览】:
test
----EchoServerTest.java(5KB)
----SenderButton.java(579B)
----ClientTest.java(5KB)

网友评论

  • 能运行,挺好用。程序比较简单,初学者也能看懂,可以做为参考
  • 还行吧~~如果更完整点就好了
  • ExtJS入门教程(超级详细)
  • 还好,适合新手
  • 挺好,适合初学者学习研究
  • 没有解决问题。。。。
  • 过程蛮详细的,适合新手学习
  • 不错的资源,不过没有解决我的问题,想找个android下的