是否可以创建带有关闭按钮的标签(不可编辑的文本字段)?
单击按钮时,标签应该消失。
最佳答案
Joey 的答案有效,但请注意,您可以通过将按钮用作标签的图形来将按钮嵌入标签内:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class LabelWithCloseButton extends Application {
@Override
public void start(Stage primaryStage) {
Button closeButton = new Button("X");
// In real life, use an external style sheet rather than inline styles:
// I did it this way for brevity
closeButton.setStyle("-fx-font-size: 6pt; -fx-text-fill:red;");
Label label = new Label("Click the button to close");
label.setGraphic(closeButton);
label.setContentDisplay(ContentDisplay.RIGHT);
HBox root = new HBox(label);
closeButton.setOnAction(event -> root.getChildren().remove(label));
Scene scene = new Scene(root, 250, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
关于java - 带有关闭按钮 JavaFX 的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25650913/