2016-04-04 8 views
3

enter image description hereJavaFXボーダータイトル

上記の画像は私のコードの結果です。しかし、私は以下のようにしたいと思います。
enter image description here

どのように修正できますか?以下は私のコードです。私はあまりにも多くのソースを読んでいますが、あまりにも複雑です。たとえば、a source that I read、私はこの方法は非常に複雑だと思う。おそらく、この問題を解決する簡単な方法があります。

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.VBox; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class Test extends Application { 

    public BorderPane border = new BorderPane(); 

    public Label name = new Label("Name"); 
    public Label surname = new Label("Surname"); 

    public TextField name1 = new TextField(); 
    public TextField surname1 = new TextField(); 

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

    } 

    @Override 
    public void start(Stage arg0) throws Exception { 
     Scene scene = new Scene(new VBox(), 300, 200); 
     arg0.setTitle("Header"); 
     arg0.setResizable(false); 
     scene.setFill(Color.OLDLACE); 

     StackPane grid = addStackPane(); 
     border.setMargin(grid, new Insets(12,12,12,12)); 
     border.setCenter(grid); 

     ((VBox) scene.getRoot()).getChildren().add(border); 
     arg0.setScene(scene); 
     arg0.show(); 

    } 

    @SuppressWarnings("static-access") 
    public StackPane addStackPane() { 
     StackPane pane = new StackPane(); 
     GridPane grid = new GridPane(); 
     Label title = new Label("Border Title"); 
     title.setStyle("-fx-translate-y: -7"); 
     pane.setAlignment(title, Pos.TOP_LEFT); 
     grid.setStyle("-fx-content-display: top"); 
     grid.setStyle("-fx-border-insets: 20 15 15 15"); 
     grid.setStyle("-fx-background-color: white"); 
     grid.setStyle("-fx-border-color: black"); 
     grid.setHgap(10); 
     grid.setVgap(10); 
     grid.setPadding(new Insets(25, 10, 25, 10)); 
     grid.add(name, 1, 0); 
     grid.add(name1, 2, 0); 

     grid.add(surname, 1, 1); 
     grid.add(surname1, 2, 1); 
     pane.getChildren().addAll(grid, title); 

     return pane; 
    } 

} 

このトピックを読んでいただきありがとうございます。

答えて

4

タイトルラベルの-fx-background-colorをborderPaneの背景と同じ色に設定してください。それはあなたがそれらを連結しない限り、setStyle()を介して複数のスタイルを設定することはできませんので、そして、あなたはCSSファイルで設定されていることを確認します

myComponent.setStyle("-fx-text-fill: white;"+ 
    "-fx-background-color: black;"); 

をさらに、常により高い優先順位を持つようInlineStyleSheetsを使用する悪い習慣ですルールはCSSスタイルシートで指定されています。

(あなたがStackPane.setAlignment(title, Pos.TOP_LEFT)pane.setAlignment(title, Pos.TOP_LEFT)を変更する場合は、「静電気のアクセスも」警告を削除することができます。)

+0

ありがとうございました。このコメントは私にとって非常に役に立ちます。 @jns – ZpCikTi