對話框的封裝使用
前言
登錄需要彈出登錄對話框,但是,Jfoenix庫使用對話框比較難受,還得動態去生成布局,我想起了Android的對話框生成,便是封裝了一個,一行代碼即可生成
使用
使用的話,直接一行代碼即可 ,下面的幾種常用的情況!
- 只有一個確定按鈕,按下esc可以退出
//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();
- 輸出路徑對話框
點擊打開資源管理器,並定位當該目錄
new DialogBuilder(tfOutPath).setTitle("提示")
.setMessage("已完成,輸出目錄為")
.setHyperLink("Q:\\MyBlog")
.setNegativeBtn("確定").create();
- 網頁鏈接對話框
點擊打開默認瀏覽器,跳轉到該網址
new DialogBuilder(tfOutPath).setTitle("提示")
.setMessage("已完成,輸出目錄為")
.setHyperLink("www.cnblogs.com/kexing")
.setNegativeBtn("確定").create();
- 提供一個輸入框
new DialogBuilder(btnLogin).setTitle("提示").setMessage("輸入").setTextFieldText(new DialogBuilder.OnInputListener() {
@Override
public void onGetText(String result) {
//返回一個輸入結果result
//相關的邏輯操作
}
}).setPositiveBtn("確定").setNegativeBtn("取消").create();
后期有空再更新,更新常用的對話框布局
代碼
后期更新新功能不會修改這里的代碼了,需要的請去我的github項目地址查看
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 java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javafx.scene.control.Control;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.layout.Border;
import javafx.scene.layout.HBox;
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 Hyperlink hyperlink = null;
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;
}
public DialogBuilder setHyperLink(String text) {
hyperlink = new Hyperlink(text);
hyperlink.setBorder(Border.EMPTY);
hyperlink.setOnMouseClicked(event -> {
if (text.contains("www") || text.contains("com") || text.contains(".")) {
try {
Desktop.getDesktop().browse(new URI(text));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
} else if (text.contains(File.separator)) {
try {
Desktop.getDesktop().open(new File(text));
} catch (IOException e) {
e.printStackTrace();
}
}
});
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));
//添加hyperlink超鏈接文本
if (hyperlink != null) {
layout.setBody(new HBox(new Label(this.message),hyperlink));
} else {
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();
}
}