鼠标单击事件不起作用

时间:2018-04-04 22:45:18

标签: java javafx mouseevent

Hello Everyone我正在为课程做一个程序而且我无法使用鼠标点击事件工作我有一个关键的新闻事件可以工作但由于某种原因我无法点击鼠标甚至响应。我添加了代码并且想知道是否有人知道我做错了什么我更喜欢它在我单击文本计算时工作,因为我必须在计算后添加一个清除按钮。谢谢所有帮助我解决这个问题的人。

P.S我使用intelli J作为我的IDE我不知道这是否重要

import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.geometry.Pos;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.KeyCode;

public class DecimalToBase extends Application {
protected TextField tfDecimal = new TextField();
protected TextField tfOctal = new TextField();
protected TextField tfBinary = new TextField();
protected Button tfCalculate = new Button();

@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
    // Set text field preferences
    tfDecimal.setAlignment(Pos.BOTTOM_RIGHT);
    tfOctal.setAlignment(Pos.BOTTOM_RIGHT);
    tfBinary.setAlignment(Pos.BOTTOM_RIGHT);
    tfCalculate.setAlignment(Pos.BASELINE_RIGHT);

    // Create a grid pane and add nodes to it
    GridPane pane = new GridPane();
    pane.setAlignment(Pos.CENTER);
    pane.setHgap(10);
    pane.setVgap(2);
    pane.add(new Label("Decimal"), 0, 0);
    pane.add(tfDecimal, 1, 0);
    pane.add(new Label("Octal"), 0, 1);
    pane.add(tfOctal, 1, 1);
    pane.add(new Label("Binary"), 0, 2);
    pane.add(tfBinary, 1, 2);
    pane.add(new Label("Calculate"),0,3);


    // Create and register handlers
    tfDecimal.setOnKeyPressed(e -> {
        if (e.getCode() == KeyCode.ENTER) {
            tfOctal.setText(Integer.toOctalString(
                    Integer.parseInt(tfDecimal.getText())));

            tfBinary.setText(Integer.toBinaryString(
                    Integer.parseInt(tfDecimal.getText())));
        }
    });

    tfOctal.setOnKeyPressed(e -> {
        if (e.getCode() == KeyCode.ENTER) {
            tfDecimal.setText(String.valueOf(
                    Integer.parseInt(tfOctal.getText(), 10)));

            tfBinary.setText(Integer.toBinaryString(
                    Integer.parseInt(tfOctal.getText(), 10)));
        }
    });

    tfBinary.setOnKeyPressed(e -> {
        if (e.getCode() == KeyCode.ENTER) {
            tfDecimal.setText(String.valueOf(
                    Integer.parseInt(tfBinary.getText(), 2)));

            tfOctal.setText(Integer.toOctalString(
                    Integer.parseInt(tfBinary.getText(), 2)));
        }
    });
    tfCalculate.setOnMouseClicked(e -> {
                if (e.getButton() == MouseButton.PRIMARY) {
                    tfDecimal.setText(String.valueOf(Integer.parseInt(tfDecimal.getText())));

                    tfOctal.setText(Integer.toOctalString(Integer.parseInt(tfDecimal.getText())));

                    tfBinary.setText(Integer.toBinaryString(Integer.parseInt(tfDecimal.getText())));
                }
            });
    // Create a scene and place it in the stage
    Scene scene = new Scene(pane, 500, 500);
    primaryStage.setTitle("Program16-Base Converter"); // Set the stage 
title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
}
}

0 个答案:

没有答案