开发者社区> 问答> 正文

如何调用类的实例以将其与GUI连接?

我试图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>

展开
收起
几许相思几点泪 2019-12-24 20:43:37 846 0
1 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
从“连接”到“交互” 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多