当选中多个复选框时如何执行操作?

问题描述 投票:2回答:1

[当用户选中多个复选框时,我需要使用相同的名称执行以下操作。如何为此使用for循环条件?

如果只有一个选择被执行,我试图使用if-else。有什么方法可以实现for循环以减少代码长度?

我正在使用JFrame绝对布局,因此复选框变量已经预先定义。我无法使用数组。

public first() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 512, 263);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("Checkout Suite:");
    lblNewLabel.setBounds(10, 11, 104, 20);
    contentPane.add(lblNewLabel);

    JComboBox comboBox = new JComboBox();
    comboBox.addItem("SNS Legacy checkout - Full Run - Desktop");
    comboBox.addItem("SNS Legacy checkout - Sanity Run - Mobile");
    comboBox.setBounds(124, 11, 331, 20);
    contentPane.add(comboBox);          

    JLabel lblNewLabel_1 = new JLabel("Locales:");
    lblNewLabel_1.setBounds(10, 53, 79, 34);
    contentPane.add(lblNewLabel_1);

    JCheckBox chckbxNewCheckBox = new JCheckBox("US");
    chckbxNewCheckBox.setBounds(17, 94, 63, 23);
    contentPane.add(chckbxNewCheckBox);

    JCheckBox chckbxNewCheckBox_1 = new JCheckBox("UK");
    chckbxNewCheckBox_1.setBounds(104, 94, 63, 23);
    contentPane.add(chckbxNewCheckBox_1);

    JCheckBox chckbxNewCheckBox_2 = new JCheckBox("IN");
    chckbxNewCheckBox_2.setBounds(177, 94, 72, 23);
    contentPane.add(chckbxNewCheckBox_2);

    JCheckBox chckbxNewCheckBox_3 = new JCheckBox("JP");
    chckbxNewCheckBox_3.setBounds(17, 133, 63, 23);
    contentPane.add(chckbxNewCheckBox_3);

    JCheckBox chckbxNewCheckBox_4 = new JCheckBox("FR");
    chckbxNewCheckBox_4.setBounds(104, 133, 63, 23);
    contentPane.add(chckbxNewCheckBox_4);

    JCheckBox chckbxNewCheckBox_5 = new JCheckBox("DE");
    chckbxNewCheckBox_5.setBounds(177, 133, 72, 23);
    contentPane.add(chckbxNewCheckBox_5);

    JCheckBox chckbxNewCheckBox_6 = new JCheckBox("ES");
    chckbxNewCheckBox_6.setBounds(17, 174, 63, 23);
    contentPane.add(chckbxNewCheckBox_6);

    JCheckBox chckbxNewCheckBox_7 = new JCheckBox("IT");
    chckbxNewCheckBox_7.setBounds(104, 174, 63, 23);
    contentPane.add(chckbxNewCheckBox_7);

    JCheckBox chckbxNewCheckBox_8 = new JCheckBox("CA");
    chckbxNewCheckBox_8.setBounds(177, 174, 72, 23);
    contentPane.add(chckbxNewCheckBox_8);

    JButton btnNewButton = new JButton("Create Test Run");
    btnNewButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {


            if(comboBox.getSelectedItem().equals("SNS Legacy checkout - Full Run - Desktop"))
            {
                System.setProperty("webdriver.chrome.driver","C:\\Users\\marmga\\Documents\\MARMGA\\My folder\\free_screen_recorder\\chromedriver_win32\\chromedriver.exe");
                WebDriver aru=new ChromeDriver();
                aru.manage().window().maximize();
                aru.get("https://testrail.labcollab.net/testrail/index.php?/suites/overview/124");

                //Testrail Login
                aru.findElement(By.id("name")).sendKeys("[email protected]");
                aru.findElement(By.id("password")).sendKeys("xyz");
                aru.findElement(By.id("button_primary")).click(); //Login
                //aru.findElement(By.xpath("//*[@id=\"button_primary\"]/span")).click();
                //aru.findElement(By.className("loginpage-button-sso-disable loginpage-button-sso-disable-hover  loginpage-button-sso-disable-active")).click();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                aru.findElement(By.xpath("//*[@id=\"suite-22496\"]/div[2]/div[1]/a")).click();
                aru.findElement(By.xpath("//*[@id=\"content-header\"]/div/div[2]/a[1]")).click();
                aru.findElement(By.xpath("//*[@id=\"includeSpecific\"]")).click();
                aru.findElement(By.xpath("//*[@id=\"includeSpecificInfo\"]/a")).click();
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                aru.findElement(By.xpath("//*[@id=\"selectCasesNode-744350\"]/input")).click();
                aru.findElement(By.id("selectCasesSubmit")).click();

                //Suite Name
                aru.findElement(By.id("name")).clear();
                DateFormat today = new SimpleDateFormat("MM/dd/yyyy ");
                Date palani = new Date();
                String content= today.format(palani);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
                if(chckbxNewCheckBox.isSelected())
                {

                    aru.findElement(By.id("name")).sendKeys("SNS - Checkout Preflight - Desktop - US - "+content);
                    //aru.findElement(By.className("button button-left button-positive button-ok")).click();

                }

                else if(chckbxNewCheckBox_1.isSelected())
                {
                    aru.findElement(By.id("name")).sendKeys("SNS - Checkout Preflight - Desktop - UK - "+content);
                    //aru.findElement(By.className("button button-left button-positive button-ok")).click();
                }

                else if(chckbxNewCheckBox_2.isSelected())
                {
                    aru.findElement(By.id("name")).sendKeys("SNS - Checkout Preflight - Desktop - IN - "+content);
                    //aru.findElement(By.className("button button-left button-positive button-ok")).click();
                }
                else if(chckbxNewCheckBox_3.isSelected())
                {
                    aru.findElement(By.id("name")).sendKeys("SNS - Checkout Preflight - Desktop - JP - "+content);
                    //aru.findElement(By.className("button button-left button-positive button-ok")).click();
                }

                else if(chckbxNewCheckBox_4.isSelected())
                {
                    aru.findElement(By.id("name")).sendKeys("SNS - Checkout Preflight - Desktop - FR - "+content);
                    //aru.findElement(By.className("button button-left button-positive button-ok")).click();
                }
                else if(chckbxNewCheckBox_5.isSelected())
                {
                    aru.findElement(By.id("name")).sendKeys("SNS - Checkout Preflight - Desktop - DE - "+content);
                    //aru.findElement(By.className("button button-left button-positive button-ok")).click();
                }
                else if(chckbxNewCheckBox_6.isSelected())
                {
                    aru.findElement(By.id("name")).sendKeys("SNS - Checkout Preflight - Desktop - ES - "+content);
                    //aru.findElement(By.className("button button-left button-positive button-ok")).click();
                }
                else if(chckbxNewCheckBox_7.isSelected())
                {
                    aru.findElement(By.id("name")).sendKeys("SNS - Checkout Preflight - Desktop - IT - "+content);
                    //aru.findElement(By.className("button button-left button-positive button-ok")).click();
                }
                else if(chckbxNewCheckBox_8.isSelected())
                {
                    aru.findElement(By.id("name")).sendKeys("SNS - Checkout Preflight - Desktop - CA - "+content);
                    //aru.findElement(By.className("button button-left button-positive button-ok")).click();
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Please select from list");
                }

            }


        }
    });

    btnNewButton.setBounds(285, 122, 169, 34);
    contentPane.add(btnNewButton);
}
}
java swing jframe jcheckbox
1个回答
0
投票

如果使用else if,则一旦if返回true,它将停止执行更多的案例。一种可能会减少代码的方法是将所有复选框添加到ArrayList,然后循环遍历。

    List<JCheckBox> cbList = new ArrayList<>();

    JCheckBox chckbxNewCheckBox = new JCheckBox("US");
    chckbxNewCheckBox.setBounds(17, 94, 63, 23);
    contentPane.add(chckbxNewCheckBox);
    cbList.add(chckbxNewCheckBox);

    JCheckBox chckbxNewCheckBox_1 = new JCheckBox("UK");
    chckbxNewCheckBox_1.setBounds(104, 94, 63, 23);
    contentPane.add(chckbxNewCheckBox_1);
    cbList.add(chckbxNewCheckBox_1);

    JCheckBox chckbxNewCheckBox_2 = new JCheckBox("IN");
    chckbxNewCheckBox_2.setBounds(177, 94, 72, 23);
    contentPane.add(chckbxNewCheckBox_2);
    cbList.add(chckbxNewCheckBox_2);

    JCheckBox chckbxNewCheckBox_3 = new JCheckBox("JP");
    chckbxNewCheckBox_3.setBounds(17, 133, 63, 23);
    contentPane.add(chckbxNewCheckBox_3);
    cbList.add(chckbxNewCheckBox_3);

    JCheckBox chckbxNewCheckBox_4 = new JCheckBox("FR");
    chckbxNewCheckBox_4.setBounds(104, 133, 63, 23);
    contentPane.add(chckbxNewCheckBox_4);
    cbList.add(chckbxNewCheckBox_4);

    JCheckBox chckbxNewCheckBox_5 = new JCheckBox("DE");
    chckbxNewCheckBox_5.setBounds(177, 133, 72, 23);
    contentPane.add(chckbxNewCheckBox_5);
    cbList.add(chckbxNewCheckBox_5);

    JCheckBox chckbxNewCheckBox_6 = new JCheckBox("ES");
    chckbxNewCheckBox_6.setBounds(17, 174, 63, 23);
    contentPane.add(chckbxNewCheckBox_6);
    cbList.add(chckbxNewCheckBox_6);

    JCheckBox chckbxNewCheckBox_7 = new JCheckBox("IT");
    chckbxNewCheckBox_7.setBounds(104, 174, 63, 23);
    contentPane.add(chckbxNewCheckBox_7);
    cbList.add(chckbxNewCheckBox_7);

    JCheckBox chckbxNewCheckBox_8 = new JCheckBox("CA");
    chckbxNewCheckBox_8.setBounds(177, 174, 72, 23);
    contentPane.add(chckbxNewCheckBox_8);
    cbList.add(chckbxNewCheckBox_8);

[Alexeys的评论让我考虑是否要检查任何复选框,这可能在一开始就很有用。

private boolean selectionWasMade()
{
    int selectedCounter = 0;

    for(JCheckBox checkbox : cbList)
    {
        if(checkbox.isSelected())
        {
            selectedCounter++;
        }
    }

    if(selectedCounter != 0)
    {
        return true;
    }
    return false;
}

现在您只需检查它是否返回true并执行此循环:

if(selectionWasMade())
{
    for(JCheckBox checkbox : cbList)
    {
        if(checkbox.isSelected())
        {
            aru.findElement(By.id("name")).sendKeys("SNS - Checkout Preflight - Desktop - " + checkbox.getText() + " - "+content);
        }
    }
}
else
{
    JOptionPane.showMessageDialog(null, "Please select from list");
}

希望我能为您提供帮助,我的答案应该在几处修改后全部清理并纠正!

© www.soinside.com 2019 - 2024. All rights reserved.