Lesson 03

包管理Package Management

使用包管理工具 共享代码

背景

当 供应方 更新代码,就需要重新下载,复制粘贴。

类似 当 QQ 出现新版本,就得重新下载,安装

希望有个 软件管家,自动检测版本更新,自动下载新版本的 jar

Maven

是什么

Java 下的 依赖管理 解决方案 之一

(Package / Module)Dependencies Management

优点

自动下载最新框架

记录当前程序对哪些组件有依赖

包管理软件

JavaMaven / Gradle / SBT
JavaScriptnpm / yarn
SwiftCocoaPod / Swift Package Manager
Pythonpip / conda

框架Frameworks

什么是

别人写好的一系列工具

类 函数 接口

心态

不是所有的代码都需要自己写

去找找 有没有现成写好的框架

学习 这些框架的 API

toString

substring

roll

Apache Common Cli

是什么

一套用于帮助解析 指令行工具的 类库

下载网址

案例

1. 新建项目

07_common_cli

2. maven

Add Framework Support

加 dependency

3. 写一个代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
public class Game { public static void main(String[] args) { Options options = new Options(); options.addOption("s", "style", true, "dice style"); CommandLineParser parser = new DefaultParser(); try { CommandLine commandLine = parser.parse(options, args); if (commandLine.hasOption("s")) { String style = commandLine.getOptionValue("s"); int styleInt = Integer.valueOf(style); System.out.println(Math.random() * styleInt); } } catch (ParseException e) { e.printStackTrace(); } }}
4. 配置运行

配置程序参数

-s 4
--style 6

客户端 UI 编程

Starter

1 2 3 4 5 6 7 8 9 10 11 12 13 14
public class App extends Application { @Override public void start(Stage primaryStage) { Pane pane = new Pane(); Scene scene = new Scene(pane, 800, 600); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { App.launch(args); }}

增加界面元素

3大步

1. 新建组件对象

2. 设置属性

3. 增加到父级容器里

样例代码
1 2 3 4 5 6 7 8+9 10 11+12+13 14 15+16 17 18 19 20 21 22 23 24
public class App extends Application { @Override public void start(Stage primaryStage) { Pane pane = new Pane(); Scene scene = new Scene(pane, 1000, 100); // step 1 Label label = new Label("LABEL"); // step 2 label.setLayoutX(100); label.setLayoutY(20); // step 3 pane.getChildren().add(label); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { App.launch(args); }}

事件响应

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
package com;public class App extends Application { @Override public void start(Stage primaryStage) { Pane pane = new Pane(); Scene scene = new Scene(pane, 1000, 100); ... Button button = new Button("点我啊"); button.setLayoutX(100); button.setLayoutY(80); button.setPrefWidth(100); pane.getChildren().add(button); button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { // 被点击时会执行 } }); ... primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { App.launch(args); }}

ZZAX 微信公众

文档一更新,立刻告诉你