2014-09-03 136 views

回答

1

創建一個按鈕,並觸發其標籤,以無形的:

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

@Override 
public void handle(ActionEvent arg0) { 

    labelName.setVisible(false); 

             } 

    }); 

這裏是一個Link它展示瞭如何使用BorderPane佈局隱藏/取消隱藏Labels來自不同區域。

1

喬伊的回答工作,但請注意,您可以使用它作爲標籤的圖形嵌入標籤裏面的按鈕:

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