我对ItemListeners有问题。基本上,我想查询数据库以获取一些数据,并按照DAO模式将它们放入对象中。问题是,当我将ArrayList
中的CheakBoxes
放入时,我不知道如何向它们添加侦听器。
在通过循环将其放入其容器之前,我尝试添加ItemListener
。我也曾尝试将ItemListeners添加到容器后添加,但也无济于事。
我只为1个chekBox测试了我的脚本,它可以工作。当涉及到ArrayList时,出现以下错误消息:无法引用封闭范围中定义的非最终局部变量jCheckBox
下面的代码是另一个测试,目的只是为了查看我的程序是否存在错误,但是在将侦听器添加到ArrayList对象时似乎给了我同样的问题
package test;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
private static ArrayList<JCheckBox> jCheckBoxs;
private static JFrame jFrame=new JFrame();
public static void main(String[] args) {
init();
}
private static void init() {
jFrame.setSize(new Dimension(400,400));
jFrame.setTitle("test");
JPanel jp=new JPanel();
jCheckBoxs=new ArrayList<JCheckBox>();
addArray("A");
addArray("B");
addArray("C");
jp.add(jCheckBoxs.get(0));
jp.add(jCheckBoxs.get(1));
jp.add(jCheckBoxs.get(2));
jFrame.getContentPane().add(jp);
jFrame.setVisible(true);
}
private static void addArray(String msg) {
JCheckBox jCheckBox=new JCheckBox();
jCheckBox.setText(msg);
jCheckBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println(jCheckBox.getName());
}
});
}
}
答案 0 :(得分:0)
final
例如:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
@SuppressWarnings("serial")
public class MultCheckBoxes extends JPanel {
private List<JCheckBox> checkBoxes = new ArrayList<>();
private JTextArea showSelectionTArea = new JTextArea(25, 50);
private JPanel checkBoxPanel = new JPanel(new GridLayout(0, 1));
private ItemListener myItemListener = new MyItemListener();
public MultCheckBoxes() {
showSelectionTArea.setFocusable(false); // so user can't enter text
JPanel wrapperPanel = new JPanel(new BorderLayout());
wrapperPanel.add(checkBoxPanel, BorderLayout.PAGE_START);
JPanel bottomPanel = new JPanel();
bottomPanel.add(new JButton(new GetAllStatesAction()));
setLayout(new BorderLayout());
add(new JScrollPane(showSelectionTArea));
add(wrapperPanel, BorderLayout.LINE_END);
add(bottomPanel, BorderLayout.PAGE_END);
}
public void addCheckBox(String name) {
JCheckBox checkBox = new JCheckBox(name);
checkBox.addItemListener(myItemListener);
checkBoxes.add(checkBox);
checkBoxPanel.add(checkBox);
revalidate();
repaint();
}
private class MyItemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent itemEvt) {
// The ItemEvent object will give us the JCheckBox that was changed
JCheckBox checkBox = (JCheckBox) itemEvt.getSource();
// get its text and its state
String text = checkBox.getText();
String select = itemEvt.getStateChange() == ItemEvent.SELECTED ? "selected" : "unselected";
// display this in the JTextArea
showSelectionTArea.append(text + ": " + select + "\n");
}
}
private class GetAllStatesAction extends AbstractAction {
public GetAllStatesAction() {
super("Get All CheckBox States"); // button's text
putValue(MNEMONIC_KEY, KeyEvent.VK_A); // mnemonic key
}
@Override
public void actionPerformed(ActionEvent e) {
showSelectionTArea.append("\n");
showSelectionTArea.append("All CheckBox States: \n");
for (JCheckBox jCheckBox : checkBoxes) {
String text = jCheckBox.getText();
String select = jCheckBox.isSelected() ? "selected" : "unselected";
showSelectionTArea.append(text + ": " + select + "\n");
}
showSelectionTArea.append("\n");
}
}
private static void createAndShowGui() {
MultCheckBoxes mainPanel = new MultCheckBoxes();
mainPanel.addCheckBox("Box A");
mainPanel.addCheckBox("Box B");
mainPanel.addCheckBox("Box C");
mainPanel.addCheckBox("Box D");
mainPanel.addCheckBox("Box E");
JFrame frame = new JFrame("MultCheckBoxes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}