gpt4 book ai didi

java - 为什么我的 ItemListener 不起作用?

转载 作者:行者123 更新时间:2023-12-02 08:33:12 26 4
gpt4 key购买 nike

我正在尝试在 JCheckBox 中实现 ItemListener。以下代码有效:

public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel p = new JPanel();
p.setLayout(new FlowLayout());

int size = 5;
JLabel[] labels = new JLabel[size];
for (int i = 1; i < labels.length; i++) {
labels[i] = new JLabel();
labels[i].setText("" + i);
;

labels[i].setPreferredSize(new Dimension(25, 25));
p.add(labels[i]);
}

JCheckBox[] checkBoxs = new JCheckBox[size];
for (int i = 1; i < checkBoxs.length; i++) {
checkBoxs[i] = new JCheckBox();
checkBoxs[i].setSelected(true);
p.add(checkBoxs[i]);
checkBoxs[i].addItemListener(new MyItemListener(labels[i]));
}

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setContentPane(p);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

和监听类:

public class MyItemListener implements ItemListener {

JLabel label = new JLabel();

public MyItemListener(JLabel label) {
super();
this.label = label;
}

@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
label.setVisible(true);
label.revalidate();
label.repaint();
} else {
label.setVisible(false);
}
}

但是现在,我试图用单独的类做同样的事情,但它不起作用:

测试.java

public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel p = new JPanel();
p.setLayout(new FlowLayout());

PanelOfLabel panelOfLabel = new PanelOfLabel();
panelOfLabel.setBorder(new LineBorder(Color.red));
p.add(panelOfLabel);

PanelOfBoxes panelOfBoxes = new PanelOfBoxes();
panelOfBoxes.setBorder(new LineBorder(Color.green));
p.add(panelOfBoxes);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setContentPane(p);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

PanelOfLabel.java

