gpt4 book ai didi

java - 在 switch 循环中获取选定的 AbstractListModel 的项目

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:07 25 4
gpt4 key购买 nike

从如下创建的 AbstractListModel 检索数据时出现问题:

warmup1List.setModel(new javax.swing.AbstractListModel<String>() {
String[] strings = { "sleepIn", "monkeyTrouble", "sumDouble", "diff21", "parrotTrouble", "makes10", "nearHundred", "posNeg", "notString", "missingChar", "frontBack", "front3", "backAround", "or35", "front22", "startHi", "icyHot", "in1020", "hasTeen", "loneTeen", "delDel", "mixStart", "startOz", "intMax", "close10", "in3050", "max1020", "stringE", "lastDigit", "endUp", "everyNth" };
public int getSize() { return strings.length; }
public String getElementAt(int i) { return strings[i]; }
});

我想从这个列表中获取项目,使用这样的 switch 方法:

public CodingBatGUI() {
initComponents();

switch(warmup1List.getSelectedValue()) {
case "sleepIn":
descriptionTextArea.setText("This is a test.");
break;
case "monkeyTrouble":
descriptionTextArea.setText("This is another test");
break;
default:
descriptionTextArea.setText("Nothing selected");
}

}

但是每当我尝试这样做时,就会出现此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Paketti.CodingBatGUI.<init>(CodingBatGUI.java:20)
at Paketti.CodingBatGUI$6.run(CodingBatGUI.java:321)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我还尝试使用 switch(warmup1List.getSelectedIndex()) 但它只会显示默认的“case”,然后我有 case 1: case 2: 等,但我希望它们作为字符串,因为项目是 AbstractListModel 中的字符串。

那么,我如何真正从 switch 循环内的列表中获取值?提前致谢!

编辑:问题是我在 CodingBatGUI 类中添加了代码。这是更新的脚本:

public void addActionListener(final ActionListener al) {

warmup1List.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
switch(warmup1List.getSelectedValue()) {
case "sleepIn":
descriptionTextArea.setText("This is a test.");
break;
case "monkeyTrouble":
descriptionTextArea.setText("This is another test");
break;
case "sumDouble":
descriptionTextArea.setText("This is another test");
break;
case "diff21":
descriptionTextArea.setText("This is another test");
break;
case "parrotTrouble":
descriptionTextArea.setText("This is another test");
break;
case "makes10":
descriptionTextArea.setText("This is another test");
break;
case "nearHundred":
descriptionTextArea.setText("This is another test");
break;
case "posNeg":
descriptionTextArea.setText("This is another test");
break;
case "notString":
descriptionTextArea.setText("This is another test");
break;
case "missingChar":
descriptionTextArea.setText("This is another test");
break;
case "frontBack":
descriptionTextArea.setText("This is another test");
break;
case "front3":
descriptionTextArea.setText("This is another test");
break;
case "icyHot":
descriptionTextArea.setText("This is another test");
break;
case "in1020":
descriptionTextArea.setText("This is another test");
break;
case "hasTeen":
descriptionTextArea.setText("This is another test");
break;
case "loneTeen":
descriptionTextArea.setText("This is another test");
break;
case "delDel":
descriptionTextArea.setText("This is another test");
break;
case "mixStart":
descriptionTextArea.setText("This is another test");
break;
case "startOz":
descriptionTextArea.setText("This is another test");
break;
case "intMax":
descriptionTextArea.setText("This is another test");
break;
case "close10":
descriptionTextArea.setText("This is another test");
break;
case "in3050":
descriptionTextArea.setText("This is another test");
break;
case "max1020":
descriptionTextArea.setText("This is another test");
break;
case "stringE":
descriptionTextArea.setText("This is another test");
break;
case "lastDigit":
descriptionTextArea.setText("This is another test");
break;
case "endUp":
descriptionTextArea.setText("This is another test");
break;
case "everyNth":
descriptionTextArea.setText("This is another test");
break;
default:
descriptionTextArea.setText("Nothing selected");
break;
}
}
}
});
}

但是,它仍然不会显示任何内容。

最佳答案

通过此脚本解决:

public CodingBatGUI() {
initComponents();

warmup1List.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
switch(warmup1List.getSelectedValue()) {
case "sleepIn":
descriptionTextArea.setText("This is a test.");
break;
case "monkeyTrouble":
descriptionTextArea.setText("This is another test");
break;
case "sumDouble":
descriptionTextArea.setText("This is another test");
break;
case "diff21":
descriptionTextArea.setText("This is another test");
break;
case "parrotTrouble":
descriptionTextArea.setText("This is another test");
break;
case "makes10":
descriptionTextArea.setText("This is another test");
break;
case "nearHundred":
descriptionTextArea.setText("This is another test");
break;
case "posNeg":
descriptionTextArea.setText("This is another test");
break;
case "notString":
descriptionTextArea.setText("This is another test");
break;
case "missingChar":
descriptionTextArea.setText("This is another test");
break;
case "frontBack":
descriptionTextArea.setText("This is another test");
break;
case "front3":
descriptionTextArea.setText("This is another test");
break;
case "icyHot":
descriptionTextArea.setText("This is another test");
break;
case "in1020":
descriptionTextArea.setText("This is another test");
break;
case "hasTeen":
descriptionTextArea.setText("This is another test");
break;
case "loneTeen":
descriptionTextArea.setText("This is another test");
break;
case "delDel":
descriptionTextArea.setText("This is another test");
break;
case "mixStart":
descriptionTextArea.setText("This is another test");
break;
case "startOz":
descriptionTextArea.setText("This is another test");
break;
case "intMax":
descriptionTextArea.setText("This is another test");
break;
case "close10":
descriptionTextArea.setText("This is another test");
break;
case "in3050":
descriptionTextArea.setText("This is another test");
break;
case "max1020":
descriptionTextArea.setText("This is another test");
break;
case "stringE":
descriptionTextArea.setText("This is another test");
break;
case "lastDigit":
descriptionTextArea.setText("This is another test");
break;
case "endUp":
descriptionTextArea.setText("This is another test");
break;
case "everyNth":
descriptionTextArea.setText("This is another test");
break;
default:
descriptionTextArea.setText("Nothing selected");
break;
}
}
}
});

关于java - 在 switch 循环中获取选定的 AbstractListModel 的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44001710/

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