我正在使用FXML文件开发我的第一个JavaFX应用程序。 我的程序包含以下几个元素:
工作简短说明:
我有两个问题:
在1)我不知道怎么了。 2)我认为我不应该将运行dataCollector放在负责按钮事件的方法中。但是,我不确定如何正确地做到这一点。
您有什么建议,想法如何写这些片段吗? 是FXML或方法出现问题吗?
负责这部分的FXML文件如下:
<TextArea fx:id="communicateTextField"
prefHeight="300.0" prefWidth="300.0" >
<Button fx:id="addMyoButton"
onAction="#addMyo" >
方法addMyo(),在按下按钮后运行。
public void addMyo(ActionEvent event) {
try {
Hub hub = new Hub("com.example.hello-myo");
communicateTextField.setText("Attempting to find a Myo...");
Myo myo = hub.waitForMyo(10000);
if (myo == null) {
throw new RuntimeException("Unable to find a Myo!");
communicateTextField.setText(""Unable to find a Myo!");
}
communicateTextField.setText("Connected to a Myo armband!");
DeviceListener dataCollector = new DataCollector();
hub.addListener(dataCollector);
while (true) {
hub.run(1000 / 20);
communicateTextField.setText(dataCollector);
}
} catch (Exception e) {
System.err.println("Error: ");
e.printStackTrace();
System.exit(1);
}
}
Method for printing communicates to TextArea
public void printCommunicate(String communicate) {
communicateTextField.setText(communicate);
}