JavaFX按钮有透明背景 [英] JavaFX Button with transparent background
问题描述
我在JavaFX中有一些经典的按钮
,其中包含一些包含文本的框。
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.
推荐答案
在JavaFX中,使用CSS完成样式设计。
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;
}
将此代码添加到CSS文件中,将其保存到源文件所在的目录中存在包含按钮的控件文件。然后在这个类中:
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.
开始的好教程:
http://docs.oracle.com/javafx/2/css_tutorial/jfxpub-css_tutorial.htm
这篇关于JavaFX按钮有透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!