2016-11-12 104 views
0

我製作了Button,我用Rectangle對它進行了成形並更改了背景顏色。JavaFX聚焦按鈕在線


源碼:
MainApplication.java:

AnchorPane anchor = new AnchorPane(); 
    anchor.maxWidth(325); 
    Button[] buttons = new Button[3]; 
    String[] buttonName = new String[]{"Player","World","Log"}; 
    for (int i = 0 ; i < 3 ; i ++){ 
     Button button = new Button(); 
     button.setText(buttonName[i]); 
     button.setShape(new Rectangle(100, 25)); 
     button.setMaxWidth(100); 
     button.setMinWidth(100); 
     button.setMaxHeight(25); 
     button.setMinHeight(25); 
     button.setId("tabButton"); 
     StackPane stack = new StackPane(); 
     stack.setMaxWidth(100); 
     stack.setMaxHeight(25); 
     stack.setMinWidth(100); 
     stack.setMinHeight(25); 
     RippingCircle circle = new RippingCircle();//just a rippleAnimation 
     circle.setToSize(Math.sqrt(100 * 100 + 25 * 25)); 
     circle.setFromSize(0.0); 
     circle.setFill(Color.rgb(0, 0, 0, 0.2)); 
     Rectangle clip = new Rectangle(-50, -12.5, 100, 25); 
     circle.setClip(clip); 
     stack.getChildren().addAll(button, circle); 
     stack.setLayoutX(100 * i); 
     stack.setLayoutY(0); 
     button.setOnMousePressed(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent mouseEvent){ 
       StackPane.setMargin(circle, new Insets((mouseEvent.getY() - 12.5) * 2, 0, 0, (mouseEvent.getX() - 50) * 2)); 
       clip.setX(-mouseEvent.getX()); 
       clip.setY(-mouseEvent.getY()); 
       circle.play(); 
      } 
     }); 
     button.setOnMouseReleased(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent mouseEvent){ 
       circle.stop(); 
      } 
     }); 
     anchor.getChildren().add(stack); 
     buttons[i] = button; 
    } 

MainApplication.css:

#tabButton { 
-fx-background-color: #03A9F4; 
-fx-text-fill: rgba(150,255,255,255); 
-fx-font: 12 Arial; 
-fx-focus-color: #FFEB3B; 
-fx-faint-focus-color: #FFEB3B; 
-fx-accent: #FFEB3B; 
} 
#tabButton:hover { 
    -fx-background-color: #039BE5; 
    -fx-text-fill: rgba(150,255,255,255); 
    -fx-font: 12 Arial; 
    -fx-focus-color: #FFEB3B; 
    -fx-faint-focus-color: #FFEB3B; 
    -fx-accent: #FFEB3B; 
} 
#tabButton:focused { 
    -fx-background-color: #03A9F4; 
    -fx-text-fill: rgba(255,255,255,255); 
    -fx-font: 12 Arial; 
    -fx-focus-color: black; 
    -fx-faint-focus-color: black; 
} 

當它聚焦,下劃線出現Player按鈕下:

enter image description here

這樣子。
這是什麼下劃線,以及如何改變它的顏色?

+0

順便說一句:除非你想覆蓋這個值,否則你不必重複'#tabButton'規則中設置的屬性。例如。除了'-fx-background-color:#039BE5;'之外的所有東西都不是你的第二條規則。此外,您的GUI可能會因修改懸停和聚焦的按鈕的外觀而受益,即指定'#tabButton:hover:focused'的規則。此外,如果多個節點的樣式相同,則通常對每個節點使用不同的樣式類。在MainApplication.css中爲 – fabian

回答

2

我認爲它屬於焦點,如果你想隱藏它:

-fx-background-insets:0; 

或改變顏色:

-fx-focus-color: red; 
-fx-faint-focus-color: red; 

祝你好運!

+0

,我已經改變顏色,但下劃線仍然是白色。 – Person