开发工具:
文件大小: 4kb
下载次数: 0
上传时间: 2011-06-10
详细说明: 服务器与客户端应用 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 p anel1; 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(); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.