1 回答

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
您需要以某種方式將一個(gè)字段與一個(gè)字符串關(guān)聯(lián)起來(lái)。這需要您為每個(gè)TextFields 添加一些代碼,無(wú)論是userData在 fxml 中設(shè)置 ,還是將TextField和的組合存儲(chǔ)String在控制器initialize方法中合適的數(shù)據(jù)結(jié)構(gòu)中。
這樣的數(shù)據(jù)結(jié)構(gòu)可以是LinkedHashMap:
private final Map<TextField, String> fieldStrings = new LinkedHashMap<>();
@FXML
private void initialize() {
fieldStrings.put(desShear, "Design shear");
fieldStrings.put(boltSize, "Bolt size");
fieldStrings.put(tensStrengthBolt, "Bolt strength");
fieldStrings.put(noBolts, "Number of bolts");
fieldStrings.put(shearPlanes, "Number of shear planes");
fieldStrings.put(edgeDist, "Edge distance");
fieldStrings.put(plyThick, "Ply thickness");
fieldStrings.put(tensStrengthPly, "Ply strength");
fieldStrings.put(phiBolt, "Bolt phi factor");
fieldStrings.put(phiPly, "Ply phi factor");
}
private double getFieldValue(TextField field) {
return Double.parseDouble(field.getText());
}
public void run(ActionEvent clickRun) {
String errorField = fieldStrings.entrySet().stream()
.filter(entry -> entry.getKey().getText().isBlank())
.map(Map.Entry::getValue)
.findFirst().orElse(null);
if (errorField != null) {
outputMsg.setText(errorField + " field is blank");
return;
}
//Get field values
double desSHEAR = getFieldValue(desShear);
double boltSIZE = getFieldValue(boltSize);
double tensStengthBOLT = getFieldValue(tensStrengthBolt);
double noBOLTS = getFieldValue(noBolts);
double shearPLANES = getFieldValue(shearPlanes);
double edgeDIST = getFieldValue(edgeDist);
double plyTHICK = getFieldValue(plyThick);
double tensStrengthPLY = getFieldValue(tensStrengthPly);
double phiBOLT = getFieldValue(phiBolt);
double phiPLY = getFieldValue(phiPly);
//Bolt shear calculation
}
添加回答
舉報(bào)