前言:
今天看官们对“java弹框”大体比较关心,看官们都想要学习一些“java弹框”的相关资讯。那么小编同时在网摘上网罗了一些关于“java弹框””的相关内容,希望大家能喜欢,咱们快快来了解一下吧!JavaFX默认的PasswordField控件不能动态切换输入内容是否可见,无法满足实际需求。这里通过自定义JavaFX控件,实现如下的效果:
隐藏密码时
显示密码时
实现思路1.自定义组件,继承StackPane2.内部使用一个TextField和一个PasswordField,分别充当密码可见时和不可见时展示的控件,默认显示PasswordField3.分别给TextField和PasseordField设置样式,主要是设置眼睛图片作为background图片4.分别监听TextField和PasseordField的鼠标移动事件,当鼠标移动到眼睛图片附近时,设置鼠标指针的样式为手形5.分别监听TextField和PasseordField的鼠标点击事件,当点击的位置在眼睛图片附近时,切换要显示的控件6.将TextField和PasswordField的textProperty属性进行双向绑定7.定义一个用于获取输入内容的方法代码
public class FXPasswordField extends StackPane { private final TextField textField; private final PasswordField passwordField; public FXPasswordField() { super(); textField = new TextField(); passwordField = new PasswordField(); initStyle(); initEvent(); textField.textProperty().bindBidirectional(passwordField.textProperty()); getChildren().addAll(textField, passwordField); } private void initStyle() { String style = "-fx-padding: 5px 30px 5px 5px; -fx-background-repeat: no-repeat; -fx-background-position: right 10 center; -fx-background-size: contain;"; String textFieldStyle = style + "-fx-background-image: url(/assets/image/eye-open.png);"; String passwordFieldStyle = style + "-fx-background-image: url(/assets/image/eye-close.png);"; textField.setStyle(textFieldStyle); passwordField.setStyle(passwordFieldStyle); setOnMouseMovedEvent(textField, textFieldStyle); setOnMouseMovedEvent(passwordField, passwordFieldStyle); } private void setOnMouseMovedEvent(TextField textField, String style){ textField.setOnMouseMoved((MouseEvent event) -> { double width = textField.getWidth(); if(event.getX() > width - 30){ textField.setStyle(style + "-fx-cursor: hand;"); }else{ textField.setStyle(style + "-fx-cursor: text;"); } }); } private void initEvent(){ setOnMouseClickedEvent(textField, passwordField); setOnMouseClickedEvent(passwordField, textField); } private void setOnMouseClickedEvent(TextField willHideInput, TextField willShowInput){ willHideInput.setOnMouseClicked((MouseEvent event) -> { double width = textField.getWidth(); if(event.getX() > width - 30){ // 移除当前显示的输入框, 显示另一个输入框 setNodeVisible(willHideInput, false); setNodeVisible(willShowInput, true); } }); } private void setNodeVisible(Node node, boolean visible){ node.setVisible(visible); node.setManaged(visible); } public String getText(){ return textField.getText(); }}使用
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #java弹框 #javafx 输入框弹窗 #java输入文本框 #java点击输入框文字消失出现