标签:桌面 package oid 提示 listen void system 操作 des
登录需要弹出登录对话框,但是,Jfoenix库使用对话框比较难受,还得动态去生成布局,我想起了Android的对话框生成,便是封装了一个,一行代码即可生成
使用的话,直接一行代码即可 ,下面的几种常用的情况!
//tfOutPath是一个控件(controller)
new DialogBuilder(tfOutPath).setTitle("提示").setMessage("登录成功").setNegativeBtn("确定").create();
OnClickListener
监听器负责执行点击按钮后执行的操作new DialogBuilder(tfOutPath).setNegativeBtn("取消", new DialogBuilder.OnClickListener() {
@Override
public void onClick() {
//点击取消按钮之后执行的动作
}
}).setPositiveBtn("确定", new DialogBuilder.OnClickListener() {
@Override
public void onClick() {
//点击确定按钮之后执行的动作
}
}).setTitle("提示").setMessage("hello world").create();
new DialogBuilder(startBtn).setTitle("提示").setMessage("hello world").setPositiveBtn("确定", "#ff3333").setNegativeBtn("取消", "#00ff00").create();
后期有空再更新,更新常用的对话框布局
package wan.Utils;
import com.jfoenix.controls.JFXAlert;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialogLayout;
import com.sun.istack.internal.Nullable;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
/**
* @author StarsOne
* @date Create in 2019/6/2 0002 20:51
* @description
*/
public class DialogBuilder {
private String title, message;
private JFXButton negativeBtn = null;
private JFXButton positiveBtn = null;
private Window window;
private JFXDialogLayout jfxDialogLayout = new JFXDialogLayout();
private Paint negativeBtnPaint = Paint.valueOf("#747474");//否定按钮文字颜色,默认灰色
private Paint positiveBtnPaint = Paint.valueOf("#0099ff");
private JFXAlert<String> alert;
/**
* 构造方法
*
* @param control 任意一个控件
*/
public DialogBuilder(Control control) {
window = control.getScene().getWindow();
}
public DialogBuilder setTitle(String title) {
this.title = title;
return this;
}
public DialogBuilder setMessage(String message) {
this.message = message;
return this;
}
public DialogBuilder setNegativeBtn(String negativeBtnText) {
return setNegativeBtn(negativeBtnText, null, null);
}
/**
* 设置否定按钮文字和文字颜色
*
* @param negativeBtnText 文字
* @param color 文字颜色 十六进制 #fafafa
* @return
*/
public DialogBuilder setNegativeBtn(String negativeBtnText, String color) {
return setNegativeBtn(negativeBtnText, null, color);
}
/**
* 设置按钮文字和按钮文字颜色,按钮监听器和
*
* @param negativeBtnText
* @param negativeBtnOnclickListener
* @param color 文字颜色 十六进制 #fafafa
* @return
*/
public DialogBuilder setNegativeBtn(String negativeBtnText, @Nullable OnClickListener negativeBtnOnclickListener, String color) {
if (color != null) {
this.negativeBtnPaint = Paint.valueOf(color);
}
return setNegativeBtn(negativeBtnText, negativeBtnOnclickListener);
}
/**
* 设置按钮文字和点击监听器
*
* @param negativeBtnText 按钮文字
* @param negativeBtnOnclickListener 点击监听器
* @return
*/
public DialogBuilder setNegativeBtn(String negativeBtnText, @Nullable OnClickListener negativeBtnOnclickListener) {
negativeBtn = new JFXButton(negativeBtnText);
negativeBtn.setCancelButton(true);
negativeBtn.setTextFill(negativeBtnPaint);
negativeBtn.setButtonType(JFXButton.ButtonType.FLAT);
negativeBtn.setOnAction(addEvent -> {
alert.hideWithAnimation();
if (negativeBtnOnclickListener != null) {
negativeBtnOnclickListener.onClick();
}
});
return this;
}
/**
* 设置按钮文字和颜色
*
* @param positiveBtnText 文字
* @param color 颜色 十六进制 #fafafa
* @return
*/
public DialogBuilder setPositiveBtn(String positiveBtnText, String color) {
return setPositiveBtn(positiveBtnText, null, color);
}
/**
* 设置按钮文字,颜色和点击监听器
*
* @param positiveBtnText 文字
* @param positiveBtnOnclickListener 点击监听器
* @param color 颜色 十六进制 #fafafa
* @return
*/
public DialogBuilder setPositiveBtn(String positiveBtnText, @Nullable OnClickListener positiveBtnOnclickListener, String color) {
this.positiveBtnPaint = Paint.valueOf(color);
return setPositiveBtn(positiveBtnText, positiveBtnOnclickListener);
}
/**
* 设置按钮文字和监听器
*
* @param positiveBtnText 文字
* @param positiveBtnOnclickListener 点击监听器
* @return
*/
public DialogBuilder setPositiveBtn(String positiveBtnText, @Nullable OnClickListener positiveBtnOnclickListener) {
positiveBtn = new JFXButton(positiveBtnText);
positiveBtn.setDefaultButton(true);
positiveBtn.setTextFill(positiveBtnPaint);
System.out.println("执行setPostiveBtn");
positiveBtn.setOnAction(closeEvent -> {
alert.hideWithAnimation();
if (positiveBtnOnclickListener != null) {
positiveBtnOnclickListener.onClick();//回调onClick方法
}
});
return this;
}
/**
* 创建对话框并显示
*
* @return JFXAlert<String>
*/
public JFXAlert<String> create() {
alert = new JFXAlert<>((Stage) (window));
alert.initModality(Modality.APPLICATION_MODAL);
alert.setOverlayClose(false);
JFXDialogLayout layout = new JFXDialogLayout();
layout.setHeading(new Label(title));
layout.setBody(new VBox(new Label(this.message)));
if (negativeBtn != null && positiveBtn != null) {
layout.setActions(negativeBtn,positiveBtn);
}else {
if (negativeBtn != null) {
layout.setActions(negativeBtn);
} else if (positiveBtn != null) {
layout.setActions(positiveBtn);
}
}
alert.setContent(layout);
alert.showAndWait();
return alert;
}
public interface OnClickListener {
void onClick();
}
}
Java开发桌面程序学习(三)——基于Jfoenix库的JFXDialog封装仿Android对话框的工具DialogBuilder
标签:桌面 package oid 提示 listen void system 操作 des
原文地址:https://www.cnblogs.com/kexing/p/10989323.html