我正在尝试创建4个并发运行的秒表。它们都是在程序开始时开始的。
我希望用户能够使用Q,W,E,R键来加速秒表(在键盘模式后从左到右)。我已经建立了几乎所有东西,但它不会让我按任何其他键但R键加速第4个时钟。
我想实现线程来允许这个。我该怎么做呢?
下面是代码:
public class Stopwatches extends Application {
@Override
public void start(Stage primaryStage) {
Time time1 = new Time();
Time time2 = new Time();
Time time3 = new Time();
Time time4 = new Time();
Text text1 = new Text("00:00:00");
Text text2 = new Text("00:00:00");
Text text3 = new Text("00:00:00");
Text text4 = new Text("00:00:00");
text1.setFont(Font.font("Times", 35));
text2.setFont(Font.font("Times", 35));
text3.setFont(Font.font("Times", 35));
text4.setFont(Font.font("Times", 35));
Timeline textClock1 = createTextStopwatch(time1, text1);
textClock1.play();
Timeline textClock2 = createTextStopwatch(time2, text2);
textClock2.play();
Timeline textClock3 = createTextStopwatch(time3, text3);
textClock3.play();
Timeline textClock4 = createTextStopwatch(time4, text4);
textClock4.play();
BorderPane pane = new BorderPane();
TilePane tilePane = new TilePane();
tilePane.setPadding(new Insets(15,5,5,5));
tilePane.setAlignment(Pos.CENTER);
tilePane.setHgap(40.0);
tilePane.getChildren().addAll(text1, text2, text3, text4);
pane.setCenter(tilePane);
Scene scene = new Scene(pane, 1500, 400);
scene.setOnKeyPressed(e -> {
if (e.getCode() == Q ){
textClock1.setRate(20.0);
}
});
scene.setOnKeyReleased(e -> {
if (e.getCode() == Q ){
textClock1.setRate(1.0);
}
});
scene.setOnKeyPressed(e -> {
if (e.getCode() == W ){
textClock2.setRate(20.0);
}
});
scene.setOnKeyReleased(e -> {
if (e.getCode() == W ){
textClock2.setRate(1.0);
}
});
scene.setOnKeyPressed(e -> {
if (e.getCode() == E ){
textClock3.setRate(20.0);
}
});
scene.setOnKeyReleased(e -> {
if (e.getCode() == E ){
textClock3.setRate(1.0);
}
});
scene.setOnKeyPressed(e -> {
if (e.getCode() == R ){
textClock4.setRate(20.0);
}
});
scene.setOnKeyReleased(e -> {
if (e.getCode() == R ){
textClock4.setRate(1.0);
}
});
primaryStage.setTitle("Stop Watch");
primaryStage.setScene(scene);
primaryStage.show();
}
public Timeline createTextStopwatch(Time time, Text text) {
Timeline animation = new Timeline(
new KeyFrame(Duration.millis(1000), e -> {
time.increase();
text.setText(time.toString());
}));
animation.setCycleCount(Timeline.INDEFINITE);
return animation;
}
public static void main(String[] args) {
launch(args);
}
}
我已经第二次尝试实现多线程,但它不会工作。
public class Stopwatches extends Application {
@Override
public void start(Stage primaryStage) {
Time time1 = new Time();
Time time2 = new Time();
Time time3 = new Time();
Time time4 = new Time();
Text text1 = new Text("00:00:00");
Text text2 = new Text("00:00:00");
Text text3 = new Text("00:00:00");
Text text4 = new Text("00:00:00");
text1.setFont(Font.font("Times", 35));
text2.setFont(Font.font("Times", 35));
text3.setFont(Font.font("Times", 35));
text4.setFont(Font.font("Times", 35));
Timeline textClock1 = createTextStopwatch(time1, text1);
textClock1.play();
Timeline textClock2 = createTextStopwatch(time2, text2);
textClock2.play();
Timeline textClock3 = createTextStopwatch(time3, text3);
textClock3.play();
Timeline textClock4 = createTextStopwatch(time4, text4);
textClock4.play();
BorderPane pane = new BorderPane();
TilePane tilePane = new TilePane();
tilePane.setPadding(new Insets(15,5,5,5));
tilePane.setAlignment(Pos.CENTER);
tilePane.setHgap(40.0);
tilePane.getChildren().addAll(text1, text2, text3, text4);
pane.setCenter(tilePane);
Scene scene = new Scene(pane, 1500, 400);
Runnable task1 = new IncreaseTime(textClock1);
Runnable task2 = new DecreaseTime(textClock1);
Runnable task3 = new IncreaseTime(textClock2);
Runnable task4 = new DecreaseTime(textClock2);
Runnable task5 = new IncreaseTime(textClock3);
Runnable task6 = new DecreaseTime(textClock3);
Runnable task7 = new IncreaseTime(textClock4);
Runnable task8 = new DecreaseTime(textClock4);
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
Thread thread3 = new Thread(task3);
Thread thread4 = new Thread(task4);
Thread thread5 = new Thread(task5);
Thread thread6 = new Thread(task6);
Thread thread7 = new Thread(task7);
Thread thread8 = new Thread(task8);
scene.setOnKeyPressed(e -> {
if (e.getCode() == Q ){
thread1.start();
}
});
scene.setOnKeyReleased(e -> {
if (e.getCode() == Q ){
thread2.start();
}
});
scene.setOnKeyPressed(e -> {
if (e.getCode() == W ){
thread3.start();
}
});
scene.setOnKeyReleased(e -> {
if (e.getCode() == W ){
thread4.start();
}
});
scene.setOnKeyPressed(e -> {
if (e.getCode() == E ){
thread5.start();
}
});
scene.setOnKeyReleased(e -> {
if (e.getCode() == E ){
thread6.start();
}
});
scene.setOnKeyPressed(e -> {
if (e.getCode() == R ){
thread7.start();
}
});
scene.setOnKeyReleased(e -> {
if (e.getCode() == R ){
thread8.start();
}
});
primaryStage.setTitle("Stop Watch");
primaryStage.setScene(scene);
primaryStage.show();
}
public Timeline createTextStopwatch(Time time, Text text) {
Timeline animation = new Timeline(
new KeyFrame(Duration.millis(1000), e -> {
time.increase();
text.setText(time.toString());
}));
animation.setCycleCount(Timeline.INDEFINITE);
return animation;
}
public static void main(String[] args) {
launch(args);
}
}
class IncreaseTime implements Runnable {
Timeline textClock;
public IncreaseTime(Timeline textClock) {
this.textClock = textClock;
}
@Override
public void run() {
textClock.setRate(20.0);
}
}
class DecreaseTime implements Runnable {
Timeline textClock;
public DecreaseTime(Timeline textClock) {
this.textClock = textClock;
}
@Override
public void run() {
textClock.setRate(1.0);
}
}
我忘了show我的二级类创建了Time对象,这里是:
public class Time {
int value = 0;
int getSecond() {
return value % 60;
}
int getMinute() {
return (value / 60) % 60;
}
int getHour() {
return value / 3600;
}
void reset() {
value = 0;
}
void increase() {
value++;
}
@Override
public String toString() {
return getTwoDigitString(getHour()) + ":" + getTwoDigitString(getMinute())
+ ":" + getTwoDigitString(getSecond());
}
static String getTwoDigitString(int v) {
if (v < 10)
return "0" + v;
else
return "" + v;
}
}
答案 0 :(得分:0)
应用程序按下所有按钮Q,W,E,R。
问题是textClock4上的所有setOnKeyReleased方法都是setRate。 所以我相信你认为只是按下你的R按钮。
听起来像你想要的地方
Q to operate on textClock1
W to operate on textClock2
E to operate on textClock3
R to operate on textClock4
调试提示是在按下按钮时从场景向控制台添加打印语句。您开始分解并缩小代码中问题所在的位置。
答案 1 :(得分:0)
你的eventListeners覆盖了他们自己。使用一个setOnKeyPressed和setOnKeyReleased,它使用if-then-else实现逻辑。