java 具有透明背景的 JavaFX 按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36566197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX Button with transparent background
提问by Tony Chuss
I have some classical Button
in JavaFX with a box containing some text.
我Button
在 JavaFX 中有一些经典内容,其中包含一个包含一些文本的框。
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.
我需要没有那个框的按钮,只需要文本,当我将按钮悬停或用鼠标单击按钮时,它的颜色会改变。
回答by DVarga
In JavaFX styling is done by using CSS.
在 JavaFX 中,样式是通过使用 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:
将此代码添加到 CSS 文件中,并将其保存到包含按钮的控件源文件所在的目录中。然后在这个类中:
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
很好的入门教程:http: //docs.oracle.com/javafx/2/css_tutorial/jfxpub-css_tutorial.htm
回答by James_D
JavaFX has a Hyperlink
controlwhich basically has all the functionality you are looking for. It fires ActionEvent
s in the same way as a button:
JavaFX 有一个Hyperlink
控件,它基本上具有您正在寻找的所有功能。它ActionEvent
以与按钮相同的方式触发s:
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.
就像网页中的链接一样,如果它被“访问”,即如果一个动作被触发,它将以不同的颜色显示。