最近需要使用javafx+java实现一个win的桌面应用,学习了一下javafx,写的顺序可能有点乱,希望别介意,在这里做一个记录
1、JavaFX Scene Builder 2.0
极力推荐实现这个软件,这是一个编写Fxml文件的可视化软件,可以和idea无缝连接使用。
2、认识Fxml文件
Fxml文件对于我来说就是静态页面,文件结构和xml文件一样,但是标签都是对应javafx的,这个可以不用自己写,直接使用上面说的软件生成
3、按钮
每个按钮对应一个功能,在xml文件里面,Button标签就是对应按钮,可以设置大小,颜色,和网页前端一样,但是有两个特殊属性
fx:id属性,这是给按钮设置一个id,然后java通过@FXML注解映射,关联到按钮
onAction属性,设置将按钮关联到一个方法,当点击按钮时执行这个关联的方法,也需要@FXML注解
<!-- 我这里使用的ToggleButton 标签,因为我后面需要放入同一个组里面,设置点击时变换颜色-->
<ToggleButton fx:id="addbutton" maxWidth="Infinity" minHeight="24.0" minWidth="51.0" mnemonicParsing="false" onAction="#addbutton" style="-fx-background-color: #A9A9A9;" text="添加" textFill="WHITE" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets left="15.0" right="10.0" />
</HBox.margin>
</ToggleButton>
@FXML
protected ToggleButton addbutton;
@FXML
void addbutton(ActionEvent event) {
// 业务代码就不拿出来了
}
下面是给按钮添加一些属性,设置
// 给按钮添加图标
// 将按钮和图表路径分别放入两个数组中
Labeled[] labeleds = new Labeled[]{addbutton};
String[] imagePaths = new String[]{"/com/images/button_ addto.png"};
// 调用方法,设置图标
new SimpleTools().setLabeledImage(labeleds, imagePaths);
public class SimpleTools {
public void setLabeledImage(Labeled[] labeleds, String[] imagePaths) {
for (int i = 0; i < labeleds.length; i++) {
labeleds[i].setGraphic(new ImageView(new Image(imagePaths[i])));
}
}
}
给按钮设置选中时颜色变化
// 将按钮添加到一个组里面管理
ToggleGroup group = new ToggleGroup();
addbutton.setToggleGroup(group);
deletebutton.setToggleGroup(group);
// 设置颜色变化
group.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
if (newValue instanceof ToggleButton) {
System.out.println("你选择了 " + ((ToggleButton) newValue).getText());
try{
// 新选中的按钮颜色
((ToggleButton) newValue).setStyle("-fx-background-color: #0078FC");
// 取消选中按钮颜色
((ToggleButton) oldValue).setStyle("-fx-background-color: #A9A9A9");
}catch (NullPointerException e){
}
}
});
给按钮设置快捷键,具体需要设置什么快捷键,需要去看KeyCode和KeyCombination两个的文档
public void shortcutKey(Scene scene) {
// 设置添加按钮的快捷键 ctrl+[
KeyCodeCombination combination = new KeyCodeCombination(KeyCode.OPEN_BRACKET, KeyCombination.SHORTCUT_DOWN);
scene.getAccelerators().put(combination, new Runnable() {
@Override
public void run() {
addbutton.fire();
}
});
}
4、后面就想起来什么写什么
窗口
// 监听窗口大小变化
stage.heightProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println(newValue);
}
});
// 加载界面的fxml文件
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/com/anylangtech/leading/particulars.fxml"));
AnchorPane root = null;
try {
root = loader.load();
} catch (Exception e) {
e.printStackTrace();
}
Stage stage = new Stage();
Scene scene = new Scene(root);
// 设置左上角窗口名字
stage.setTitle("我的窗口");
stage.setScene(scene);
// 窗口左上角图标
stage.getIcons().add(new Image("/com/images/fylogo.png"));
ParticularsController controller = (ParticularsController) loader.getController();
// 可自由拉伸窗口
stage.setResizable(true);
//禁用·1最大化
primaryStage.setResizable(false);
// 展示舞台
stage.show();
// 监听窗口关闭时,关闭程序
stage.setOnCloseRequest(event -> {
System.out.println("关闭窗口");
// 关闭程序
System.exit(0);
});
目前先写这些,后面再补