如何使用JavaScript UI控件,构建Electron应用程序[通俗易懂]

如何使用JavaScript UI控件,构建Electron应用程序[通俗易懂]如何使用JavaScript UI控件,构建Electron应用程序

大家好,又见面了,我是你们的朋友全栈君。

概述

What is Electron?
Electron是一个使用JavaScript、HTML和CSS构建跨平台桌面应用程序的框架。 您可以将Electron与纯JavaScript或您选择的JavaScript框架一起使用:

构建一个简单的Electron应用程序

要创建基本的Electron应用程序,请按照下列步骤操作:

git clone https://github.com/electron/electron-quick-start
cd electron-quick-start
npm install
npm start

您应该看到如下所示的Hello World应用程序:

Electron apps in JavaScript

将JavaScript UI控件(WijmoJS)添加到应用程序

要将WijmoJS添加到应用程序,请先安装它。在命令提示符下,进入app文件夹(electron-quick-start)并键入:

npm install Wijmo

接下来,使用VS Code或您喜好的编辑器打开index.html文件,并添加以下内容:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>

    <!-- add Bootstrap and Wijmo css -->
    <link href=https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css
          rel="stylesheet"/>
    <link href=https://cdn.grapecity.com/wijmo/5.latest/styles/wijmo.min.css
          rel="stylesheet"/>

    <!-- define the app styles -->
    <style>
      .app-panel {
        margin: 0 48pt;
      }
      .app-panel .wj-control {
        display: inline-block;
        vertical-align: top;
        width: 400px;
        height: 300px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h1>Hello World!</h1>
      <p>
        <!-- Node.js APIs are available in this renderer process. -->
        We are using Node.js
        <script>document.write(process.versions.node)</script>,
        Chromium <script>document.write(process.versions.chrome)</script>,
        and Electron
        <script>document.write(process.versions.electron)</script>.
      </p>

      <!--add Wijmo controls to the page -->
      <div class="app-panel">
        <div id="theGrid"></div>
        <div id="theChart"></div>
      </div>
    </div>
    <script>
      // You can also require other files to run in this process
      require('./renderer.js')
    </script>
  </body>
</html>

在这一步中,我们为两个WijmoJS控件添加了一些样式和主题元素。接下来,打开“renderer.js”文件并按如下所示进行编辑:

// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.

// import Wijmo
var wjCore = require('./node_modules/wijmo/wijmo.js');
var wjGrid = require('./node_modules/wijmo/wijmo.grid.js');
var wjChart = require('./node_modules/wijmo/wijmo.chart.js');

// set the Wijmo license key
var key = 'GrapeCity-Internal-Use-Only,…';
wjCore.setLicenseKey(key);

// create the controls
var theGrid = new wjGrid.FlexGrid('#theGrid', {
    itemsSource: getData()
});
var theChart = new wjChart.FlexChart('#theChart', {
    itemsSource: theGrid.itemsSource,
    bindingX: 'country',
    series: [
        { name: 'Sales', binding: 'sales' },
        { name: 'Expenses', binding: 'expenses' },
        { name: 'Downloads', binding: 'downloads' },
    ]
});

// get some random data
function getData() {
    var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','),
        data = [];
    for (var i = 0; i < countries.length; i++) {
        data.push({
        country: countries[i],
        sales: Math.random() * 10000,
        expenses: Math.random() * 5000,
        downloads: Math.round(Math.random() * 20000),
        });
    }
    return new wjCore.CollectionView(data);
}

实现这段代码首先需要三个WijmoJS模块:WijmoJS Core,Grid和Chart。 (它设置了WijmoJS许可证密钥,因此应用程序在运行时不会显示水印。如果您没有许可证密钥,请跳过此步骤,应用程序仍将运行,但会显示水印元素)

如果您在此之前已经安装了许可证密钥,则不需要特定域。WijmoJS电子应用程序会从文件或本地主机协议运行,因此任何有效的WijmoJS密钥都将起作用,无论用于生成它的域是什么。

最后一步是创建WijmoJS控件并将它们绑定到数据源。 在此示例中,网格和图表绑定到同一数据源。

运行Electron应用程序

像以前一样运行应用程序!

npm start

这次你会看到这个:

Electron apps in JavaScript

由于表格和图表绑定到相同的数据,因此您对网格所做的任何更改(如编辑单元格或排序列)都将自动应用于图表。

现在,请下载WijmoJS,享用WijmoJS JavaScript控件的Electron应用程序吧。


WijmoJS:灵活高效的前端开发工具包,可快速搭建企业 Web 应用程序
图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107220.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java 多线程:彻底搞懂线程池

    熟悉Java多线程编程的同学都知道,当我们线程创建过多时,容易引发内存溢出,因此我们就有必要使用线程池的技术了。

    2022年4月8日
  • python3.9多线程_Python进阶

    python3.9多线程_Python进阶什么是线程?线程也叫轻量级进程,是操作系统能够进行运算调度的最小单位,它被包涵在进程之中,是进程中的实际运作单位。线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其

    2022年7月31日
  • Lucene教程具体解释

    Lucene教程具体解释

    2021年12月10日
  • SpringBoot基础之MockMvc单元测试

    SpringBoot基础之MockMvc单元测试SpringBoot创建的Maven项目中,会默认添加spring-boot-starter-test依赖。在《5分钟快速上手SpringBoot》中编写的单元测试使用了MockMvc。本篇文章就围绕MockMvc在SpringBoot中的使用进行讲解。什么是Mock在面向对象的程序设计中,模拟对象(英语:mockobject)是以可控的方式模拟真实对象行为的假对象。在编程过程中,通常通过模…

    2022年10月22日
  • busybox rootfs 与 ubuntu rootfs_boot.img root

    busybox rootfs 与 ubuntu rootfs_boot.img root下载busybox的源码,解压后,设定ARCH和CROSS_COMPILE的两个基本环境变量,选择defconfig作为默认配置,大部分的busybox工具都会被编译出来。如果不指定输出目录,默认输出到根目录的_install目录下面,如果需要指定目录,配置CONFIG_PREFIX=/a/b/c/rootfs,这样make生成的/bin,/sbin,/usr三个默认文件夹就直接在rootfs目录下。makeARCH=armCROSS_COMPILE=arm…

    2022年10月6日
  • 详解单调栈算法

    详解单调栈算法前言如果你对这篇文章可感兴趣,可以点击「【访客必读-指引页】一文囊括主页内所有高质量博客」,查看完整博客分类与对应链接。栈属于基础数据结构之一,基础到仅用「后进先出」这四个字即可完整概括其核心特征。然而,基础并不代表着简单,「后进先出」的背后反而隐藏着多样的变化与极其广泛的应用。在本篇文章中,我们将针对在基础栈上稍加改动所形成的「单调栈」算法进行详解。该算法与「单调队列」组成了算法题中最常考察的线性数据结构,属于面试中必知必会的算法知识。栈首先我们来回忆一下「栈」。「栈」是一种「后进先出」的线

    2022年9月23日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号