java : mozilla rhino chat 客户端

https://mozilla.github.io/rhino/ 下载 rhino1_7R5.zip , 解压后运行 cmd
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值