Initial project
This commit is contained in:
138
src/main/java/com/imyeyu/fx/ui/components/LabelProgressBar.java
Normal file
138
src/main/java/com/imyeyu/fx/ui/components/LabelProgressBar.java
Normal file
@@ -0,0 +1,138 @@
|
||||
package com.imyeyu.fx.ui.components;
|
||||
|
||||
import com.imyeyu.java.bean.CallbackArgReturn;
|
||||
import javafx.beans.property.DoubleProperty;
|
||||
import javafx.beans.property.StringProperty;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ProgressBar;
|
||||
import javafx.scene.layout.StackPane;
|
||||
|
||||
/**
|
||||
* 标签进度。如果继承进度组件使用反射注入标签组件时,在设置标签文本会导致标签消失
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2022-08-09 10:48
|
||||
*/
|
||||
public class LabelProgressBar extends StackPane {
|
||||
|
||||
private static final String STYLE_CLASS = "label-progress-bar";
|
||||
|
||||
/** 标签 */
|
||||
protected final Label label;
|
||||
|
||||
/** 进度 */
|
||||
protected final ProgressBar bar;
|
||||
|
||||
/** 标签转换 */
|
||||
protected CallbackArgReturn<Double, String> converter;
|
||||
|
||||
/** 默认构造 */
|
||||
public LabelProgressBar() {
|
||||
label = new Label();
|
||||
bar = new ProgressBar();
|
||||
bar.prefWidthProperty().bind(widthProperty());
|
||||
bar.getStyleClass().add(STYLE_CLASS);
|
||||
|
||||
getChildren().addAll(bar, label);
|
||||
|
||||
bar.progressProperty().addListener((obs, o, p) -> {
|
||||
if (converter != null) {
|
||||
if (p == null) {
|
||||
label.setText(converter.handler(-1D));
|
||||
} else {
|
||||
label.setText(converter.handler(p.doubleValue()));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前标签文本
|
||||
*
|
||||
* @return 标签文本
|
||||
*/
|
||||
public String getText() {
|
||||
return label.getText();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置标签文本
|
||||
*
|
||||
* @param text 标签文本
|
||||
*/
|
||||
public void setText(String text) {
|
||||
label.setText(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标签文本监听
|
||||
*
|
||||
* @return 标签文本监听
|
||||
*/
|
||||
public StringProperty textProperty() {
|
||||
return label.textProperty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标签组件
|
||||
*
|
||||
* @return 标签组件
|
||||
*/
|
||||
public Label getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置进度值
|
||||
*
|
||||
* @param progress 进度值,取值范围 [0, 1]
|
||||
*/
|
||||
public void setProgress(double progress) {
|
||||
bar.setProgress(progress);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取进度值
|
||||
*
|
||||
* @return 进度值
|
||||
*/
|
||||
public double getProgress() {
|
||||
return bar.getProgress();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取进度监听
|
||||
*
|
||||
* @return 进度监听
|
||||
*/
|
||||
public DoubleProperty progressProperty() {
|
||||
return bar.progressProperty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取进度组件
|
||||
*
|
||||
* @return 进度组件
|
||||
*/
|
||||
public ProgressBar getBar() {
|
||||
return bar;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标签转换回调
|
||||
*
|
||||
* @return 转换回调
|
||||
*/
|
||||
public CallbackArgReturn<Double, String> getConverter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置标签转换回调,进度变换时触发回调
|
||||
*
|
||||
* @param converter 标签转换回调
|
||||
*/
|
||||
public void setConverter(CallbackArgReturn<Double, String> converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user