我有一个名为 columnsArray[] 的数组,它被预定义为包含 6 个字符串。当我运行我的方法 columns() 时,它应该用用户通过复选框选择的新字符串数组覆盖 columnsArray[]。我尝试实现的方式将每个选中的框添加到arrayList,然后将arrayList转换为array[]。但是,当代码运行时,columnsArray 不会被覆盖。
到目前为止,这是我的代码:
public class EditView {
private JFrame frame;
JCheckBox appNo, name, program, date, pName, country, fileLoc, email, uni,
countryUni, degree, classification, funding, supervisor,
rejectedBy, misc;
public ArrayList<String> columnArrLst;
public String[] columnsArray = { "Application Number", "Name", "Program",
"Date", "Project Name", "Country of Uni" };
public EditView() {
}
public void makeFrame() {
frame = new JFrame("Edit View");
frame.setPreferredSize(new Dimension(300, 350));
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(0, 2, 20, 20));
appNo = new JCheckBox("appNo");
name = new JCheckBox("Name");
program = new JCheckBox("Program");
date = new JCheckBox("Date");
pName = new JCheckBox("Project Title");
country = new JCheckBox("Country of Origin");
fileLoc = new JCheckBox("Current File Location");
// countryRef = new JCheckBox("");
email = new JCheckBox("Email address");
uni = new JCheckBox("Last University");
countryUni = new JCheckBox("Country of last Uni");
degree = new JCheckBox("Degree");
classification = new JCheckBox("Degree Classification");
funding = new JCheckBox("funding");
supervisor = new JCheckBox("Supervisor");
rejectedBy = new JCheckBox("Rejected By");
misc = new JCheckBox("Miscelaneous");
contentPane.add(appNo);
contentPane.add(name);
contentPane.add(program);
contentPane.add(date);
contentPane.add(pName);
contentPane.add(country);
contentPane.add(fileLoc);
contentPane.add(email);
contentPane.add(uni);
contentPane.add(countryUni);
contentPane.add(degree);
contentPane.add(classification);
contentPane.add(supervisor);
contentPane.add(rejectedBy);
contentPane.add(misc);
JButton changeView = new JButton("Change View");
changeView.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
columns();
frame.dispose();
}
});
contentPane.add(changeView);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public String[] columns() {
columnArrLst = new ArrayList<String>();
if (appNo.isSelected()) {
columnArrLst.add("AppNo");
}
if (name.isSelected()) {
columnArrLst.add("Name");
}
if (date.isSelected()) {
columnArrLst.add("Date");
}
if (fileLoc.isSelected()) {
columnArrLst.add("file Location");
}
if (country.isSelected()) {
columnArrLst.add("Country");
}
if (email.isSelected()) {
columnArrLst.add("Email");
}
if (uni.isSelected()) {
columnArrLst.add("University");
}
if (countryUni.isSelected()) {
columnArrLst.add("Country of Uni");
}
if (degree.isSelected()) {
columnArrLst.add("Degree");
}
if (classification.isSelected()) {
columnArrLst.add("Degree Classification");
}
if (pName.isSelected()) {
columnArrLst.add("ProjectName");
}
if (funding.isSelected()) {
columnArrLst.add("Funding");
}
if (supervisor.isSelected()) {
columnArrLst.add("Supervisor");
}
if (rejectedBy.isSelected()) {
columnArrLst.add("rejected By");
}
if (misc.isSelected()) {
columnArrLst.add("Miscelaneous");
}
columnsArray = new String[columnArrLst.size()];
columnArrLst.toArray(columnsArray);
return columnsArray;
}
}
任何想法为什么它不被覆盖?谢谢你的帮助。