简体   繁体   English

(JavaFX 8)CSS按钮边框和背景色问题

[英](JavaFX 8) css button border and background color issue

I'm trying to create a Fallout 4 like button style via css with simple background-color and border properties and actually it works well. 我正在尝试通过具有简单背景颜色和边框属性的css创建一个Fallout 4像按钮样式,实际上效果很好。 Only problem is that the border doesn't cover the whole button. 唯一的问题是边框不能覆盖整个按钮。 There's a piece of the background sticking out at the bottom of the button. 按钮的底部有一块背景。 (see screenshot 1) (请参见屏幕截图1)

屏幕截图1

When I click on the button and keep the mouse pressed it disappears, though not completely. 当我单击按钮并按住鼠标时,它消失了,尽管没有完全消失。 (see screenshot 2) (请参见屏幕截图2)

屏幕截图2

Here's the part of my css: 这是我的CSS的一部分:

.button {
    -fx-background-color:transparent ;
    -fx-background-radius:0;
    -fx-border-color:transparent;
    -fx-border-width: 0 3 3 0;
}

.button:hover {
    -fx-background-color:lime;
    -fx-background-radius:0;
    -fx-border-color:black;
    -fx-border-width: 0 3 3 0;
}

Any idea what could cause this? 知道是什么原因造成的吗?

In order to reproduce the problem you will need more that one Button nodes on the Scene. 为了重现该问题,场景中将需要多个“ Button”节点。 The visual effect is caused due to background insets when the button is not focus. 当按钮未聚焦时,视觉效果是由背景插图引起的。 To avoid that add -fx-background-insets: 0; 为了避免这种情况,请添加-fx-background-insets: 0; on the .button:hover CSS rule and the problem will be fixed. .button:hover CSS规则上,此问题将得到解决。

.button:hover {
    -fx-background-color:lime;
    -fx-background-radius:0;
    -fx-border-color:black;
    -fx-border-width: 0 3 3 0;
    -fx-background-insets: 0;
}

Here's a simple program you wanted using the following css. 这是您想要使用以下CSS的简单程序。 Though the problem can't really be reproduced with this. 尽管此问题不能真正重现。

public class test extends Application
{
    public static void main(String[] args) 
    {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception 
    {
        BorderPane pane = new BorderPane();
        pane.getStylesheets().add(
                getClass().getResource("test.css").toExternalForm()
        );

        Button test = new Button("Back");
        test.setPrefWidth(120);
        pane.setCenter(test);

        Scene scene = new Scene(pane, 150, 150);
        primaryStage.setScene(scene);
        primaryStage.setTitle("csstest");
        primaryStage.show();
    }
}

test.css test.css

@font-face{
    src: url('roboto.ttf');
}

.root {
    -fx-background-color:black;
}

.text {
    -fx-font-family: "Roboto Condensed";
    -fx-fill: lime;
    -fx-font-size: 20;
}

.button {
    -fx-background-color:transparent ;
    -fx-background-radius:0;
    -fx-border-color:transparent;
    -fx-border-width: 0 3 3 0;
}

.button:hover {
    -fx-background-color:lime;
    -fx-background-radius:0;
    -fx-border-color:black;
    -fx-border-width: 0 3 3 0;
}

.button:hover .text{
    -fx-fill: black;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM