簡體   English   中英

帶關閉按鈕的標簽JavaFX

[英]Label with a close Button JavaFX

是否可以使用關閉按鈕創建Label(不可編輯的文本字段)?

單擊按鈕后,標簽應消失。

創建一個按鈕並觸發要使其不可見的標簽:

Button moveBut = new Button("Hide Label"); 
moveBut.setOnAction(new EventHandler<actionevent>() {

 @Override 
 public void handle(ActionEvent arg0) {  

     labelName.setVisible(false);

                                      } 

   });

這是一個鏈接 ,顯示了如何使用BorderPane布局從不同區域隱藏/取消隱藏Labels

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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM