简体   繁体   English

如何通过按键盘上的“回车”而不是按“开始比赛”按钮来开始比赛。”

[英]How can I start the event by pressing "enter" on the keyboard instead of pressing the button "Start Race"."

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.scene.shape.*;
import javafx.scene.paint.Color;
import javafx.animation.*;
import static javafx.application.Application.launch;
import javafx.util.Duration;

public class CarsRacing extends Application 
{
  public static void main(String[] args) 
  {
    launch(args);
  }
  public void start(Stage primaryStage) 
  {
    Pane pane = new Pane();
    
    Button btn = new Button("Start Race"); ``` creating button with lable "Start Race"
Polygon p1 = new Polygon();
Polygon p2 = new Polygon();
Polygon p3 = new Polygon();
    
Circle wheel1 = new Circle(90,110,10);
Circle wheel2 = new Circle(140,110,10);
Circle wheel3 = new Circle(90,220,10);
Circle wheel4 = new Circle(140,220,10);
Circle wheel5 = new Circle(90,320,10);
Circle wheel6 = new Circle(140,320,10);

Line l1 = new Line(50,140,1050, 140);
Line l2 = new Line(50,240,1050, 240);
Line l3 = new Line(50,340,1050, 340);

p1.getPoints().addAll
(
 new Double[]
 {        
    50.0,110.0,
    50.0,80.0,
    80.0,80.0,
    100.0,60.0,
    130.0,60.0,
    150.0,80.0,
    180.0,80.0,
    180.0,110.0,
 }
);
p2.getPoints().addAll
(
 new Double[]
 {        
    50.0,220.0,
    50.0,190.0,
    80.0,190.0,
    100.0,170.0,
    130.0,170.0,
    150.0,190.0,
    180.0,190.0,
    180.0,220.0,
 }
);

p3.getPoints().addAll
(
 new Double[]
 {        
    50.0,320.0,
    50.0,290.0,
    80.0,290.0,
    100.0,270.0,
    130.0,270.0,
    150.0,290.0,
    180.0,290.0,
    180.0,320.0,
 }
);

p1.setFill(Color.ORANGE);
p1.setStroke(Color.BLUE);
p2.setFill(Color.GREEN);
p2.setStroke(Color.RED);
p3.setFill(Color.BLUE);
p3.setStroke(Color.ORANGE);

wheel1.setFill(Color.BLACK);
wheel2.setFill(Color.BLACK);
wheel3.setFill(Color.BLACK);
wheel4.setFill(Color.BLACK);
wheel5.setFill(Color.BLACK);
wheel6.setFill(Color.BLACK);

Path path1 = new Path();
Path path2 = new Path();
Path path3 = new Path();
Path path4 = new Path();                    
Path path5 = new Path();
Path path6 = new Path();

Path path7 = new Path();                    
Path path8 = new Path();
Path path9 = new Path();

path1.getElements().addAll(new MoveTo( 90, 110), new HLineTo(950));
path2.getElements().addAll(new MoveTo(140, 110), new HLineTo(1000));
path3.getElements().addAll(new MoveTo( 90, 220), new HLineTo(950));
path4.getElements().addAll(new MoveTo(140, 220), new HLineTo(1000));
path5.getElements().addAll(new MoveTo( 90, 320), new HLineTo(950));
path6.getElements().addAll(new MoveTo(140, 320), new HLineTo(1000));

path7.getElements().addAll(new MoveTo(  115, 85), new HLineTo(970));
path8.getElements().addAll(new MoveTo(  115, 195), new HLineTo(970));
path9.getElements().addAll(new MoveTo(  115, 295), new HLineTo(970));

PathTransition pt1 = new PathTransition(Duration.millis(4000), path1, wheel1);
PathTransition pt2 = new PathTransition(Duration.millis(3970), path2, wheel2);
PathTransition pt3 = new PathTransition(Duration.millis(3000), path3, wheel3);
PathTransition pt4 = new PathTransition(Duration.millis(2970), path4, wheel4);  
PathTransition pt5 = new PathTransition(Duration.millis(5000), path5, wheel5);
PathTransition pt6 = new PathTransition(Duration.millis(4970), path6, wheel6);

PathTransition pt7 = new PathTransition(Duration.millis(4000), path7, p1);
PathTransition pt8 = new PathTransition(Duration.millis(3000), path8, p2);
PathTransition pt9 = new PathTransition(Duration.millis(5000), path9, p3);

btn.setOnAction```

// Instead of using this button to start the event how can i make it start with pressing enter on the keyboard //不是使用这个按钮来启动事件,我怎样才能通过按下键盘上的回车键来启动它

    (
      e -> 
      {
         pt1.play();
         pt2.play();
         pt3.play();
         pt4.play();
         pt5.play();
         pt6.play();
         pt7.play();
         pt8.play();
         pt9.play();
      }
    );
    
    pane.getChildren().addAll(btn, p1, p2, p3, wheel1, wheel2, wheel3, wheel4, wheel5, wheel6, l1, l2, l3);
``` adding the button to the pane```
    
    Scene scene = new Scene(pane, 1100, 370);
    primaryStage.setTitle("Cars Racing"); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
  }
  
}```


Make your button a default button:使您的按钮成为默认按钮:

 button.setDefaultButton(true);

From the javadoc :javadoc

A default Button is the button that receives a keyboard VK_ENTER press, if no other node in the scene consumes it.默认按钮是接收键盘 VK_ENTER 按下的按钮,如果场景中没有其他节点使用它。

Alternately see:另见:

Which provides this solution:它提供了这个解决方案:

scene.setOnKeyPressed( event -> {
  if( event.getCode() == KeyCode.ENTER ) {
    doSomething();
  }
} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不按录制按钮的情况下启用自动开始录制视频? - How can I enable auto start recording video without pressing record button? 在按开始按钮到特定时间(HH:mm:ss)格式后,如何设置初始延迟 - How can I set the initial Delay after pressing the start Button to a specific time (HH:mm:ss) format 按“ Enter”再次启动Java程序 - Pressing “Enter” to start Java program again SWT按下ENTER按钮事件处理程序 - SWT Pressing ENTER Button event handler 如何通过在弹出对话框中按OK按钮正确启动nextQuestion方法? - How to properly start nextQuestion method by pressing OK button in a popup dialog? 按JButton而不是键盘 - Pressing JButton instead of keyboard 如何在Android中按相同的按钮(Buggy实现)来开始/停止歌曲? - How to I start/stop a song by pressing the same button (buggy implementation) in Android? 使用JFrame,如何获取Enter键以触发与按下按钮相同的结果? - Using JFrame, how can I get the enter key to trigger the same result as pressing the button? 如何检查用户是否按下了特定的键,即 Enter Key 像 python 的键盘模块? - How can I check if a user is pressing a specific key i.e. Enter Key Like python's keyboard module? 如何关联按“输入”与点击按钮? - How to associate pressing “enter” with clicking button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM