我用 NetBeans Swind 创建了应用程序。为了形成我添加了我自己的 JList:
public class JStaticMessagesList extends JList<StaticMessagePanel> implements StaticMessages<StaticMessagePanel> {
private JStaticMessagesList.MyListModel messages = new MyListModel();
private final StaticMessagesForm messgeForm;
public JStaticMessagesList(StaticMessagesForm messgeForm) {
this.messgeForm = messgeForm;
}
public JStaticMessagesList(StaticMessagesForm messgeForm, StaticMessages<StaticMessage> staticMessages) {
this(messgeForm);
for (StaticMessage msg : staticMessages) {
messages.addElement(new StaticMessagePanel(messgeForm, msg));
}
}
@Override
public void add(StaticMessagePanel msg) {
messages.addElement(msg);
}
@Override
public void deleteStaticMessage(StaticMessagePanel msg) {
messages.removeElement(msg);
}
@Override
public Iterator<StaticMessagePanel> iterator() {
return messages.iterator();
}
@Override
public ListModel<StaticMessagePanel> getModel() {
return null;
}
@Override
public final void setModel(ListModel<StaticMessagePanel> model) {
if (model instanceof MyListModel) {
super.setModel(model);
}
}
public class MyListModel extends DefaultListModel<StaticMessagePanel> implements Iterable<StaticMessagePanel> {
@Override
public Iterator<StaticMessagePanel> iterator() {
return new MyModelIterator(this);
}
private class MyModelIterator implements Iterator<StaticMessagePanel> {
private int idx = 0;
private final MyListModel model;
private MyModelIterator(MyListModel model) {
this.model = model;
}
@Override
public boolean hasNext() {
return model.getSize() > idx + 1;
}
@Override
public StaticMessagePanel next() {
return model.get(idx++);
}
@Override
public void remove() {
throw new UnsupportedOperationException("Removing with iterator is not supported!");
}
}
}
}
添加我的组件而不是普通的 JList 后,我得到了非常讨厌的异常,我不知道如何处理它。我也很确定问题出在这个组件上,因为当我删除它时一切正常。
联系方式:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1342)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
at javax.swing.plaf.basic.BasicListUI.getPreferredSize(BasicListUI.java:578)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1660)
at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:790)
at java.awt.Container.layout(Container.java:1503)
at java.awt.Container.doLayout(Container.java:1492)
at java.awt.Container.validateTree(Container.java:1688)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validate(Container.java:1623)
at java.awt.Container.validateUnconditionally(Container.java:1660)
at java.awt.Window.pack(Window.java:817)
at com.p.staticmessages.gui.StaticMessagesForm.initComponents(StaticMessagesForm.java:319)
at com.p.staticmessages.gui.StaticMessagesForm.<init>(StaticMessagesForm.java:37)
at com.p.staticmessages.gui.StaticMessagesForm.<init>(StaticMessagesForm.java:27)
at com.p.staticmessages.gui.StaticMessagesForm$14.run(StaticMessagesForm.java:411)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1342)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
at javax.swing.plaf.basic.BasicListUI.getPreferredSize(BasicListUI.java:578)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1660)
at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:790)
at java.awt.Container.layout(Container.java:1503)
at java.awt.Container.doLayout(Container.java:1492)
at java.awt.Container.validateTree(Container.java:1688)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validateTree(Container.java:1697)
at java.awt.Container.validate(Container.java:1623)
at java.awt.Window.dispatchEventImpl(Window.java:2711)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
PS com.p.staticmessages.gui.StaticMessagesForm.(StaticMessagesForm.java:37) 代码中的
这一行(此处未列出)与 pack() 一致。