2016-11-12 107 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