gpt4 book ai didi

javafx - 使用 slider 更改文本颜色

转载 作者:行者123 更新时间:2023-12-05 08:42:33 25 4
gpt4 key购买 nike

我正在尝试制作一个程序,允许用户通过单选按钮选择 RGB 颜色,并使用 slider 在 0 - 255 之间更改值。颜色更改应适用于文本。选择 R、G 或 B 时,文本中应仅显示所选颜色(即,如果选择绿色,则红色和蓝色的值为 0)。

目前该程序在一定程度上可以运行。例如,如果 slider 位于值 150,我选择了一种新颜色然后移动 slider ,文本颜色设置为 150,或者在任何情况下 slider 在尝试将其移动到新值(value)。如果我想让它更新,我必须在移动 slider 之前选择一种新颜色。它只为每种选定的颜色更新一次。我希望它无缝地更新选定的颜色。下面的代码示例:

public class Oblig5 extends Application {

static int colorValue = 0;
static int red = 0;
static int green = 0;
static int blue = 0;

public static void main(String[] args) {

launch(args);

}

public void start(Stage primaryStage) {
// Create panes
BorderPane bPane = new BorderPane();
VBox vBox = new VBox();
bPane.setLeft(vBox);

// Create text and place it in the pane
Text text = new Text("Oblig 5");
text.setFont(Font.font("Times New Roman", FontWeight.NORMAL, FontPosture.REGULAR, 40));
bPane.setCenter(text);

// Create radio buttons and place them in the VBox
RadioButton rbRed = new RadioButton("Red");
RadioButton rbGreen = new RadioButton("Green");
RadioButton rbBlue = new RadioButton("Blue");


ToggleGroup group = new ToggleGroup();
rbRed.setToggleGroup(group);
rbGreen.setToggleGroup(group);
rbBlue.setToggleGroup(group);

// Create handlers for radiobuttons
rbRed.setOnAction(e -> {
if (rbRed.isSelected()) {
red = colorValue;
green = 0;
blue = 0;
}
});

rbGreen.setOnAction(e -> {
if (rbGreen.isSelected()) {
red = 0;
green = colorValue;
blue = 0;
}
});

rbBlue.setOnAction(e -> {
if (rbBlue.isSelected()) {
red = 0;
green = 0;
blue = colorValue;
}
});

vBox.getChildren().addAll(rbRed, rbGreen, rbBlue);

// Create a slider and place it in the BorderPane
Slider slider = new Slider(0, 255, 135);
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
bPane.setBottom(slider);
bPane.setAlignment(slider, Pos.CENTER);

// Create a handler for the slider
slider.valueProperty().addListener(ov -> {
colorValue = (int) slider.getValue();
text.setFill(Color.rgb(red, green, blue));
});

// Create a scene and place it in the stage
Scene scene = new Scene(bPane, 400, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("Oblig 5");
primaryStage.show();

}

}

非常感谢任何意见!

最佳答案

[注意:@fabian 在我写这篇文章时发布了一个答案。任何一个答案都可以,我想我也会发布这个,因为它显示了略有不同的技术。与往常一样,在 JavaFX 中有多种实现同一目标的好方法。]

当所选按钮发生变化或 slider 值发生变化时,您需要使用适当的值设置文本填充。现在,当所选按钮发生变化时,您只更新变量 redgreenblue(但不更改文本填充) 并且当 slider 值发生变化时,您可以使用 redgreenblue 调用 setFill(...) >,但不要更改这些变量的值。

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Slider;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Oblig5 extends Application {

public static void main(String[] args) {

launch(args);

}

public void start(Stage primaryStage) {
// Create panes
BorderPane bPane = new BorderPane();
VBox vBox = new VBox();
bPane.setLeft(vBox);

Text text = new Text("Oblig 5");
text.setFont(Font.font("Times New Roman", FontWeight.NORMAL, FontPosture.REGULAR, 40));
bPane.setCenter(text);

RadioButton rbRed = new RadioButton("Red");
RadioButton rbGreen = new RadioButton("Green");
RadioButton rbBlue = new RadioButton("Blue");


ToggleGroup group = new ToggleGroup();
rbRed.setToggleGroup(group);
rbGreen.setToggleGroup(group);
rbBlue.setToggleGroup(group);

vBox.getChildren().addAll(rbRed, rbGreen, rbBlue);

Slider slider = new Slider(0, 255, 135);
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
bPane.setBottom(slider);
BorderPane.setAlignment(slider, Pos.CENTER);

// Create a handler for the updating text fill:
ChangeListener<Object> updateListener = (obs, oldValue, newValue) -> {
int colorValue = (int) slider.getValue() ;
int red = rbRed.isSelected() ? colorValue : 0 ;
int green = rbGreen.isSelected() ? colorValue : 0 ;
int blue = rbBlue.isSelected() ? colorValue : 0 ;
text.setFill(Color.rgb(red, green, blue));
};
slider.valueProperty().addListener(updateListener);
group.selectedToggleProperty().addListener(updateListener);

// Create a scene and place it in the stage
Scene scene = new Scene(bPane, 400, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("Oblig 5");
primaryStage.show();

}


}

关于javafx - 使用 slider 更改文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40306370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com