当用户选择多个复选框时,我需要使用相同的名称执行以下操作。如何为此使用 for 循环条件?
我尝试使用 if-else if 但只有一个选择被执行。有没有办法实现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("Platform - Desktop");
comboBox.addItem("Platform - 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("A");
chckbxNewCheckBox.setBounds(17, 94, 63, 23);
contentPane.add(chckbxNewCheckBox);
JCheckBox chckbxNewCheckBox_1 = new JCheckBox("B");
chckbxNewCheckBox_1.setBounds(104, 94, 63, 23);
contentPane.add(chckbxNewCheckBox_1);
JCheckBox chckbxNewCheckBox_2 = new JCheckBox("C");
chckbxNewCheckBox_2.setBounds(177, 94, 72, 23);
contentPane.add(chckbxNewCheckBox_2);
JCheckBox chckbxNewCheckBox_3 = new JCheckBox("D");
chckbxNewCheckBox_3.setBounds(17, 133, 63, 23);
contentPane.add(chckbxNewCheckBox_3);
JCheckBox chckbxNewCheckBox_4 = new JCheckBox("E");
chckbxNewCheckBox_4.setBounds(104, 133, 63, 23);
contentPane.add(chckbxNewCheckBox_4);
JCheckBox chckbxNewCheckBox_5 = new JCheckBox("F");
chckbxNewCheckBox_5.setBounds(177, 133, 72, 23);
contentPane.add(chckbxNewCheckBox_5);
JCheckBox chckbxNewCheckBox_6 = new JCheckBox("G");
chckbxNewCheckBox_6.setBounds(17, 174, 63, 23);
contentPane.add(chckbxNewCheckBox_6);
JCheckBox chckbxNewCheckBox_7 = new JCheckBox("H");
chckbxNewCheckBox_7.setBounds(104, 174, 63, 23);
contentPane.add(chckbxNewCheckBox_7);
JCheckBox chckbxNewCheckBox_8 = new JCheckBox("I");
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("PLatform - Desktop"))
{
System.setProperty("webdriver.chrome.driver","\\chromedriver_win32\\chromedriver.exe");
WebDriver aru=new ChromeDriver();
aru.manage().window().maximize();
aru.get("https://google.com");
//Testrail Login
aru.findElement(By.id("name")).sendKeys("gmail@gmail.com");
aru.findElement(By.id("password")).sendKeys("xyz");
aru.findElement(By.id("button_primary")).click(); //Login
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("Platform - Desktop - A - "+content);
//aru.findElement(By.className("button button-left button-positive button-ok")).click();
}
else if(chckbxNewCheckBox_1.isSelected())
{
aru.findElement(By.id("name")).sendKeys("Platform - Desktop - B - "+content);
//aru.findElement(By.className("button button-left button-positive button-ok")).click();
}
else if(chckbxNewCheckBox_2.isSelected())
{
aru.findElement(By.id("name")).sendKeys("Platform - Desktop - C - "+content);
//aru.findElement(By.className("button button-left button-positive button-ok")).click();
}
else if(chckbxNewCheckBox_3.isSelected())
{
aru.findElement(By.id("name")).sendKeys("Platform - Desktop - D - "+content);
//aru.findElement(By.className("button button-left button-positive button-ok")).click();
}
else if(chckbxNewCheckBox_4.isSelected())
{
aru.findElement(By.id("name")).sendKeys("Platform - Desktop - E - "+content);
//aru.findElement(By.className("button button-left button-positive button-ok")).click();
}
else if(chckbxNewCheckBox_5.isSelected())
{
aru.findElement(By.id("name")).sendKeys("Platform - Desktop - F - "+content);
//aru.findElement(By.className("button button-left button-positive button-ok")).click();
}
else if(chckbxNewCheckBox_6.isSelected())
{
aru.findElement(By.id("name")).sendKeys("Platform - Desktop - G - "+content);
//aru.findElement(By.className("button button-left button-positive button-ok")).click();
}
else if(chckbxNewCheckBox_7.isSelected())
{
aru.findElement(By.id("name")).sendKeys("Platform - Desktop - H - "+content);
//aru.findElement(By.className("button button-left button-positive button-ok")).click();
}
else if(chckbxNewCheckBox_8.isSelected())
{
aru.findElement(By.id("name")).sendKeys("Platform - Desktop - I - "+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);
}
}
请您参考如下方法:
如果您使用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);
Alexey 的评论让我考虑检查是否选择了任何复选框,这可能对首先检查很有用。
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");
}
希望我能提供帮助,经过几次编辑后,我的答案现在应该已经全部清理干净并正确了!