JavaFX Button with transparent background

ⅰ亾dé卋堺 提交于 2019-12-06 07:32:23

问题


I have some classical Button in JavaFX with a box containing some text.

I need buttons without that box, just the text, and when I hover the button or click on the button with mouse, it shall change its color to different.


回答1:


In JavaFX styling is done by using CSS.

.button{
    -fx-border-color: transparent;
    -fx-border-width: 0;
    -fx-background-radius: 0;
    -fx-background-color: transparent;
    -fx-font-family:"Segoe UI", Helvetica, Arial, sans-serif;
    -fx-font-size: 1em; /* 12 */
    -fx-text-fill: #828282;
}

.button:focused {
    -fx-border-color: transparent, black;
    -fx-border-width: 1, 1;
    -fx-border-style: solid, segments(1, 2);
    -fx-border-radius: 0, 0;
    -fx-border-insets: 1 1 1 1, 0;
}

.button:pressed {
    -fx-background-color: black;
    -fx-text-fill: white;
}

Add this code to a CSS file, save it to the directory where the source file of the control exists which contains you buttons. Then in this class:

getStylesheets().add(getClass().getResource("nameofyourcssfile.css").toExternalForm());

Then all of the buttons that that object contain will use this style-classes.

Modification on your need is straightforward.

Good tutorial to start: http://docs.oracle.com/javafx/2/css_tutorial/jfxpub-css_tutorial.htm




回答2:


JavaFX has a Hyperlink control which basically has all the functionality you are looking for. It fires ActionEvents in the same way as a button:

Hyperlink button = new Hyperlink("Some text");
button.setOnAction(e -> System.out.println("Hyperlink clicked"));

Like a link in a web page, it will appear in a different color if it has been "visited", i.e. if an action has been fired on it.



来源:https://stackoverflow.com/questions/36566197/javafx-button-with-transparent-background

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