我无法通过在connectArduino方法中使用settext()来设置tempDisplay标签的文本。它只能显示部分字符串,如符号“°C”,就像屏幕截图所示。但字符串“输入”实际上可以打印出来。 这是我的控制器类
import javafx.application.Platform;
import javafx.scene.chart.LineChart;
import javafx.scene.control.*;
import javafx.event.ActionEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ToggleButton;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
import jssc.SerialPort;
import static jssc.SerialPort.MASK_RXCHAR;
import jssc.SerialPortEvent;
import jssc.SerialPortException;
import jssc.SerialPortList;
public class MainController {
SerialPort arduinoPort = null;
ObservableList<String> portList;
public String input;
public static double max;
public static double min;
public static String phoneNumber;
public static final String ACCOUNT_SID =
"AC935209d3c44660b4a550e3380249857a";
public static final String AUTH_TOKEN = "42bcd28e23344404c737eb3499d2a747";
final int NUM_OF_POINT = 300;
XYChart.Series series;
@FXML
private ComboBox comboBox;
@FXML
private ToggleButton modeButton;
@FXML
private ToggleButton ledButton;
@FXML
private Button connectButton;
@FXML
private TextArea displayArea;
@FXML
private Label tempDisplay;
@FXML
private NumberAxis xAxis;
@FXML
private NumberAxis yAxis;
@FXML
private LineChart lineChart;
@FXML
private void initialize(){
displayArea.setEditable(false);
detectPort();
comboBox.setPromptText("Port List");
comboBox.setItems(portList);
setLineChart();
//sendSMS();
}
private void detectPort(){
portList = FXCollections.observableArrayList();
String[] serialPortNames = SerialPortList.getPortNames();
portList.addAll(serialPortNames);
}
private void connect(){
disconnectArduino();
if(connectButton.getText().equals("Connect")) {
connectArduino(comboBox.getValue().toString());
}
ledButton.setSelected(false);
}
public boolean connectArduino(String port){
displayArea.appendText("connect Arduino");
if(port.isEmpty()){
alert("Connect Failed!");
return false;
}
else {
connectButton.setText("Disconnect");
boolean success = false;
SerialPort serialPort = new SerialPort(port);
try {
serialPort.openPort();
serialPort.setParams(
SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setEventsMask(MASK_RXCHAR);
serialPort.addEventListener((SerialPortEvent serialPortEvent) -> {
if (serialPortEvent.isRXCHAR()) {
try {
input = serialPort.readString(serialPortEvent
.getEventValue());
System.out.println(input);
if (input.contains("false")) {
alert("Unplugged Sensor");
} else {
Platform.runLater(() -> {
String st = input + " °C";
tempDisplay.setText(st);
// shiftSeriesData(Float.parseFloat(input));
});
}
} catch (SerialPortException ex) {
Logger.getLogger(MainController.class.getName())
.log(Level.SEVERE, null, ex);
alert("No Data Available");
}
}
});
arduinoPort = serialPort;
success = true;
} catch (SerialPortException ex) {
Logger.getLogger(MainController.class.getName())
.log(Level.SEVERE, null, ex);
System.out.println("SerialPortException: " + ex.toString());
alert("No Data Available");
}
return success;
}
}
这是我的fxml类
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="627.0" prefWidth="664.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainController">
<children>
<LineChart fx:id="lineChart" createSymbols="false" layoutY="74.0" prefHeight="343.0" prefWidth="396.0" title="Temperature of Session">
<xAxis>
<NumberAxis autoRanging="false" label="Time (s)" side="BOTTOM" tickUnit="100" fx:id="xAxis" upperBound="300"/>
</xAxis>
<yAxis>
<NumberAxis fx:id="yAxis" autoRanging="false" label="Temperature (°C)" lowerBound="10" side="RIGHT" upperBound="50" />
</yAxis>
</LineChart>
<TextArea fx:id="displayArea" editable="false" layoutX="15.0" layoutY="426.0" prefHeight="186.0" prefWidth="635.0" />
<Label layoutX="94.0" layoutY="14.0" prefHeight="60.0" prefWidth="482.0" text="Team Placeholder Themometer Interface">
<font>
<Font size="24.0" />
</font>
</Label>
<Button fx:id="connectButton" layoutX="475.0" layoutY="145.0" mnemonicParsing="false" onAction="#connectPressed" prefHeight="80.0" prefWidth="131.0" text="Connect">
<font>
<Font size="18.0" />
</font>
</Button>
<Button fx:id="setButton" layoutX="475.0" layoutY="241.0" mnemonicParsing="false" onAction="#setPressed" prefHeight="34.0" prefWidth="131.0" text="Set SMS" />
<Label layoutX="400.0" layoutY="314.0" prefHeight="27.0" prefWidth="131.0" text="Current Temperature" />
<ComboBox fx:id="comboBox" layoutX="475.0" layoutY="94.0" prefHeight="34.0" prefWidth="131.0" />
<ToggleButton fx:id="ledButton" layoutX="540.0" layoutY="360.0" mnemonicParsing="false" onAction="#ledPressed" prefHeight="34.0" prefWidth="109.0" text="Show on LEDs" />
<ToggleButton fx:id="modeButton" layoutX="540.0" layoutY="314.0" mnemonicParsing="false" onAction="#modePressed" prefHeight="34.0" prefWidth="109.0" text="In Fahrenheit" />
<Label fx:id="tempDisplay" layoutX="400.0" layoutY="341.0" prefHeight="39.0" prefWidth="131.0" textFill="#e10707">
<font>
<Font size="32.0" />
</font>
</Label>
</children>
界面的屏幕截图