javafx事件处理程序返回错误,我不完全了解
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javafx事件处理程序返回错误,我不完全了解相关的知识,希望对你有一定的参考价值。
我对Javafx相当陌生,试图了解我遇到的错误。创建事件处理程序时,程序返回错误:错误:(56,94)java:->预期。
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application
@Override
public void start(Stage primaryStage) throws Exception
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Credit Card Input");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
public static void main(String[] args)
launch(args);
Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
public class Controller
@FXML
TextField creditCardNo;
@FXML
TextField expDate;
@FXML
TextField ccvInput ;
@FXML
String imageOutput = new String("default.png");
public String ccvCheck(Object ccvInput)
String ccInput = this.ccvInput.getText();
char[] intArray = ccInput.toCharArray();
StringBuilder doubleDigitCheck = new StringBuilder();
doubleDigitCheck.append(intArray[0]).append(intArray[1]);
String str = new String(doubleDigitCheck);
int checkValue = Integer.parseInt(String.valueOf(intArray[0]));
String str2 = ccInput.substring(0,1);
if (str.matches("34"))
return imageOutput = new String("amex.png");
if (str.matches("35"))
return this.imageOutput = new String("jcb.png");
if (str.matches("37"))
return this.imageOutput = new String("amex.png");
if (str2.matches("4"))
return this.imageOutput = new String("visa.png");
if (str2.matches("5"))
return this.imageOutput = new String("mastercard.png");
else return this.imageOutput = new String("default.png");
public TextField getCcvInput()
return ccvInput;
EventHandler<? super KeyEvent> eventHandler= ccvInput.setOnKeyReleased(eventHandler -> () ccvCheck(ccvInput); );
//TODO:Set event handler to run checkValue and link to ccvInput
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<GridPane prefHeight="119.0" prefWidth="321.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints maxWidth="160.5" minWidth="160.5" prefWidth="160.5" />
<ColumnConstraints maxWidth="0.0" minWidth="0.0" prefWidth="0.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints minHeight="10.0" prefHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TextField fx:id="creditCardNo" GridPane.rowIndex="1" />
<TextField fx:id="expDate" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="ccvInput" onAction="#eventHandler" GridPane.rowIndex="3"/>
<ImageView fx:id="imageOutput" fitHeight="72.0" fitWidth="105.0" opacity="0.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.rowIndex="3">
<image>
<Image url="@../../visa.png" />
</image></ImageView>
<Label text="Credit Card Number" />
<Label text="Exp Date" GridPane.columnIndex="1" />
<Label text="CCV" GridPane.rowIndex="2" />
</children>
</GridPane>
我正在尝试设置事件处理程序,以便当用户在CVV字段(TextField ccvInput)中输入数字时,将选择一个图像。目前收到错误,据我了解。他们的语法是正确的。非常感谢您对此有任何帮助。
答案
这不是创建EventHandler
的正确方法。您可以使用lambda直接在EventHandler
方法中定义setOnKeyReleased()
:
ccvInput.setOnKeyReleased(event ->
ccvCheck(ccvInput.getText());
);
但是,您的代码可能会遇到很多其他问题,您可能需要重新考虑您的结构和流程。
以上是关于javafx事件处理程序返回错误,我不完全了解的主要内容,如果未能解决你的问题,请参考以下文章