gpt4 book ai didi

java - 无法从 jcombobox 中删除第一个元素

转载 作者:行者123 更新时间:2023-11-30 11:51:54 27 4
gpt4 key购买 nike

我无法从 jcombobox 中删除第一个元素。我的代码如下,

JComboBox cBox= cBox= new JComboBox();
...
while (cBox.getItemCount() > 0)
cBox.removeItemAt(0);

对于测试运行,我在 cBox 中有 3 个项目。当到达 removeItemAt(0) 时,调试变得困惑,进入一些绝对不相关的文件访问代码。这样做两次然后得到以下异常。我尝试了直接获得相同异常的 removeAllItems() 。但是,removeItem(1) 会正常工作,直到只剩下 1 个元素。异常不会使应用程序崩溃,之后我在组合框中看不到任何项目,所以它有点工作。我究竟做错了什么。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at util.Gui$4.actionPerformed(Gui.java:111)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.DefaultComboBoxModel.removeElementAt(Unknown Source)
at javax.swing.JComboBox.removeItemAt(Unknown Source)
at util.Gui.prepareSubLists(Gui.java:164)
at util.Gui$3.actionPerformed(Gui.java:97)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

你的条件语句是不是错了?将 while 替换为 if,这样

if(cBox.getItemCount() > 0){
cBox.removeItemAt(0);
}

这是一个 SSCCE :

public final class JComboBoxDemo {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
createAndShowGUI();
}
});
}

public static void createAndShowGUI(){
final JFrame frame = new JFrame("JComboBox Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(JComboPane.newInstance());
frame.setSize(new Dimension(250, 100)); // for demonstration purposes only
//frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private static final class JComboPane extends JPanel{
private JComboPane(){
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JCenteredComboBox comboBox = JCenteredComboBox.newInstance();
JCenteredButton button = JCenteredButton.newInstance(comboBox);
add(comboBox);
add(button);
}

public static final JComboPane newInstance(){
return new JComboPane();
}

private static final class JCenteredComboBox extends JComboBox{
private JCenteredComboBox(){
super(new String[]{"Item 1", "Item 2", "Item 3"});
setAlignmentX(Component.CENTER_ALIGNMENT);
}

public static final JCenteredComboBox newInstance(){
return new JCenteredComboBox();
}
}

private static final class JCenteredButton extends JButton{
private JCenteredButton(final JComboBox comboBox){
super("Remove First Item");
setAlignmentX(Component.CENTER_ALIGNMENT);
addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(comboBox.getItemCount() > 0){
comboBox.removeItemAt(0); // your logic
}
}
});
}

public static final JCenteredButton newInstance(final JComboBox comboBox){
return new JCenteredButton(comboBox);
}
}
}
}

enter image description here

当您运行它时,按下 JButton 将删除 JComboBox 中的第一项。你可以一直按这个直到它变空。

关于java - 无法从 jcombobox 中删除第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7175391/

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