javafx是Sun公司在2008年发布的一款GUI编程框架,可跨平台,支持 Windows XP,Windows Vista,Windows 7/8/10 ,Mac OS X和Linux操作系统,支持css美化窗体,十分强大。
javaFx窗体的构建使用fxml文件描述,idea支持可视化拖拽构建窗体
请看下面这个fxml文件:
<AnchorPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="152.0" prefWidth="389.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.aline.ptrscr.Controller">
<children>
<Pane prefHeight="152.0" prefWidth="389.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<CheckBox fx:id="autoCbx" layoutX="14.0" layoutY="19.0" mnemonicParsing="false" text="启用自动保存" />
<Label alignment="CENTER" layoutX="6.0" layoutY="47.0" prefHeight="25.0" prefWidth="85.0" text="保存路径:" />
<Label fx:id="savePath" layoutX="79.0" layoutY="48.0" prefHeight="23.0" prefWidth="269.0" />
<ComboBox fx:id="types" layoutX="79.0" layoutY="113.0" prefHeight="23.0" prefWidth="150.0" />
<Label layoutX="17.0" layoutY="117.0" text="保存格式:" />
<Button fx:id="saveBtn" layoutX="303.0" layoutY="112.0" mnemonicParsing="false" onAction="#saveConf" prefHeight="25.0" prefWidth="60.0" text="保存" />
<Button fx:id="fcBtn" layoutX="350.0" layoutY="48.0" mnemonicParsing="false" onAction="#chooseFile" prefHeight="23.0" prefWidth="25.0" text="..." />
<Label layoutX="29.0" layoutY="86.0" text="文件名:" />
<ComboBox fx:id="fileNameRule" layoutX="79.0" layoutY="82.0" prefHeight="23.0" prefWidth="150.0" />
<Label layoutX="129.0" layoutY="20.0" text="[Alt+A]截图" textFill="#e32983" />
<Label layoutX="214.0" layoutY="20.0" text="[Alt+S]隐藏" textFill="#e32983" />
<Label layoutX="288.0" layoutY="20.0" text="[Alt+D]显示" textFill="#e32983" />
children>
Pane>
children>
AnchorPane>
对其中一些标签解释一下:
AnchorPane中的 fx:controller属性指向一个java类,该类可以定义组件的数据加载和事件捕捉处理
CheckBox 、Label 、ComboBox …这些标签都是组件,其中 :
id 是在Controller对应的java类中变量的名称(变量要加上@FXML注解)
onAction 是用户点击按钮触发事件在controller中对应的方法(方法要加上@FXML注解)
看一下Controller中的处理:
package com.aline.ptrscr;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import java.io.File;
public class Controller {
@FXML
private Button saveBtn;
@FXML
private ComboBox<String> types;
@FXML
private CheckBox autoCbx;
@FXML
private Label savePath;
@FXML
private ComboBox<String> fileNameRule;
@FXML
private Button fcBtn;
private ObservableList<String> comItems = FXCollections.observableArrayList();
private ObservableList<String> fileNameRuleItem = FXCollections.observableArrayList();
public void init() {
comItems.addAll(".png", ".jpeg", ".jpg");
fileNameRuleItem.addAll(Config.DATETIME_NAME, Config.RANDM_NAME);
types.setItems(comItems);
fileNameRule.setItems(fileNameRuleItem);
boolean autoSave = Config.isAutoSave();
autoCbx.setSelected(autoSave);
if (autoSave) {
changeDisable(!autoSave);
types.getSelectionModel().select(Config.getType());
fileNameRule.getSelectionModel().select(Config.getFileNameRule());
savePath.setText(Config.getSavePath());
} else {
types.getSelectionModel().select(0);
fileNameRule.getSelectionModel().select(0);
changeDisable(true);
}
autoCbx.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
changeDisable(oldValue);
}
});
}
public void changeDisable(boolean disable) {
types.setDisable(disable);
fileNameRule.setDisable(disable);
fcBtn.setDisable(disable);
}
@FXML
public void chooseFile(ActionEvent event) {
DirectoryChooser directoryChooser = new DirectoryChooser();
directoryChooser.setTitle("请选择默认保存路径");
File file = directoryChooser.showDialog(new Stage());
if (file == null)
return;
String path = file.getPath();
savePath.setText(path);
Config.setSavePath(path);
}
@FXML
public void saveConf(ActionEvent event) {
Config.setAutoSave(Boolean.valueOf(autoCbx.isSelected()));
Config.setFileNameRule(fileNameRule.getSelectionModel().getSelectedItem());
Config.setSavePath(savePath.getText());
Config.saveToFile();
}
}
解释一下@FXML注解
@FXML注解可用于属性和方法,用于属性时该属性名称对应fxml文件中的某个组件的id,用于方法时该方法名称对应fxml中一个组件的onAction属性#号后面的内容,用于触发事件
看一下启动类:
package com.aline.ptrscr;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
// 启动类需要继承javafx.application.Application这个抽象类,并重写start方法
public class Starter extends Application {
// fxml文件中指定的Controller类
private static Controller ui;
private static Parent root;
public static void main(String[] args) {
// 此处没有开启线程,launch方法后面的代码不会执行
launch(args);
System.out.println("start ...");
}
@Override
public void start(Stage primaryStage) throws IOException {
// 从classpath加载fxml文件
URL location = App.class.getClassLoader().getResource("app.fxml");
// 构造FXML加载器
FXMLLoader loader = new FXMLLoader(location);
// 获取根元素
root = loader.load();
// 获取controller对象
ui = loader.getController();
// 调用controller自定义初始化方法
ui.init();
// 显示窗体
primaryStage.show();
}
}