java : mozilla rhino chat 客户端
程序员文章站
2022-04-28 23:07:49
...
https://mozilla.github.io/rhino/ 下载 rhino1_7R5.zip , 解压后运行 cmd
cd D:\rhino\rhino1_7R5
编写 rhino.bat
写了一个 Chat Client 测试用例 char.js
运行 rhino.bat char.js
cd D:\rhino\rhino1_7R5
编写 rhino.bat
@echo off java -cp D:/rhino/rhino1_7R5/js.jar;. org.mozilla.javascript.tools.shell.Main %*
写了一个 Chat Client 测试用例 char.js
// Import the Swing GUI components and a few other classes var swingNames = new JavaImporter(javax.swing, javax.swing.event, javax.swing.border, java.awt,java.awt.event); importPackage(java.net); importPackage(java.io); importPackage(java.util); importClass(java.lang.Thread); with (swingNames) { var frame = new JFrame("Chat Client"); // The application window //frame.setSize(600,400); frame.setLocation(200,200); var txtfield = new JTextField(30); // txt entry field var button1 = new JButton("发送"); // Button to send message var button2= new JButton("Clear"); var filechooser = new JFileChooser(); // A file selection dialog var row = Box.createHorizontalBox(); // A box for field and button var col = Box.createVerticalBox(); // For the row & progress bars var padding = new EmptyBorder(3,3,3,3); // Padding for rows var texta = new JTextArea(10,30); texta.setEditable(false); // texta.setLineWrap(true); // Put them all together and display the GUIm row.add(txtfield); // Input field goes in the row row.add(button1); // Button goes in the row row.add(button2); col.add(row); // Row goes in the column col.add(texta); frame.add(col); // Column goes in the frame row.setBorder(padding); // Add some padding to the row frame.pack(); // Set to minimum size frame.visible = true; // Make the window visible frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // When the user clicks the button, call this function button1.addActionListener(function() { var str = txtfield.getText(); if ( str.trim().length() >1){ texta.append(str+"\n"); new java.lang.Thread(function(){ connect("127.0.0.1",12345,str);}).start(); txtfield.setText(""); } else { texta.append("Input length error\n"); } }); // Clear button2.addActionListener(function() { texta.setText(""); }); // KeyEvent: ENTER txtfield.addActionListener(function() { var str = txtfield.getText(); texta.append(str+"\n"); txtfield.setText(""); }); // 连接 function connect(host,port,msg){ try { var socket = new java.net.Socket(host,port); var message = msg.trim(); var writer = new java.io.PrintWriter(socket.getOutputStream(),true); writer.println(message); try { var ins = new java.util.Scanner(socket.getInputStream()); while( ins.hasNextLine()){ texta.append(ins.nextLine()+"\n"); } } finally { socket.close(); } } catch(e){ texta.append(e.toString()); } } }
运行 rhino.bat char.js