是否有堆叠 if 的替代方法来检查 javafx 应用程序中的许多空白字段?
Is there an alternative to stacked if's for checking for many blank fields in javafx app?
我是 java(和一般编程)的新手,正在通过将基本电子表格转换为 javafx 应用程序来学习。
为此,我正在使用:
Java & JavaFX 12
GUI 的 FXML 和场景构建器
大约有 10 个输入字段,它们不能为空(应用程序崩溃,因为 getText 似乎在空白字段上失败)。
我编写了堆栈式 if 语句来检查是否有空白字段,如果是,则打印错误消息,并使用 return 停止进程而不会使应用程序崩溃。
Switch 语句似乎并不比 if 语句好多少。
有没有办法用更少的代码行来做到这一点?
package SteelDesign_BoltedConnection;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextArea;
import javafx.event.ActionEvent;
public class mainController {
//Header details
@FXML private TextField refNo;
@FXML private TextField jobDesc;
@FXML private TextField author;
@FXML private DatePicker date;
//Design data
@FXML private TextField desShear;
@FXML private TextField boltSize;
@FXML private TextField boltGrade;
@FXML private TextField tensStrengthBolt;
@FXML private TextField noBolts;
@FXML private TextField shearPlanes;
@FXML private TextField edgeDist;
@FXML private TextField plyThick;
@FXML private TextField tensStrengthPly;
//Constants
@FXML private TextField phiBolt;
@FXML private TextField phiPly;
//Results - Bolt Shear
@FXML private TextField boltDesShear;
@FXML private TextField boltCap;
@FXML private TextField loadFactorBolt;
//Results - Ply Shear & Bearing
@FXML private TextField plyDesShear;
@FXML private TextField plyCap;
@FXML private TextField loadFactorPly;
//Output messages
@FXML private TextArea outputMsg;
public void run(ActionEvent clickRun) {
String outputMSG;
//Check fields are populated
if(desShear.getText().isBlank()) {
outputMSG = "Design shear field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(boltSize.getText().isBlank()) {
outputMSG = "Bolt size field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(tensStrengthBolt.getText().isBlank()) {
outputMSG = "Bolt strength field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(noBolts.getText().isBlank()) {
outputMSG = "Number of bolts field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(shearPlanes.getText().isBlank()) {
outputMSG = "Number of shear planes field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(edgeDist.getText().isBlank()) {
outputMSG = "Edge distance field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(plyThick.getText().isBlank()) {
outputMSG = "Ply thickness field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(tensStrengthPly.getText().isBlank()) {
outputMSG = "Ply strength field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(phiBolt.getText().isBlank()) {
outputMSG = "Bolt phi factor field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(phiPly.getText().isBlank()) {
outputMSG = "Ply phi factor field is blank";
outputMsg.setText(outputMSG);
return;
}
//Get field values
double desSHEAR = Double.parseDouble(desShear.getText());
double boltSIZE = Double.parseDouble(boltSize.getText());
double tensStengthBOLT = Double.parseDouble(tensStrengthBolt.getText());
double noBOLTS = Double.parseDouble(noBolts.getText());
double shearPLANES = Double.parseDouble(shearPlanes.getText());
double edgeDIST = Double.parseDouble(edgeDist.getText());
double plyTHICK = Double.parseDouble(plyThick.getText());
double tensStrengthPLY = Double.parseDouble(tensStrengthPly.getText());
double phiBOLT = Double.parseDouble(phiBolt.getText());
double phiPLY = Double.parseDouble(phiPly.getText());
//Bolt shear calculation
}
}
您需要以某种方式将一个字段与一个字符串相关联。这需要您为每个 TextField
添加一些代码,无论是在 fxml 中设置 userData
还是在合适的数据中存储 TextField
和 String
的组合控制器的 initialize
方法中的结构。
这样的数据结构可以是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
}
我是 java(和一般编程)的新手,正在通过将基本电子表格转换为 javafx 应用程序来学习。 为此,我正在使用: Java & JavaFX 12 GUI 的 FXML 和场景构建器
大约有 10 个输入字段,它们不能为空(应用程序崩溃,因为 getText 似乎在空白字段上失败)。
我编写了堆栈式 if 语句来检查是否有空白字段,如果是,则打印错误消息,并使用 return 停止进程而不会使应用程序崩溃。
Switch 语句似乎并不比 if 语句好多少。
有没有办法用更少的代码行来做到这一点?
package SteelDesign_BoltedConnection;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextArea;
import javafx.event.ActionEvent;
public class mainController {
//Header details
@FXML private TextField refNo;
@FXML private TextField jobDesc;
@FXML private TextField author;
@FXML private DatePicker date;
//Design data
@FXML private TextField desShear;
@FXML private TextField boltSize;
@FXML private TextField boltGrade;
@FXML private TextField tensStrengthBolt;
@FXML private TextField noBolts;
@FXML private TextField shearPlanes;
@FXML private TextField edgeDist;
@FXML private TextField plyThick;
@FXML private TextField tensStrengthPly;
//Constants
@FXML private TextField phiBolt;
@FXML private TextField phiPly;
//Results - Bolt Shear
@FXML private TextField boltDesShear;
@FXML private TextField boltCap;
@FXML private TextField loadFactorBolt;
//Results - Ply Shear & Bearing
@FXML private TextField plyDesShear;
@FXML private TextField plyCap;
@FXML private TextField loadFactorPly;
//Output messages
@FXML private TextArea outputMsg;
public void run(ActionEvent clickRun) {
String outputMSG;
//Check fields are populated
if(desShear.getText().isBlank()) {
outputMSG = "Design shear field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(boltSize.getText().isBlank()) {
outputMSG = "Bolt size field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(tensStrengthBolt.getText().isBlank()) {
outputMSG = "Bolt strength field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(noBolts.getText().isBlank()) {
outputMSG = "Number of bolts field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(shearPlanes.getText().isBlank()) {
outputMSG = "Number of shear planes field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(edgeDist.getText().isBlank()) {
outputMSG = "Edge distance field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(plyThick.getText().isBlank()) {
outputMSG = "Ply thickness field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(tensStrengthPly.getText().isBlank()) {
outputMSG = "Ply strength field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(phiBolt.getText().isBlank()) {
outputMSG = "Bolt phi factor field is blank";
outputMsg.setText(outputMSG);
return;
}
else if(phiPly.getText().isBlank()) {
outputMSG = "Ply phi factor field is blank";
outputMsg.setText(outputMSG);
return;
}
//Get field values
double desSHEAR = Double.parseDouble(desShear.getText());
double boltSIZE = Double.parseDouble(boltSize.getText());
double tensStengthBOLT = Double.parseDouble(tensStrengthBolt.getText());
double noBOLTS = Double.parseDouble(noBolts.getText());
double shearPLANES = Double.parseDouble(shearPlanes.getText());
double edgeDIST = Double.parseDouble(edgeDist.getText());
double plyTHICK = Double.parseDouble(plyThick.getText());
double tensStrengthPLY = Double.parseDouble(tensStrengthPly.getText());
double phiBOLT = Double.parseDouble(phiBolt.getText());
double phiPLY = Double.parseDouble(phiPly.getText());
//Bolt shear calculation
}
}
您需要以某种方式将一个字段与一个字符串相关联。这需要您为每个 TextField
添加一些代码,无论是在 fxml 中设置 userData
还是在合适的数据中存储 TextField
和 String
的组合控制器的 initialize
方法中的结构。
这样的数据结构可以是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
}