public class PanelOfLabel extends JPanel {

private JLabel[] labels = new JLabel[5];

public PanelOfLabel() {
super();
createLabels();
}

public void createLabels() {
for (int i = 1; i < labels.length; i++) {
labels[i] = new JLabel();
labels[i].setText("" + i);
labels[i].setPreferredSize(new Dimension(25, 25));
this.add(labels[i]);
}

}

PanelOfBoxes.java

public class PanelOfBoxes extends JPanel {

private JCheckBox[] checkBoxs = new JCheckBox[5];
private PanelOfLabel[] PanelOfLabels = new PanelOfLabel[5];

public PanelOfBoxes() {
super();
createBoxes();
}

public void createBoxes() {
for (int i = 1; i < checkBoxs.length; i++) {
checkBoxs[i] = new JCheckBox();
checkBoxs[i].setSelected(true);
this.add(checkBoxs[i]);
checkBoxs[i].addItemListener(new MyListener(PanelOfLabels[i]));
}
}

和 MyListener.java

public class MyListener implements ItemListener {

private PanelOfLabel panelOfLabels;

public MyListener(PanelOfLabel panelOfLabels) {
super();
this.panelOfLabels = panelOfLabels;
}

@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
panelOfLabels.setVisible(true);
panelOfLabels.revalidate();
panelOfLabels.repaint();
} else {
panelOfLabels.setVisible(false);
}
}

我哪里错了?有人可以帮我吗?

最佳答案

这没有意义:

private PanelOfLabel[] PanelOfLabels = new PanelOfLabel[5];

public PanelOfBoxes() {
super();
createBoxes();
}

public void createBoxes() {
for (int i = 1; i < checkBoxs.length; i++) {
checkBoxs[i] = new JCheckBox();
checkBoxs[i].setSelected(true);
this.add(checkBoxs[i]);
checkBoxs[i].addItemListener(new MyListener(PanelOfLabels[i]));
}
}

由于尚未构建每个 PanelOfLabels 项,因此每个项都将为空。您正在将空引用传递给 MyListener 构造函数。

难道您不应该将 一个 JLabel 传递给该构造函数而不是 JPanel 数组吗?

例如,

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.LineBorder;

public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel p = new JPanel();
p.setLayout(new FlowLayout());

PanelOfLabel panelOfLabel = new PanelOfLabel();
panelOfLabel.setBorder(new LineBorder(Color.red));
p.add(panelOfLabel);

PanelOfBoxes panelOfBoxes = new PanelOfBoxes(panelOfLabel); //!!
panelOfBoxes.setBorder(new LineBorder(Color.green));
p.add(panelOfBoxes);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setContentPane(p);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

class PanelOfLabel extends JPanel {
private JLabel[] labels = new JLabel[5];

public PanelOfLabel() {
super();
createLabels();
}

public void setLabelVisibility(int index, boolean visible) {
if (index < 0 || index >= labels.length) {
throw new ArrayIndexOutOfBoundsException(index);
}

labels[index].setVisible(visible);
}

public void createLabels() {
for (int i = 1; i < labels.length; i++) {
labels[i] = new JLabel();
labels[i].setText("" + i);
labels[i].setPreferredSize(new Dimension(25, 25));
this.add(labels[i]);
}

}
}

class PanelOfBoxes extends JPanel {

private JCheckBox[] checkBoxs = new JCheckBox[5];
// !! private PanelOfLabel[] PanelOfLabels = new PanelOfLabel[5];
private PanelOfLabel panelOfLabel; //!!

public PanelOfBoxes(PanelOfLabel panelOfLabel) {
super();
this.panelOfLabel = panelOfLabel; //!!
createBoxes();
}

public void createBoxes() {
for (int i = 1; i < checkBoxs.length; i++) {
checkBoxs[i] = new JCheckBox();
checkBoxs[i].setSelected(true);
this.add(checkBoxs[i]);
// !!
checkBoxs[i].addItemListener(new MyListener(i, panelOfLabel)); //!!
}
}
}

class MyListener implements ItemListener {

private PanelOfLabel panelOfLabels;
private int index;

// !!
public MyListener(int index, PanelOfLabel panelOfLabels) {
super();
this.index = index; // !!
this.panelOfLabels = panelOfLabels;
}

@Override //!!
public void itemStateChanged(ItemEvent e) {
boolean visible = e.getStateChange() == ItemEvent.SELECTED;
panelOfLabels.setLabelVisibility(index, visible);
Container parent = panelOfLabels.getParent();
parent.repaint();
}
}

编辑
我对模型- View -控制 (MVC) 解决方案的尝试:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.event.*;

public class TestMvc {

private static final int SIZE = 6;

private static void createAndShowGui() {
TestModel model = new TestModel(SIZE);
TestView view = new TestView();
new TestControl(model, view);

JFrame frame = new JFrame("TestMvc");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(view.getMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class TestControl {

private TestModel model;
private TestView view;

public TestControl(final TestModel model, final TestView view) {
this.model = model;
this.view = view;
view.setModel(model);
TestViewCheckBoxBox checkBoxBox = view.getCheckBoxBox();
for (int i = 0; i < model.getSize(); i++) {
checkBoxBox.addCheckBoxItemListener(i, new MyItemListener(i));
}

model.addPropertyChangeListener(new ModelPropertyChangeListener());
}

private class MyItemListener implements ItemListener {
private int index;

public MyItemListener(int index) {
this.index = index;
}

@Override
public void itemStateChanged(ItemEvent iEvt) {
model.setValue(index, iEvt.getStateChange() == ItemEvent.SELECTED);
}
}

private class ModelPropertyChangeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if (TestModel.VALUE.equals(pcEvt.getPropertyName())) {
IndexedPropertyChangeEvent ipcEvt = (IndexedPropertyChangeEvent) pcEvt;
int index = ipcEvt.getIndex();
boolean visible = (Boolean)ipcEvt.getNewValue();
view.getLabelBox().setLabelVisible(index, visible);
}
}
}

}

class TestView {
private JPanel mainPanel = new JPanel();
private TestModel model;
private TestViewLabelBox labelBox;
private TestViewCheckBoxBox checkBoxBox;

public void setModel(TestModel model) {
this.model = model;
mainPanel.setLayout(new GridLayout(1, 0, 5, 0));
labelBox = new TestViewLabelBox(model.getSize());
checkBoxBox = new TestViewCheckBoxBox(model.getSize());

mainPanel.add(labelBox.getMainPanel());
mainPanel.add(checkBoxBox.getMainPanel());
}

public TestViewLabelBox getLabelBox() {
return labelBox;
}

public TestViewCheckBoxBox getCheckBoxBox() {
return checkBoxBox;
}

public JComponent getMainPanel() {
return mainPanel;
}

}

class TestViewLabelBox {
private JPanel mainPanel = new JPanel();
private List<JLabel> labelList = new ArrayList<>();

public TestViewLabelBox(int count) {
mainPanel.setBorder(BorderFactory.createLineBorder(Color.black));
mainPanel.setLayout(new GridLayout(1, 0, 5, 0));
for (int i = 0; i < count; i++) {
JLabel label = new JLabel(String.valueOf(i + 1), SwingConstants.CENTER);
labelList.add(label);
mainPanel.add(label);
}
}

public void setLabelVisible(int index, boolean visible) {
if (index < 0 || index >= labelList.size()) {
throw new ArrayIndexOutOfBoundsException(index);
}
String text = visible ? String.valueOf(index + 1) : "";
labelList.get(index).setText(text);
}

public JComponent getMainPanel() {
return mainPanel;
}

}

class TestViewCheckBoxBox {
private JPanel mainPanel = new JPanel();
private List<JCheckBox> checkBoxList = new ArrayList<>();

public TestViewCheckBoxBox(int count) {
mainPanel.setBorder(BorderFactory.createLineBorder(Color.black));
mainPanel.setLayout(new GridLayout(1, 0, 5, 0));
for (int i = 0; i < count; i++) {
JCheckBox checkBox = new JCheckBox("", true);
checkBoxList.add(checkBox);
mainPanel.add(checkBox);
}
}

public void addCheckBoxItemListener(int index, ItemListener itemListener) {
if (index < 0 || index >= checkBoxList.size()) {
throw new ArrayIndexOutOfBoundsException(index);
}
checkBoxList.get(index).addItemListener(itemListener);
}

public JComponent getMainPanel() {
return mainPanel;
}
}

class TestModel {
public static final String VALUE = "value";
private List<Boolean> values = new ArrayList<>();
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(
this);

public TestModel(int size) {
for (int i = 0; i < size; i++) {
values.add(Boolean.TRUE);
}
}

public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}

public int getSize() {
return values.size();
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}

public boolean getValue(int index) {
if (index < 0 || index >= values.size()) {
throw new ArrayIndexOutOfBoundsException(index);
}
return values.get(index);
}

public void setValue(int index, boolean value) {
if (index < 0 || index >= values.size()) {
throw new ArrayIndexOutOfBoundsException(index);
}
boolean oldValue = getValue(index);
boolean newValue = value;
values.set(index, value);
pcSupport.fireIndexedPropertyChange(VALUE, index, oldValue, newValue);
}
}

关于java - 为什么我的 ItemListener 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24597977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com