我试图RFIDtagListener在GUI窗口运行时在后台运行,所以我在类中创建了一个RFIDTagListener方法,并在MainController类中创建了该方法的实例Main。当我运行程序时,只有GUI运行并做出响应,而没有RFIDtagListener!
有人可以帮我看看有什么问题吗?
主类:
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Wallet locator !");
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws PhidgetException {
//new Main();
launch(args);
new MainController();
}
}
MainController类:
public class MainController {
RFID rfid = new RFID();
public MainController() throws PhidgetException {
// Make the RFID Phidget able to detect loss or gain of an rfid card
rfid.addTagListener(new RFIDTagListener() {
// What to do when a tag is found
public void onTag(RFIDTagEvent e) {
try {
ReaderNo = String.valueOf(rfid.getDeviceSerialNumber());
} catch (PhidgetException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Reader serial number is " + ' '+ReaderNo);
wallet walletData = new wallet("385055");
walletJson = gson.toJson(walletData);
String resultfromserver = sendToServer(walletJson);
System.out.println("DEBUG: data in json : " +resultfromserver);
wallet walletObject = gson.fromJson(resultfromserver, wallet.class);
System.out.println("DEBUG: The wallet's Data: "+' '+ walletObject);
WalletName.setText(walletObject.getWalletName());
}
});
rfid.addTagLostListener(new RFIDTagLostListener() {
// What to do when a tag is lost
public void onTagLost(RFIDTagLostEvent e) {
// optional print, used as debug here
System.out.println("DEBUG: Tag lost: " + e.getTag());
}
});
}
}
Main.FXML:
<?xml version="1.0" encoding="UTF-8"?>
<AnchorPane prefHeight="269.0" prefWidth="403.0" xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<Button layoutX="215.0" layoutY="201.0" mnemonicParsing="false" onAction="#getTags"
prefHeight="35.0" prefWidth="58.0" text="tags" />
<Label fx:id="message" layoutX="322.0" layoutY="14.0" prefHeight="35.0" prefWidth="71.0"
text="Room A" textAlignment="CENTER" />
<ListView id="studentObservableList" fx:id="list" layoutX="14.0" layoutY="174.0"
prefHeight="131.0" prefWidth="198.0" />
<TableView fx:id="tableViewData" layoutX="14.0" prefHeight="162.0" prefWidth="198.0">
<columns>
<TableColumn fx:id="NameColumn" prefWidth="68.79998779296875" text="Name" />
<TableColumn fx:id="LocationColumn" prefWidth="75.20001220703125" text="Location" />
<TableColumn fx:id="TagColumn" prefWidth="53.60003662109375" text="Tag" />
</columns>
</TableView>
<Button layoutX="215.0" layoutY="63.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="71.0"
text="wallets" />
<ListView layoutX="307.0" layoutY="49.0" prefHeight="174.0" prefWidth="173.0" />
<Button layoutX="375.0" layoutY="236.0" mnemonicParsing="false" onAction="#Track"
prefHeight="29.0" prefWidth="58.0" text="Track" />
<Label fx:id="WalletName" layoutX="381.0" layoutY="20.0" prefHeight="22.0" prefWidth="71.0"
text="Label" />
</children>
</AnchorPane>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。