目录
**16.17(使用ScrollBar和Slider)
习题思路
示例代码
结果展示
**16.17(使用ScrollBar和Slider)
编写一个程序,使用滚动条或者滑动条选择文本的颜色,如图16-43所示。使用四个水平滚动条选择颜色(红色、绿色、和蓝色),以及透明度的百分比。
-
习题思路
- 创建一个HBox,用于放置文本。
- 创建一个GridPane,创建四个Label和四个水平滚动条,并依次添加到布局中。
- 创建一个BorderPane,将HBox设置在顶部,将GridPane设置在中心。
- 为四个水平滚动条注册事件监听器,当值滚动条的值改变时,分别获取四个滚动条的值(因为滚动条初始的值是0~100,需要除以100才能作为传入Color的参数),再以new Color(r, g, b, a)设置到text文本的颜色上
-
示例代码
编程练习题16_17UseScrollBarAndSlider.java
package chapter_16;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class 编程练习题16_17UseScrollBarAndSlider extends Application{
private ScrollBar sbRed = new ScrollBar();
private ScrollBar sbGreen = new ScrollBar();
private ScrollBar sbBlue = new ScrollBar();
private ScrollBar sbAlpha = new ScrollBar();
Text text = new Text("Show Colors");
@Override
public void start(Stage primaryStage) throws Exception {
HBox hBox = new HBox(text);
hBox.setAlignment(Pos.CENTER);
hBox.setPadding(new Insets(15, 15, 10, 10));
GridPane gridPane = new GridPane();
gridPane.setHgap(10);
gridPane.setAlignment(Pos.CENTER);
Label lbRed = new Label("Red ",sbRed);
lbRed.setContentDisplay(ContentDisplay.RIGHT);
lbRed.setGraphicTextGap(20);
Label lbGreen = new Label("Green",sbGreen);
lbGreen.setContentDisplay(ContentDisplay.RIGHT);
lbGreen.setGraphicTextGap(20);
Label lbBlue = new Label("Blue ",sbBlue);
lbBlue.setContentDisplay(ContentDisplay.RIGHT);
lbBlue.setGraphicTextGap(20);
Label lbAlpha = new Label("Alpha",sbAlpha);
sbAlpha.setValue(100);
lbAlpha.setContentDisplay(ContentDisplay.RIGHT);
lbAlpha.setGraphicTextGap(20);
gridPane.add(lbRed, 0, 0);
gridPane.add(lbGreen, 0, 1);
gridPane.add(lbBlue, 0, 2);
gridPane.add(lbAlpha, 0, 3);
gridPane.add(sbRed, 0, 0);
gridPane.add(sbGreen, 1, 1);
gridPane.add(sbBlue, 2, 2);
gridPane.add(sbAlpha, 3, 3);
BorderPane borderPane = new BorderPane();
borderPane.setTop(hBox);
borderPane.setCenter(gridPane);
sbRed.valueProperty().addListener(ov ->{
setColor();
});
sbGreen.valueProperty().addListener(ov ->{
setColor();
});
sbBlue.valueProperty().addListener(ov ->{
setColor();
});
sbAlpha.valueProperty().addListener(ov ->{
setColor();
});
Scene scene = new Scene(borderPane,250, 150);
primaryStage.setTitle("编程练习题16_17UseScrollBarAndSlider");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
public void setColor() {
double r = Double.valueOf(sbRed.getValue())/100.0;
double g = Double.valueOf(sbGreen.getValue())/100.0;
double b = Double.valueOf(sbBlue.getValue())/100.0;
double a = Double.valueOf(sbAlpha.getValue())/100.0;
System.out.println("r:"+r);
System.out.println("g:"+g);
System.out.println("b:"+b);
System.out.println("a:"+a);
text.setFill(new Color(r, g, b, a));
}
}
-
结果展示