【问题标题】:Javafx: Getting rid of Slider marginJavafx:摆脱滑块边距
【发布时间】:2015-09-03 20:55:13
【问题描述】:

我有一个“滑块进度条混合”,如the answer to this question 中所述(带有值绑定的进度条顶部的不可见滑块),我的问题是滑块的左右边距很小,所以当滑块拇指放在滑块末端附近时,进度条会偏离几个像素。我尝试将滑块和轨道以及Slider#setPadding() 上的-fx-background-insets-fx-background-radius-fx-padding 设置为零,但均无济于事。

如何消除滑块轨道两侧的间隙?

到目前为止,这是我的代码:

StackPane seekSliderStack = new StackPane();

ProgressBar seekProgressBar = new ProgressBar();
seekProgressBar.setMaxWidth(Double.MAX_VALUE);

Slider seekSlider = new Slider();
seekSlider.setMaxWidth(Double.MAX_VALUE);
seekSlider.valueProperty().addListener(new ChangeListener<Number>() {
    public void changed(ObservableValue<? extends Number> ov,
            Number oldValue, Number newValue) {
    seekProgressBar.setProgress(newValue.doubleValue() / 100);
    }
});
seekSlider.setValue(30);

//seekSlider.setPadding(Insets.EMPTY); // doesn't change anything

seekSliderStack.getChildren().addAll(seekProgressBar, seekSlider);

还有 css 样式表:

.slider {
    -fx-background-insets: 0; 
    -fx-background-radius: 0;
    -fx-padding: 0;
    -fx-border-insets: 0;
}

.slider .thumb {
    -fx-background-color: null;
}

.slider .track {
    -fx-background-color: null;
    -fx-pref-height: 20px;
    -fx-background-insets: 0; 
    -fx-background-radius: 0;
    -fx-padding: 0;
    -fx-border-insets: 0;
}

.progress-bar {
    -fx-background-color: null;
    -fx-background-insets: 0; 
    -fx-background-radius: 0;
}

.progress-bar .track{
    -fx-background-color: null;
    -fx-background-insets: 0;
    -fx-background-radius: 0;
}

.progress-bar .bar { 
    -fx-background-color: blue;
    -fx-background-insets: 0; 
    -fx-background-radius: 0;
}

最后是显示间隙的小图像(白色条是滑块轨道,红色轮廓属于滑块,蓝色部分是绿色进度条轨道上的进度条,将滑块高度更改为进度条的一半并制作滑块轨道可用于演示):

【问题讨论】:

    标签: java css user-interface javafx slider


    【解决方案1】:

    哇,在发布问题几分钟后就找到了我的问题的解决方案:

    将滑块拇指上的-fx-pref-width 设置为零会使这些间隙消失,这对我有用,因为无论如何我都隐藏了拇指。

    我想确实是谈论(或者在这种情况下:写作)一个问题更容易解决它。

    【讨论】:

      猜你喜欢
      • 2016-11-29
      • 2016-11-09
      • 2013-07-24
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多