带有关闭按钮的标签JavaFX
答
创建一个按钮,并触发其标签,以无形的:
Button moveBut = new Button("Hide Label");
moveBut.setOnAction(new EventHandler<actionevent>() {
@Override
public void handle(ActionEvent arg0) {
labelName.setVisible(false);
}
});
这里是一个Link它展示了如何使用BorderPane
布局隐藏/取消隐藏Labels
来自不同区域。
答
乔伊的回答工作,但请注意,您可以使用它作为标签的图形嵌入标签里面的按钮:
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);
}
}