使用角色退出程序

时间:2016-03-16 00:17:45

标签: java javafx keylistener

你如何使用字母" X"退出该计划?我使用了键字事件,键号为#34; X"用作我的退出键,但我真的应该使用Key Listener,我只是不能让它工作。任何提示或建议都非常感谢。

import java.awt.event.KeyAdapter;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class module1 extends Application {




Pane pane = new Pane();
double width = 400;
double height = 400;
double cX = width / 2;
double cY = height / 2;


@Override
public void start(Stage primaryStage) {


    pane.setOnKeyPressed(e -> {
        switch (e.getCode()) {
            case UP: moveUp(); break;
            case DOWN: moveDown(); break;
            case LEFT: moveLeft(); break;
            case RIGHT: moveRight(); break;
        default:
            break;
        }
    });

    primaryStage.setScene(new Scene(pane, width, height));
    primaryStage.setTitle("Click to see position..");
    primaryStage.show();
    pane.requestFocus();
}

private void moveUp() {
    Line sLine = new Line(cX, cY, cX, cY - 10);
    sLine.setStroke(Color.BLACK);
    pane.getChildren().add(sLine);
    cY -= 10;

}



class KeyHandler extends KeyAdapter {
    public void keyPressed(KeyEvent e)
    {                
        if (e.getKeyChar() == 'x') {

            System.exit(0);
        }

        }
    }



public static void main(String[] args) {
    Application.launch(args);


    }


}

1 个答案:

答案 0 :(得分:1)

删除你的AWT KeyEvent并在你的舞台上添加这样的东西:

<solid android:color="?attr/MY_ATTRIBUTE" />