以下是我设置的系统,用于跟踪添加到arraylist的面板数量,并在不再需要时删除这些面板。基本上我需要帮助的部分是同时使用JButton的Item / Actionlistener和JCheckBox。我设法存储动态创建的包含arraylist中的textfield和checkboxes的rowpanel,但是将它应用于itemlistener或actionlistener有点令人困惑。我希望删除按钮只删除选中的复选框,但由于checkitem变量超出其范围,我得到一个空指针异常。我觉得如果我能找到解决方法,我相信传递一个局部变量作为参数,我觉得我的删除按钮会起作用。另外我知道rowPanel是一个全局变量是另一个因素,抛出我的所有引用,但删除它,并使其本地我最终与我的删除按钮相同的问题。我的Itemlistener有效,因为我想确保它可以跟踪检查哪些复选框并打印出数组的大小。然而我的另一个问题是我必须选择使用哪个特定的听众,因为我已经阅读过我可以将这两种方法结合在一起吗?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class UI extends JFrame implements ItemListener {
JPanel mainPanel, infoPanel, txtFldNames, txtFlds, bttn, checkBox, rowPanel;
JTextField name, checkOutDate, checkInDate, email;
JButton add, delete;
JLabel label1, label2, label3, label4, label5;
JScrollPane scrollBar;
ArrayList<Component> checkBoxArrLst = new ArrayList<Component>();
public UI() {
txtFldNames = new JPanel(new GridLayout(1, 4, 5, 5));
label1 = new JLabel("Name", SwingConstants.CENTER);
label2 = new JLabel("Check Out Date", SwingConstants.CENTER);
label3 = new JLabel("Check In Date", SwingConstants.CENTER);
label4 = new JLabel("Email", SwingConstants.CENTER);
label5 = new JLabel("", SwingConstants.CENTER); // empty label used as a
// place holder for the
// checkbox
txtFldNames.add(label1);
txtFldNames.add(label2);
txtFldNames.add(label3);
txtFldNames.add(label4);
txtFldNames.add(label5);
add = new JButton("Add");
add.addActionListener(e -> {
JPanel rowPanel = new JPanel(new GridLayout(1, 4, 5, 5));
JCheckBox checkItem = new JCheckBox();
checkItem.addItemListener(this);
for (int i = 0; i < 4; i++) {
rowPanel.add(new JTextField());
}
for (int l = 0; l < 1; l++) {
rowPanel.add(checkItem, BorderLayout.EAST);
}
checkBoxArrLst.add(rowPanel);
infoPanel.add(rowPanel);
infoPanel.revalidate();
infoPanel.repaint();
});
delete = new JButton("Delete");
delete.addActionListener(e -> {
Component[] components = rowPanel.getComponents();
for (Component c : components) {
if (c instanceof JCheckBox) {
JCheckBox checkItem = (JCheckBox) c;
if (((JCheckBox) c).isSelected()) {
infoPanel.remove(checkItem);
}
}
}
infoPanel.revalidate();
infoPanel.repaint();
});
bttn = new JPanel(new FlowLayout(FlowLayout.CENTER));
bttn.add(add);
bttn.add(delete);
name = new JTextField();
checkOutDate = new JTextField();
checkInDate = new JTextField();
email = new JTextField();
name.setColumns(20);
checkOutDate.setColumns(20);
checkInDate.setColumns(20);
email.setColumns(20);
txtFlds = new JPanel(new GridLayout(1, 4, 5, 5));
infoPanel = new JPanel(new GridLayout(0, 1, 2, 2));
infoPanel.add(txtFldNames);
infoPanel.add(txtFlds);
mainPanel = new JPanel(new BorderLayout(5, 5));
mainPanel.add(infoPanel, BorderLayout.PAGE_START);
scrollBar = new JScrollPane(mainPanel);
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setTitle("Form 48 Tracker");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(1000, 400));
this.add(scrollBar, BorderLayout.CENTER);
this.add(bttn, BorderLayout.PAGE_END);
this.pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void itemStateChanged(ItemEvent checkItem) {
if (checkItem.getStateChange() == ItemEvent.SELECTED) {
System.out.println(checkBoxArrLst.size());
}
infoPanel.revalidate();
infoPanel.repaint();
}
public static void main(String[] args) {
new UI();
}
}