java : mozilla rhino js 打开文件
程序员文章站
2022-04-28 23:07:31
...
https://mozilla.github.io/rhino/ 下载 rhino1_7R5.zip , 解压后运行 cmd
cd D:\rhino\rhino1_7R5
java -jar js.jar openfile.js
运行 rhino.bat openfile.js
彩蛋: mozilla rhino js引擎 使用教程
https://blog.csdn.net/belldeep/article/details/81837171
cd D:\rhino\rhino1_7R5
java -jar js.jar openfile.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); //import java.awt.Desktop; importClass(java.net.URI); importClass(java.io.File); importClass(java.lang.Thread); with (swingNames) { var frame = new JFrame("Open URI or file"); // The application window frame.setLocation(200,200); var txtfield = new JTextField(30); // text entry field txtfield.setText("http://"); 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 var label = new JLabel("this is test"); // 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 col.add(label); 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); // KeyEvent: ENTER txtfield.addActionListener(function() { try { if (Desktop.isDesktopSupported()) { // 判断系统是否提供了对该类的支持 var desktop = Desktop.getDesktop();// 获得该类的对象 var fname = txtfield.text; if (fname.startsWith("http://") || fname.startsWith("https://")){ if (desktop.isSupported(Desktop.Action.BROWSE)) desktop.browse(new URI(fname)); // 浏览网站 } label.setText(fname); } else { label.setText("isDesktopSupported==false"); } txtfield.setText("http://"); } catch(ex) { // Display a dialog box if anything goes wrong JOptionPane.showMessageDialog(frame, ex.message, "Exception", JOptionPane.ERROR_MESSAGE); } }); // When the user clicks the button, call this function button.addActionListener(function() { try { var fname = txtfield.text; if (fname=="" || fname=="http://"){ 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();// 获得该类的对象 var fname = txtfield.text; if (fname.startsWith("http://") || fname.startsWith("https://")){ if (desktop.isSupported(Desktop.Action.BROWSE)) desktop.browse(new URI(fname)); // 浏览网站 } else if (desktop.isSupported(Desktop.Action.OPEN)) desktop.open(new File(fname));// 打开 label.setText(fname); } else { label.setText("isDesktopSupported==false"); } txtfield.setText(""); } catch(e) { // Display a dialog box if anything goes wrong JOptionPane.showMessageDialog(frame, e.message, "Exception", JOptionPane.ERROR_MESSAGE); } }); }
运行 rhino.bat openfile.js
彩蛋: mozilla rhino js引擎 使用教程
https://blog.csdn.net/belldeep/article/details/81837171
上一篇: javascript MVC框架学习笔记
下一篇: 81.Java异常概述