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

Java 有没有办法缩短由于对象的相似属性而重复的代码?,java,arraylist,javafx-8,code-duplication,Java,Arraylist,Javafx 8,Code Duplication,我有许多复选框,所有复选框都依赖于某个未被勾选的框,如果它被勾选,则必须勾选并隐藏这些框。我知道人们一直在说ArrayList,但我不知道如何编辑ArrayList中项目的某些属性的语法 我的代码是有效的,我只需要将代码缩短,因为我相信如果它继续这样运行,最终会减慢进程,我想了解这对于我拥有的其他对象是如何起作用的 public void cbxSalesSelectA() { boolean t = cbx_SALES_Select_All.getText().equals("Sel

我有许多复选框,所有复选框都依赖于某个未被勾选的框,如果它被勾选,则必须勾选并隐藏这些框。我知道人们一直在说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");
    }
}

如果使用ArrayList或checkbox对象数组,则可以使用循环遍历所有复选框,并根据需要选中或取消选中它们

比如说,

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  extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception {
        // Creating an ArrayList here and adding all the checkboxes required. In your
        // case you'd add your existing checkboxes
        List<CheckBox> list = new ArrayList<>();
        list.add(new CheckBox());
        list.add(new CheckBox());
        list.add(new CheckBox());
        list.add(new CheckBox());

        // this combobox controls whether or not all checkboxes are selected
        ComboBox<String> comboBox = new ComboBox<>();

        // two options to either select all or deselect all checkboxes
        comboBox.setItems(FXCollections.observableArrayList("Select All", "Deselect All"));

        // container to hold all the controls
        VBox vBox = new VBox();
        vBox.getChildren().add(comboBox);
        vBox.getChildren().addAll(list);

        // this is the important bit
        // if the combobox selection is changed, then this fires
        comboBox.setOnAction(event -> {
            // if the selected option is select all, then a for-each loop is used to make all the 
            // checkboxes in the arraylist checked and vice versa if the deselect option is selected
            if (comboBox.getSelectionModel().getSelectedItem().equalsIgnoreCase("Select All")) {
                for (CheckBox checkBox : list) checkBox.setSelected(true);
            } else {
                for (CheckBox checkBox : list) checkBox.setSelected(false);
            }
        });

        primaryStage.setScene(new Scene(vBox));
        primaryStage.setTitle("Example");
        primaryStage.setWidth(600);
        primaryStage.setHeight(400);
        primaryStage.show();
    }
}
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.scene.scene;
导入javafx.scene.control.CheckBox;
导入javafx.scene.control.ComboBox;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
导入java.util.ArrayList;
导入java.util.List;
公共类示例扩展了应用程序{
@凌驾
public void start(Stage primaryStage)引发异常{
//在此处创建ArrayList并添加所需的所有复选框。在
//如果您要添加现有复选框
列表=新的ArrayList();
添加(新复选框());
添加(新复选框());
添加(新复选框());
添加(新复选框());
//此组合框控制是否选中所有复选框
ComboBox ComboBox=新建ComboBox();
//选择全部或取消选择所有复选框的两个选项
comboBox.setItems(FXCollections.observableArrayList(“全选”、“全选”));
//容器来容纳所有控件
VBox VBox=新的VBox();
vBox.getChildren().add(组合框);
vBox.getChildren().addAll(列表);
//这是重要的一点
//如果组合框选择发生更改,则会触发此操作
comboBox.setOnAction(事件->{
//如果所选选项为“全选”,则使用“for each”循环生成所有
//如果选择了“取消选择”选项,则选中arraylist中的复选框,反之亦然
if(comboBox.getSelectionModel().getSelectedItem().equalsIgnoreCase(“全选”)){
for(CheckBox:list)CheckBox.setSelected(true);
}否则{
for(CheckBox:list)CheckBox.setSelected(false);
}
});
设置场景(新场景(vBox));
primaryStage.setTitle(“示例”);
初级阶段。设置宽度(600);
初生阶段:坐骨高度(400);
primaryStage.show();
}
}

希望这能有所帮助。

如果使用ArrayList或checkbox对象数组,您可以使用循环遍历所有复选框,并根据需要选中或取消选中它们

比如说,

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  extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception {
        // Creating an ArrayList here and adding all the checkboxes required. In your
        // case you'd add your existing checkboxes
        List<CheckBox> list = new ArrayList<>();
        list.add(new CheckBox());
        list.add(new CheckBox());
        list.add(new CheckBox());
        list.add(new CheckBox());

        // this combobox controls whether or not all checkboxes are selected
        ComboBox<String> comboBox = new ComboBox<>();

        // two options to either select all or deselect all checkboxes
        comboBox.setItems(FXCollections.observableArrayList("Select All", "Deselect All"));

        // container to hold all the controls
        VBox vBox = new VBox();
        vBox.getChildren().add(comboBox);
        vBox.getChildren().addAll(list);

        // this is the important bit
        // if the combobox selection is changed, then this fires
        comboBox.setOnAction(event -> {
            // if the selected option is select all, then a for-each loop is used to make all the 
            // checkboxes in the arraylist checked and vice versa if the deselect option is selected
            if (comboBox.getSelectionModel().getSelectedItem().equalsIgnoreCase("Select All")) {
                for (CheckBox checkBox : list) checkBox.setSelected(true);
            } else {
                for (CheckBox checkBox : list) checkBox.setSelected(false);
            }
        });

        primaryStage.setScene(new Scene(vBox));
        primaryStage.setTitle("Example");
        primaryStage.setWidth(600);
        primaryStage.setHeight(400);
        primaryStage.show();
    }
}
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.scene.scene;
导入javafx.scene.control.CheckBox;
导入javafx.scene.control.ComboBox;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
导入java.util.ArrayList;
导入java.util.List;
公共类示例扩展了应用程序{
@凌驾
public void start(Stage primaryStage)引发异常{
//在此处创建ArrayList并添加所需的所有复选框。在
//如果您要添加现有复选框
列表=新的ArrayList();
添加(新复选框());
添加(新复选框());
添加(新复选框());
添加(新复选框());
//此组合框控制是否选中所有复选框
ComboBox ComboBox=新建ComboBox();
//选择全部或取消选择所有复选框的两个选项
comboBox.setItems(FXCollections.observableArrayList(“全选”、“全选”));
//容器来容纳所有控件
VBox VBox=新的VBox();
vBox.getChildren().add(组合框);
vBox.getChildren().addAll(列表);
//这是重要的一点
//如果组合框选择发生更改,则会触发此操作
comboBox.setOnAction(事件->{
//如果所选选项为“全选”,则使用“for each”循环生成所有
//如果选择了“取消选择”选项,则选中arraylist中的复选框,反之亦然
if(comboBox.getSelectionModel().getSelectedItem().equalsIgnoreCase(“全选”)){
for(CheckBox:list)CheckBox.setSelected(true);
}否则{
for(CheckBox:list)CheckBox.setSelected(false);
}
});
设置场景(新场景(vBox));
primaryStage.setTitle(“示例”);
初级阶段。设置宽度(600);
初生阶段:坐骨高度(400);
primaryStage.show();
}
}

希望这能有所帮助。

我不认为您真的可以提高代码的性能,但为了可读性,您可以使用单独的方法更改所选的
。比如说

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");
    }
}

我不认为您真的可以提高代码的性能,但是为了可读性,您可以用一种单独的方法对所选的
进行更改。比如说

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");
    }
}

您不太可能改变性能。但是,您可以使解决方案更具可读性。您不太可能改变性能。但是,您可以使解决方案更具可读性。