2017-07-29 79 views

回答

0

您可能想看看使用TextArea的background財產。

 new TextArea().setBackground(new Background(new BackgroundImage(myImage,BackgroundRepeat.NO_REPEAT,BackgroundRepeat.NO_REPEAT,BackgroundPosition.CENTER,BackgroundSize.DEFAULT))); 

此處的代碼假設您可以將這些行作爲圖像。

你可以找到背景這裏更多的信息:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Background.html

如果您希望背景圖像變化,是動態的,您目前的需求,你有兩個選擇。

  1. 只需在整個項目中使用Canvas即可。首先將線條繪製到畫布上,然後在其上繪製字母。這可能會更好,因爲它可以讓你自定義你的項目,但是你需要更多的代碼和想法。

  2. 使用TextArea,併爲BackgroundImage使用另一個畫布的快照。您可以使用畫布根據需要繪製線條,然後使用Snapshot將其轉換爲圖像。

WritableImage i = canvas.snapshot(new SnapshotParameters(), null);

然後,使用這張圖片,你可以將其用作文本區的使用BackgroundImage背景。

+0

我覺得這是一個非常好的創新想法,一定會嘗試一下。但是我需要這些線條是動態的,因爲它們需要改變顏色,當用戶點擊某些按鈕時,它們之間的寬度需要動態改變。我可以通過代碼創建圖像,以使圖像反映這些更改。 –

+0

@IndronilChaudhuri是的!我添加了一些關於如何使背景圖像變爲動態的信息。 – Jaboyc

0

考慮在Pane像這樣畫線:

public class StageTest extends Application{ 

    private static final double WIDTH = 100, HEIGHT = 60; 

    @Override 
    public void start(Stage stage) throws Exception { 

     stage.setTitle("Test Stage"); 
     Label label = new Label("Some text "); 
     label.setStyle("-fx-background-color:TRANSPARENT"); 
     label.setAlignment(Pos.CENTER); 
     label.setPrefSize(WIDTH, HEIGHT); 

     Pane linesPane = getPane(label); 
     StackPane root = new StackPane(linesPane, label); 

     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    private Pane getPane(Label label) { 

     Pane pane = new Pane(); 
     pane.setStyle("-fx-background-color:WHITE"); 
     Line blueLine = new Line(); 
     blueLine.setStroke(Color.BLUE); 
     blueLine.startXProperty().bind(label.layoutXProperty()); 
     blueLine.startYProperty().bind(label.layoutYProperty().add(label.heightProperty().multiply(.333))); 
     blueLine.endXProperty().bind(label.layoutXProperty().add(label.widthProperty())); 
     blueLine.endYProperty().bind(label.layoutYProperty().add(label.heightProperty().multiply(.333))); 

     Line redLine = new Line(); 
     redLine.setStroke(Color.RED); 
     redLine.startXProperty().bind(label.layoutXProperty()); 
     redLine.startYProperty().bind(label.layoutYProperty().add(label.heightProperty().multiply(.666))); 
     redLine.endXProperty().bind(label.layoutXProperty().add(label.widthProperty())); 
     redLine.endYProperty().bind(label.layoutYProperty().add(label.heightProperty().multiply(.666))); 

     pane.getChildren().addAll(blueLine, redLine); 

     return pane; 
    } 


    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

我還需要在文本上輸入文字,並且只要我現在在javafx中的窗格上鍵入文本是不可能的。糾正我,如果我錯了。 –

+0

這是可能的。您可以在窗格上繪製文字,或者更好,如本例中使用「Label」這樣的組件。 「標籤」可以添加到「窗格」中。 – c0der