如何在程序等待按下按钮时执行某些操作?

我想一直更改CIRECLE颜色,计数器小于3,而我等待单击按钮。当圆环变红时,他需要按“停止”。这是程序:


public class Q1_2 extends Application {

    private MyCircle circle = MyCircle.getInstance();

    public int COUNTER;

    public Color CURRENT_COLOR;

    public Color currentColor;

    public static void main(String[] args) {

        launch();

    }

    @Override

    public void start(Stage primaryStage) throws Exception {

        circle.setRadius(100);

        RadioButton bColor = new RadioButton("Stop");

        COUNTER = 0;

        HBox box = new HBox(40, bColor);

        box.setPadding(new Insets(30));

        StackPane pane = new StackPane(circle, box);

        Scene scene = new Scene(pane, 500, 500);

        primaryStage.setScene(scene);

        primaryStage.show();

        bColor.setOnAction(e -> {

            if (bColor.isSelected() == true && currentColor != javafx.scene.paint.Color.RED) {

                COUNTER++;

                bColor.setSelected(false);

            }

            if (bColor.isSelected() == true && currentColor == javafx.scene.paint.Color.RED)

                System.out.println("GREAT");

        });

        while (COUNTER < 3) {

            currentColor = chooseColor();

            circle.setFill(currentColor);

            if (COUNTER == 3)

                System.out.println("YOU LOSE");

        }

    }

}

谢谢!


浮云间
浏览 109回答 1
1回答

慕田峪9158850

如果我对你的理解是正确的,你可以使用时间轴来改变圆圈的颜色,并在按下按钮时停止。Timeline示例代码:import java.util.Arrays;import java.util.List;import java.util.concurrent.atomic.AtomicInteger;import javafx.animation.KeyFrame;import javafx.animation.Timeline;import javafx.application.Application;import javafx.event.ActionEvent;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.StackPane;import javafx.scene.layout.VBox;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.stage.Stage;import javafx.util.Duration;/**&nbsp;*&nbsp;* @author blj0011&nbsp;*/public class JavaFXApplication357 extends Application{&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage primaryStage)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List<Color> colors = Arrays.asList(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, Color.PURPLE);&nbsp; &nbsp; &nbsp; &nbsp; AtomicInteger counter = new AtomicInteger(0);&nbsp; &nbsp; &nbsp; &nbsp; Circle circle = new Circle(200, Color.TRANSPARENT);&nbsp; &nbsp; &nbsp; &nbsp; Timeline timeline = new Timeline(new KeyFrame(Duration.millis(300), (event) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; circle.setFill(colors.get(counter.getAndIncrement() % colors.size()));&nbsp; &nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; &nbsp; &nbsp; timeline.setCycleCount(Timeline.INDEFINITE);&nbsp; &nbsp; &nbsp; &nbsp; timeline.play();&nbsp; &nbsp; &nbsp; &nbsp; Button btn = new Button();&nbsp; &nbsp; &nbsp; &nbsp; btn.setText("Stop");&nbsp; &nbsp; &nbsp; &nbsp; btn.setOnAction((ActionEvent event) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timeline.stop();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; VBox root = new VBox(new StackPane(circle), new StackPane(btn));&nbsp; &nbsp; &nbsp; &nbsp; Scene scene = new Scene(root, 450, 450);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setTitle("Hello World!");&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setScene(scene);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.show();&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param args the command line arguments&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; launch(args);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java