我一直想在COUNTER小于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");
}
}
}
谢谢!
答案 0 :(得分:1)
如果我对您的理解正确,则可以使用Timeline
更改圆圈的颜色,并在按下按钮时停止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;
/**
*
* @author blj0011
*/
public class JavaFXApplication357 extends Application
{
@Override
public void start(Stage primaryStage)
{
List<Color> colors = Arrays.asList(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, Color.PURPLE);
AtomicInteger counter = new AtomicInteger(0);
Circle circle = new Circle(200, Color.TRANSPARENT);
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(300), (event) -> {
circle.setFill(colors.get(counter.getAndIncrement() % colors.size()));
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
Button btn = new Button();
btn.setText("Stop");
btn.setOnAction((ActionEvent event) -> {
timeline.stop();
});
VBox root = new VBox(new StackPane(circle), new StackPane(btn));
Scene scene = new Scene(root, 450, 450);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}