现在需要做一个桌面应用,心里有点不甘,因为想做出一个简单的客户端,你要么使用Java的Swing编程,要么会使用MFC等等,这样学习的代价太高,也不便维护,于是了解了一下Electron,Electron提供了丰富的本地(操作系统)的API,使你能够使用纯JavaScript来创建桌面应用程序。与其它各种的Node.js运行时不同的是Electron专注于桌面应用程序而不是Web服务器,这并不意味着Electron是一个绑定图形用户界面(GUI)的JavaScript库,取而代之的是,Electron使用Web页面作为它的图形界面,所以你也可以将它看作是一个由JavaScript控制的迷你的Chrominum浏览器,像钉订客户端就是使用这种方式开发的,但是是使用的nw.js,我想要去学习和掌握的是Electron,ATOM就是这样造出来的。

去官网下载并解压打开electron弹出如下界面

这样最简单的就给你安装好了electron的环境了,现在就可以开始写一个桌面程序,然后拖入它打开就好了,当然也可以去按照官网的说明使用npm下载

现在来书写第一个应用hello world

通常,一个Electron应用的结构类似下面:

your-app/
├── package.json
├── main.js
└── index.html

package.json 的格式与Node的模块格式是一致的,其中 main 字段指定的脚本就是你应用的启动脚本,该脚本将运行在主进程中。你的 package.json 也许看上去像下面这个例子:

{
"name" : "your-app",
"version" : "0.1.0",
"main" : "main.js"
}

注意 如果在 package.json 中的 main 字段没有指定,那么Electron将尝试装载一个名为 index.js 的脚本。

main.js 应当创建窗口并且处理系统事件,一个典型的例子如下:

const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow // Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow function createWindow () {
// 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`) // Open the DevTools.
//mainWindow.webContents.openDevTools() // 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
})
} // This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow) // Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
}) app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
}) // In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

最后 index.html 则是你想要展示在窗口中:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using node <script>document.write(process.versions.node)</script>,
Chrome <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
</body>
</html>

然后拖入electron运行就好了

Electron(一)--初步了解并动手HelloWorld的更多相关文章

  1. nodejs的桌面应用(electron)

    最近发现nodejs可以做桌面应用,主要是之前的同事在搞,我也要稍微研究下不能落后啊,基于nodejs的桌面应用,常用的就是nw.js和electron,nw出的比较早,资料比较多,bug也很多,它的 ...

  2. 【作业一】Android开发环境以及开发前的准备

    对于Android平台的开发工具,我知道的就是Eclipse和Android Studio(后面简称AS).之前在学习JAVA时,渐渐习惯了Eclipse,后来要搭建Android的开发环境时,本来也 ...

  3. 动手写一个简单的Web框架(HelloWorld的实现)

    动手写一个简单的Web框架(HelloWorld的实现) 关于python的wsgi问题可以看这篇博客 我就不具体阐述了,简单来说,wsgi标准需要我们提供一个可以被调用的python程序,可以实函数 ...

  4. Linux下Electron的Helloworld

    什么是Electron Electron 框架的前身是 Atom Shell,可以让你写使用 JavaScript,HTML 和 CSS 构建跨平台的桌面应用程序.它是基于io.js 和 Chromi ...

  5. struts2学习笔记--动手搭建环境+第一个helloworld项目

    在Myeclipse中已经内置好了struts2的环境,但是为了更好的理解,这里自己从头搭建一下: 前期准备:下载struts2的完整包,下载地址:https://struts.apache.org/ ...

  6. SpringMVC初步——HelloWorld的实现

    开通博客园好几个月了,今天开始要用博客园记录自己的学习过程! 目录: 导包: 1. 配置web.xml文件的springDispatcherServlet 在xml中 alt+/ 找到springdi ...

  7. Lucene 初步 之 HelloWorld

    万恶的源头 HelloWorld 要完成lucene 的配置 需要几个jar包 (如果需要可以留言我私发) 创建索引API分析: 1. Directory: 类代表一个Lucene索引的位置,FSDi ...

  8. 1、Electron入门HelloWorld案例

    一.Electron是什么? 官网:https://electronjs.org/ Electron是由Github开发,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一个开源库. ...

  9. C#动手实践:Kinect V2 开发(1):初步了解及环境搭建

    该分享使用的是Kinect 二代,C#做为编程语言,请知悉 Kinect是微软在2009年6月2日的E3游戏展上,正式公布的XBOX360体感周边外设.它是一种3D体感摄影机,同时它导入了即时动态捕捉 ...

随机推荐

  1. ios开发——实用技术篇Swift篇&多点触摸与手势识别

    多点触摸与手势识别 //点击事件 var atap = UITapGestureRecognizer(target: self, action: "tapDo:") self.vi ...

  2. lvs持久性工作原理和配置

    作者: ISADBA|FH.CN 日期: // 转载请注明: 作者:ISADBA.COM|FH.CN BLOG:http://isadba.com 原文:http://isadba.com/?p=67 ...

  3. Exploring Python Code Objects

    Exploring Python Code Objects https://late.am/post/2012/03/26/exploring-python-code-objects.html Ins ...

  4. javascript 实现htmlEncode htmlDecode

    屌屌的写法..function htmlEncode(value){ //create a in-memory div, set it's inner text(which jQuery automa ...

  5. "jobTracker is not yet running"(hadoop 配置)

    今天自己尝试做配置了一下hadoop,环境是ubuntu13.10+jdk1.7.0_51+hadoop version1.2.1. 主要过程主要参考http://blog.csdn.net/hitw ...

  6. 基础知识 - Rabin-Karp 算法

    Rabin-Karp 算法(字符串快速查找) Go 语言的 strings 包(strings.go)中用到了 Rabin-Karp 算法.Rabin-Karp 算法是基于这样的思路:即把字符串看作是 ...

  7. APP测试基本流程

    一. 测试周期 测试周期一般为两周,根据项目情况以及版本质量可适当缩短或延长测试时间.正式测试前先向主管或产品经理确认项目排期. 二.测试资源 测试任务开始前,检查各项测试资源. 产品功能需求文档 产 ...

  8. Tomcat配置随笔

    启动内存参数的配置 tomcat/bin/catalina.bat 如果是linux 就是 catalina.sh 在rem 的后面增加如下参数 set JAVA_OPTS= -Xms256m -Xm ...

  9. linux-进程相关

    ps查看当前运行的进程 ps -ef|grep httpd ps是静态的 ps -aux|grep httpd top 是动态的 kill 杀进程  kill -[选项] 进程号 -9强制杀死 kil ...

  10. autoscan; aclocal; autoconf; automake --add-missing; ./configure; make

    1.autoscan 在源码目录下执行autoscan,生成configure.scan,重命名为configure.in或者configure.ac,然后编辑文件内容: ============== ...