java : mozilla rhino chat 客户端_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java : mozilla rhino chat 客户端

java : mozilla rhino chat 客户端

 2018/8/25 15:35:30  belldeep  程序员俱乐部  我要评论(0)
  • 摘要:https://mozilla.github.io/rhino/下载rhino1_7R5.zip,解压后运行cmdcdD:\rhino\rhino1_7R5编写rhino.bat@echooffjava-cpD:/rhino/rhino1_7R5/js.jar;.org.mozilla.javascript.tools.shell.Main%*写了一个ChatClient测试用例char
  • 标签:Java 客户 客户端 Mozilla
https://mozilla.github.io/rhino/ 下载 rhino1_7R5.zip , 解压后运行 cmd
cd D:\rhino\rhino1_7R5
编写 rhino.bat
class="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("");
});
// 连接
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
上一篇: java : chat 客户端 下一篇: 没有下一篇了!
发表评论
用户名: 匿名