我想比较一下列表是否相同。无论结果如何,我都需要通知用户他们的列表是否比较。这是我做过的第一个GUI,所以我对如何对它们进行编程仍然很新。我有我需要的文本字段但是如何在选择比较按钮后测试用户输入?这是我的代码:
JLabel lblNewLabel = new JLabel("Identical Lists");
panel.add(lblNewLabel);
txtEnterListOne = new JTextField();
txtEnterListOne.setText("Enter List One: ");
txtEnterListOne.setBounds(22, 50, 205, 24);
contentPane.add(txtEnterListOne);
txtEnterListOne.setColumns(10);
int size1 = Integer.parseInt(txtEnterListOne.getText());
int[] list1 = new int[size1];
for (int i = 0; i < list1.length; i++)
list1[i] = Integer.parseInt(txtEnterListOne.getText());
txtEnterListTwo = new JTextField();
txtEnterListTwo.setText("Enter List Two:");
txtEnterListTwo.setBounds(22, 107, 205, 24);
contentPane.add(txtEnterListTwo);
txtEnterListTwo.setColumns(10);
int size2 = Integer.parseInt(txtEnterListTwo.getText());
int[] list2 = new int[size2];
for (int i = 0; i < list2.length; i++)
list2[i] = Integer.parseInt(txtEnterListTwo.getText());
JButton btnCompareLists = new JButton("Compare Lists");
btnCompareLists.setBounds(162, 162, 134, 48);
contentPane.add(btnCompareLists);
}
public static boolean equals(int[] list1, int[] list2) {
if (list1.length != list2.length)
return false;
for (int i = 0; i < list1.length; i++)
if (list1[i] != list2[i])
return false;
return true;
}
}