前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >atom-shell小例子

atom-shell小例子

作者头像
jeremyxu
发布2018-05-09 18:04:52
1.1K0
发布2018-05-09 18:04:52
举报

今天一个朋友问我一个问题,他想做一个win32的桌面应用程序,而且还希望程序能做出web页面那种漂亮的效果,可目前项目组的成员全是以前做前端的一批人,怎么办?

我想了一下,几乎毫不犹豫地推荐了node-webkit, 但又想起前段时间看到的atom-shell,于是也推荐了下atom-shell,随手写了个atom-shell的例子给他。

package.json

代码语言:javascript
复制
{
  "name"    : "pingdemo",
  "version" : "0.1.0",
  "main"    : "main.js"
}

main.js

代码语言:javascript
复制
var app = require('app');  // Module to control application life.
var BrowserWindow = require('browser-window');  // Module to create native browser window.

// Report crashes to our server.
require('crash-reporter').start();

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the javascript object is GCed.
var mainWindow = null;

// Quit when all windows are closed.
app.on('window-all-closed', function() {
  if (process.platform != 'darwin')
    app.quit();
});

// This method will be called when atom-shell has done everything
// initialization and ready for creating browser windows.
app.on('ready', function() {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 800, height: 600});

  // and load the index.html of the app.
  mainWindow.loadUrl('file://' + __dirname + '/index.html');

  // Emitted when the window is closed.
  mainWindow.on('closed', function() {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null;
  });
});

index.html

代码语言:javascript
复制
<!DOCTYPE html>
<html>
  <head>
    <title>Ping Demo</title>
  </head>
  <body>
    <h1>Ping Demo</h1>
    <input id="addr" type="text" name="addr" value="www.baidu.com"/>
    <input id="ping" type="button" name="ping" value="Ping"/>
    <div id="ping-result"></div>
    <script type="text/javascript" src="ping.js"></script>
  </body>
</html>

ping.js

代码语言:javascript
复制
try {
  var child_process = require('child_process');
  window.$ = window.jQuery = require('./jquery');
} catch(e) {console.log(e)}

$('#ping').on('click', function (){
  var opts = {
    encoding: 'utf8',
    timeout: 2000,
    maxBuffer: 200*1024,
    killSignal: 'SIGKILL',
    cwd: null,
    env: null
  };
  var cmd = 'ping -t 1 -c 1 ' + $('#addr').val();
  var child = child_process.exec(cmd, opts, function(error, stdout, stderr) {
    $('#ping-result').empty();
    if(stdout.length > 0){
      $('#ping-result').append('<pre>' + stdout + '</pre>');
    }
    if(stderr.length > 0){
      $('#ping-result').append('<pre>' + stderr + '</pre>');
    }
  });
});

jquery.js从code.jquery.com/jquery-1.11.0.min.js下载过来重命名即可

上述所有文件放在一个PingDemo目录中

最后执行

1

/Applications/Atom.app/Contents/MacOS/Atom ./PingDemo/

想想java调用外部程序还要考虑那么多东西,这东西真心简单啊。

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-07-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com