登陸須要彈出登陸對話框,可是,Jfoenix庫使用對話框比較難受,還得動態去生成佈局,我想起了Android的對話框生成,即是封裝了一個,一行代碼便可生成git
使用的話,直接一行代碼便可 ,下面的幾種經常使用的狀況!github
//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();
後期有空再更新,更新經常使用的對話框佈局瀏覽器
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(); } }