https://mozilla.github.io/rhino/ 下载 rhino1_7R5.zip , 解压后运行 cmd
cd D:\rhino\rhino1_7R5
java -jar js.jar openfile.js
class="java" name="code">
// 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);
//import java.awt.Desktop;
importClass(java.net.URI);
importClass(java.io.File);
importClass(java.lang.Thread);
with (swingNames) {
var frame = new JFrame("Open file"); // The application window
frame.setLocation(200,200);
var txtfield = new JTextField(30); // text entry field
var button = new JButton("打开"); // Button to start download
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
// Put them all together and display the GUI
row.add(txtfield); // Input field goes in the row
row.add(button); // Button goes in the row
col.add(row); // Row goes in the column
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
button.addActionListener(function() {
try {
var fname = txtfield.text;
if( fname==""){
var response = filechooser.showSaveDialog(frame);
// Quit now if they clicked Cancel
if (response != JFileChooser.APPROVE_OPTION) return;
// Otherwise, get the java.io.File that represents the destination file
var file = filechooser.getSelectedFile();
txtfield.setText(file);
}
if (Desktop.isDesktopSupported()) { // 判断系统是否提供了对该类的支持
var desktop = Desktop.getDesktop();// 获得该类的对象
fname = txtfield.text;
if (fname.substring(0,7)=="http://"){
if (desktop.isSupported(Desktop.Action.BROWSE))
desktop.browse(new URI(fname)); // 浏览网站
}
else if (desktop.isSupported(Desktop.Action.OPEN))
desktop.open(new File(fname));// 打开
}
} catch(e) {
// Display a dialog box if anything goes wrong
JOptionPane.showMessageDialog(frame, e.message, "Exception",
JOptionPane.ERROR_MESSAGE);
}
});
}
运行 rhino.bat openfile.js