java : mozilla rhino js 打开文件_JAVA_编程开发_程序员俱乐部

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

java : mozilla rhino js 打开文件

 2018/8/31 0:47:35  belldeep  程序员俱乐部  我要评论(0)
  • 摘要:https://mozilla.github.io/rhino/下载rhino1_7R5.zip,解压后运行cmdcdD:\rhino\rhino1_7R5java-jarjs.jaropenfile.js//ImporttheSwingGUIcomponentsandafewotherclassesvarswingNames=newJavaImporter(javax.swing,javax.swing.event,javax.swing.border,java.awt,java.awt
  • 标签:文件 Java Mozilla JS
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
上一篇: 远程调用其他主机sh脚本工具 下一篇: 没有下一篇了!
发表评论
用户名: 匿名