How to set Default CSS for all Buttons in JAVAFx

巧了我就是萌 提交于 2020-01-16 01:18:14

问题


I have over 100 Buttons in my JAVAfx application and I want to give a DEFAULT styling[given below] to all buttons in the programme. Please help ! :)

-fx-background-color:#3c7fb1;
-fx-text-fill: black;
-fx-font-size: 14px;
-fx-padding: 3 30 3 30;

回答1:


  1. Create a new CSS file.
  2. Attach the CSS file to your Scene.
  3. Put your button styles to .button {}.



回答2:


It's easy to set default Style for all JavaFX Button in an application. Just give a id to the style sheet which you want to set as default for all button.And then set this id for all button of your application.

Button button =new Button("Button");
Button button1 =new Button("Button");
button.setId("allbtn");
button1.setId("allbtn");
String  style= getClass().getResource("New.css").toExternalForm();
scene.getStylesheets().add(style);
  1. Create buttons
  2. apply id to them (for css as we apply in the html) using setId().
  3. Define CSS for this ID
  4. Finally add CSS file to the Scene Thats it.

And CSS file :

#allbtn{
-fx-color:black;
-fx-padding:4px;
-fx-background-color:#34c669;
-fx-background-radius: 10px;
}

Learn more about JavaFX Button CSS



来源:https://stackoverflow.com/questions/27668201/how-to-set-default-css-for-all-buttons-in-javafx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!