我正在尝试使用css为文本字段添加默认外观,但它不能像我期望的那样工作。
我希望在文本字段中有一个右键,就像Win8的Metro主题中的一个
这是我正在使用的fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="300.0" prefWidth="400.0" stylesheets="@custom.css" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<children>
<TextField layoutX="126.0" layoutY="138.0" />
</children>
</AnchorPane>
这是css:
.text-field{
-fx-skin: "application.MetroTextFieldSkin";
}
以下是我正在使用的2个皮肤类:
第一个:
package application;
import com.sun.javafx.scene.control.skin.TextFieldSkin;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
/**
* Created by pedro_000 on 12/5/13.
*/
public class MetroTextFieldSkin extends TextFieldWithButtonSkin{
public MetroTextFieldSkin(TextField textField) {
super(textField);
}
protected void rightButtonPressed()
{
getSkinnable().setText("");
}
}
第二个:
package application;
import com.sun.javafx.scene.control.behavior.TextFieldBehavior;
import com.sun.javafx.scene.control.skin.TextFieldSkin;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.control.Skin;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
public class TextFieldWithButtonSkin extends TextFieldSkin{
private StackPane rightButton;
private Region rightButtonGraphic;
protected TextField textField;
public TextFieldWithButtonSkin(TextField textField) {
super(textField);
this.textField = textField;
rightButton = new StackPane();
rightButton.getStyleClass().setAll("right-button");
rightButton.setFocusTraversable(false);
rightButtonGraphic = new Region();
rightButtonGraphic.getStyleClass().setAll("right-button-graphic");
rightButtonGraphic.setFocusTraversable(false);
rightButtonGraphic.setMaxWidth(Region.USE_PREF_SIZE);
rightButtonGraphic.setMaxHeight(Region.USE_PREF_SIZE);
rightButton.setVisible(false);
rightButtonGraphic.setVisible(false);
rightButton.getChildren().add(rightButtonGraphic);
getChildren().add(rightButton);
setupListeners();
}
private void setupListeners() {
final TextField textField = getSkinnable();
rightButton.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
rightButtonPressed();
}
});
rightButton.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
rightButtonReleased();
}
});
textField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
textChanged();
}
});
textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
focusChanged();
}
});
}
protected void textChanged()
{
if (textField.getText() == null)
return;
System.out.println(textField.getText().isEmpty());
rightButton.setVisible(!textField.getText().isEmpty());
rightButtonGraphic.setVisible(!textField.getText().isEmpty());
}
protected void focusChanged()
{
if (textField.getText() == null)
return;
rightButton.setVisible(textField.isFocused() && !textField.getText().isEmpty());
rightButtonGraphic.setVisible(textField.isFocused() && !textField.getText().isEmpty());
}
@Override
protected void layoutChildren(double x, double y, double w, double h) {
super.layoutChildren(x, y, w, h);
final double clearGraphicWidth = snapSize(rightButtonGraphic.prefWidth(-1));
final double clearButtonWidth = rightButton.snappedLeftInset() + clearGraphicWidth + rightButton.snappedRightInset();
rightButton.resize(clearButtonWidth, h);
positionInArea(rightButton,
(x+w) - clearButtonWidth, y,
clearButtonWidth, h, 0, HPos.CENTER, VPos.CENTER);
}
protected void rightButtonPressed()
{
}
protected void rightButtonReleased()
{
}
}
我错过了使其正常工作所必需的东西 这些是由pedro为他的主题jMetro创造的。