创建一个名为ColorDialog的选择颜色的自定义对话框,使用三个滑块来指定一种颜色的红、绿、蓝成分。自定义对话框允许用户使用滑块来选择颜色。 然后把选定的颜色用于改变第1个窗口的颜色。
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.Group;
public class ColorDialog extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("ColorDialog");
primaryStage.setResizable(false);//窗口大小不可变
Group group = new Group();
Scene scene = new Scene(group, 400, 500, Color.GRAY);//初始窗口颜色
Button btnChange = new Button("Change Button Text Color");
btnChange.setLayoutX(70);
btnChange.setLayoutY(220);
btnChange.setPrefSize(260, 60);
btnChange.setFont(Font.font(18));
group.getChildren().add(btnChange);
//监听颜色改变按钮
btnChange.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
//把场景和舞台传过去,在第二个窗口中修改按钮的颜色,让舞台显示
ShowChangeWin(primaryStage, scene,btnChange);
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
void ShowChangeWin(Stage primaryStage, Scene priScene, Button btnChange) {
/*
* 弹出第二个窗口
* */
Stage choiceStage = new Stage();
choiceStage.setTitle("ColorDialog");
choiceStage.setResizable(false); //设置窗口大小不可变
choiceStage.initOwner(primaryStage);
choiceStage.initModality(Modality.WINDOW_MODAL); //设置窗口从属关系,防止窗口弹出两次
choiceStage.setX(primaryStage.getX()+50); //防止窗口重叠
choiceStage.setY(primaryStage.getY()+50);
Group group = new Group();
Scene scene = new Scene(group, 400, 500, Color.GRAY);
//创建文本
Text textRed = new Text(50, 370, "Red:");
Text textGreen = new Text(50, 400, "Green:");
Text textBlue = new Text(50, 430, "Blue:");
textRed.setFont(Font.font(null, 18));
textBlue.setFont(Font.font(null, 18));
textGreen.setFont(Font.font(null, 18));
group.getChildren().add(textRed);
group.getChildren().add(textBlue);
group.getChildren().add(textGreen);
//创建滑块
Slider sliderRed = new Slider(0, 255, 0);
Slider sliderGreen = new Slider(0, 255, 0);
Slider sliderBlue = new Slider(0, 255, 0);
sliderRed.setLayoutX(150);
sliderRed.setLayoutY(350);
sliderGreen.setLayoutX(150);
sliderGreen.setLayoutY(380);
sliderBlue.setLayoutX(150);
sliderBlue.setLayoutY(410);
group.getChildren().add(sliderRed);
group.getChildren().add(sliderGreen);
group.getChildren().add(sliderBlue);
//颜色显示方块
Rectangle rect = new Rectangle(20, 20, 360, 300);
rect.setArcWidth(20);
rect.setArcHeight(20);
rect.setFill(Color.rgb(0, 0, 0));
group.getChildren().add(rect);
//确定和取消按钮
Button btnOK = new Button("确定");
Button btnCancle = new Button("取消");
btnOK.setLayoutX(100);
btnOK.setLayoutY(460);
btnCancle.setLayoutX(230);
btnCancle.setLayoutY(460);
btnOK.setPrefSize(80,20);
btnCancle.setPrefSize(80,20);
group.getChildren().add(btnOK);
group.getChildren().add(btnCancle);
//监听三个滑块
ListenSlider(sliderRed, sliderGreen, sliderBlue, rect);
//监听两个按钮
ListenButton(btnOK, btnCancle, primaryStage, priScene, choiceStage, sliderRed, sliderGreen, sliderBlue,btnChange);
//加一根线吧
Line line = new Line();
line.setStroke(Color.WHITE);
line.setStartX(40);
line.setStartY(450);
line.setEndX(355);
line.setEndY(450);
group.getChildren().add(line);
choiceStage.setScene(scene);
choiceStage.show();
}
private void ListenSlider(Slider sliderRed, Slider sliderGreen, Slider sliderBlue, Rectangle rect) {
/*
* 监听三个滑块组件获取0~255的rgb数值,填充显示方块
* */
sliderRed.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
int redValue = (int) sliderRed.getValue();
int greenValue = (int) sliderGreen.getValue();
int blueValue = (int) sliderBlue.getValue();
rect.setFill(Color.rgb(redValue, greenValue, blueValue));
}
});
sliderGreen.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
int redValue = (int) sliderRed.getValue();
int greenValue = (int) sliderGreen.getValue();
int blueValue = (int) sliderBlue.getValue();
rect.setFill(Color.rgb(redValue, greenValue, blueValue));
}
});
sliderBlue.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
int redValue = (int) sliderRed.getValue();
int greenValue = (int) sliderGreen.getValue();
int blueValue = (int) sliderBlue.getValue();
rect.setFill(Color.rgb(redValue, greenValue, blueValue));
}
});
}
private void ListenButton(Button btnOK, Button btnCancle, Stage primaryStage, Scene priScene, Stage choiceStage, Slider sliderRed, Slider sliderGreen, Slider sliderBlue, Button btnChange) {
/*
* 监听确定、取消按钮,点击确定修改第一个窗口场景的颜色,装入舞台显示
* */
btnOK.setOnAction(new EventHandler<ActionEvent>() {
@Override
//确定。获取0-255值,显示颜色
public void handle(ActionEvent event) {
int redValue = (int) sliderRed.getValue();
int greenValue = (int) sliderGreen.getValue();
int blueValue = (int) sliderBlue.getValue();
btnChange.setTextFill(Color.rgb(redValue, greenValue, blueValue));
primaryStage.setScene(priScene);
primaryStage.show();
choiceStage.close();
}
});
btnCancle.setOnAction(new EventHandler<ActionEvent>() {
@Override
//取消。关闭窗口
public void handle(ActionEvent event) {
choiceStage.close();
}
});
}
}