ラジオボタンでRGBカラーを選択し、スライダを使用して値を0〜255に変更するプログラムを作成しようとしています。色の変更はテキストに適用する必要があります。 R、GまたはBが選択されている場合、選択された色のみがテキストに表示されます(緑色が選択されている場合、赤色と青色の値は0です)。スライダでテキストの色を変更する
現在、プログラムはある程度機能しています。たとえば、スライダの値が150で、新しい色を選択してからスライダを動かすと、テキストの色は150に設定され、スライダが設定されている値は新しい価値。私はそれを更新したい場合は、スライダを移動する前に新しい色を選択する必要があります。選択した色ごとに1回更新されます。選択した色をシームレスに更新したい。下のコード例:
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();
}
}
入力が高く評価されています。
返信ありがとうございます!それがリスナーの使用が教えられている学校の割り当てであるので、他の答えは、ソリューションとして選択する場所でより多くのでした。とにかくあなたの答えはまた行く良い方法のように思えるので、私はアップヴォートを与えました。 – Esben86