라디오 버튼을 사용하여 RGB 색상을 선택하고 슬라이더를 사용하여 값을 0 - 255로 변경할 수있는 프로그램을 만들려고합니다. 색상 변경은 텍스트에 적용되어야합니다. R, G 또는 B를 선택한 경우 선택한 색상 만 텍스트에 있어야합니다 (예 : 녹색을 선택하면 빨간색과 파란색 값은 0).슬라이더를 사용하여 텍스트 색상 변경
현재 프로그램은 어느 정도 작동합니다. 예를 들어, 슬라이더가 값 150에 있고 새 색상을 선택한 다음 슬라이더를 움직이면 텍스트 색상이 150으로 설정되거나 슬라이더가 설정된 값으로 설정됩니다. 새로운 가치. 업데이트하려면 슬라이더를 이동하기 전에 새 색상을 선택해야합니다. 선택한 색상별로 한 번만 업데이트됩니다. 선택한 색상을 완벽하게 업데이트하고 싶습니다. 아래 코드 예 :
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();
}
}
모든 입력 사항을 높이 평가합니다!
답장을 보내 주셔서 감사합니다! 청취자의 사용이 가르쳐지는 것은 학교 임무이기 때문에, 다른 답은 해결책으로 선택 될 수있는 장소가 더 많았습니다. 나는 어쨌든 당신의 대답이 좋은 길로 보이기 때문에 나는 upvote를주었습니다. – Esben86