有没有办法缩短由于对象属性相似而重复的代码?

我有许多复选框,所有这些复选框都依赖于某个未选中的框,但如果确实选中了该框,则必须选中并隐藏该框。我知道人们一直在说 ArrayList,但是我不知道如何编辑 ArrayList 中项目的某些属性的语法。


我的代码可以工作,我只需要这段代码更短,因为我相信如果它继续像这样运行,最终会减慢进程,并且我想了解这对于我有相同功能的其他对象是如何工作的。


 public void cbxSalesSelectA() {

    boolean t = cbx_SALES_Select_All.getText().equals("Select All");

    cbx_SALESQtySold.setSelected(t);

    cbx_SALESDateSold.setSelected(t);

    cbx_SALESCustomer.setSelected(t);

    cbx_SALESDiscount.setSelected(t);

    cbx_SALESLineNumber.setSelected(t);

    cbx_SALESConsultant.setSelected(t);

    cbx_SALES_Header_Row.setSelected(t);

    if (t) {

        cbx_SALES_Select_All.setText("Deselect All");

    } else {

        cbx_SALES_Select_All.setText("Select All");

    }

}

 public void cbxLOCSelectA() {

    boolean t = cbx_LOC_Select_All.getText().equals("Select All");

    cbx_LOCHeight.setSelected(t);

    cbx_LOCWidth.setSelected(t);

    cbx_LOCDepth.setSelected(t);

    cbx_LOCWeightCap.setSelected(t);

    cbx_LOCAccessibility.setSelected(t);

    cbx_LOC_Header_Row.setSelected(t);

    if (t) {

        cbx_LOC_Select_All.setText("Deselect All");

    } else {

        cbx_LOC_Select_All.setText("Select All");

    }

}


慕尼黑8549860
浏览 77回答 2
2回答

素胚勾勒不出你

selected我不认为你真的可以提高代码的性能,但为了可读性,你可以在单独的方法中进行更改。例如static void allSetSelected(boolean isSelected, CheckBox... boxes ) {    Arrays.stream(boxes).forEach(b -> b.setSelected(isSelected));}并像这样在您的代码中使用它public void cbxSalesSelectA() {    boolean t = cbx_SALES_Select_All.getText().equals("Select All");    allSetSelected(t, cbx_SALESQtySold,                      cbx_SALESDateSold,                      cbx_SALESCustomer,                      cbx_SALESDiscount,                      cbx_SALESLineNumber,                      cbx_SALESConsultant,                      cbx_SALES_Header_Row)    if (t) {        cbx_SALES_Select_All.setText("Deselect All");    } else {        cbx_SALES_Select_All.setText("Select All");    }}public void cbxLOCSelectA() {    boolean t = cbx_LOC_Select_All.getText().equals("Select All");    allSetSelected(t, cbx_LOCHeight, cbx_LOCWidth, cbx_LOCDepth, cbx_LOCWeightCap, cbx_LOCAccessibility, cbx_LOC_Header_Row);    if (t) {        cbx_LOC_Select_All.setText("Deselect All");    } else {        cbx_LOC_Select_All.setText("Select All");    }}

慕尼黑5688855

如果您使用 ArrayList 或复选框对象数组,则只需使用循环来遍历所有复选框并根据需要选中或取消选中它们。例如,import javafx.application.Application;import javafx.collections.FXCollections;import javafx.scene.Scene;import javafx.scene.control.CheckBox;import javafx.scene.control.ComboBox;import javafx.scene.layout.VBox;import javafx.stage.Stage;import java.util.ArrayList;import java.util.List;public class Example&nbsp; extends Application {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage primaryStage) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; // Creating an ArrayList here and adding all the checkboxes required. In your&nbsp; &nbsp; &nbsp; &nbsp; // case you'd add your existing checkboxes&nbsp; &nbsp; &nbsp; &nbsp; List<CheckBox> list = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; list.add(new CheckBox());&nbsp; &nbsp; &nbsp; &nbsp; list.add(new CheckBox());&nbsp; &nbsp; &nbsp; &nbsp; list.add(new CheckBox());&nbsp; &nbsp; &nbsp; &nbsp; list.add(new CheckBox());&nbsp; &nbsp; &nbsp; &nbsp; // this combobox controls whether or not all checkboxes are selected&nbsp; &nbsp; &nbsp; &nbsp; ComboBox<String> comboBox = new ComboBox<>();&nbsp; &nbsp; &nbsp; &nbsp; // two options to either select all or deselect all checkboxes&nbsp; &nbsp; &nbsp; &nbsp; comboBox.setItems(FXCollections.observableArrayList("Select All", "Deselect All"));&nbsp; &nbsp; &nbsp; &nbsp; // container to hold all the controls&nbsp; &nbsp; &nbsp; &nbsp; VBox vBox = new VBox();&nbsp; &nbsp; &nbsp; &nbsp; vBox.getChildren().add(comboBox);&nbsp; &nbsp; &nbsp; &nbsp; vBox.getChildren().addAll(list);&nbsp; &nbsp; &nbsp; &nbsp; // this is the important bit&nbsp; &nbsp; &nbsp; &nbsp; // if the combobox selection is changed, then this fires&nbsp; &nbsp; &nbsp; &nbsp; comboBox.setOnAction(event -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if the selected option is select all, then a for-each loop is used to make all the&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // checkboxes in the arraylist checked and vice versa if the deselect option is selected&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (comboBox.getSelectionModel().getSelectedItem().equalsIgnoreCase("Select All")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (CheckBox checkBox : list) checkBox.setSelected(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (CheckBox checkBox : list) checkBox.setSelected(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setScene(new Scene(vBox));&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setTitle("Example");&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setWidth(600);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setHeight(400);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.show();&nbsp; &nbsp; }}希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java