从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]; }
});
我想从这个列表中获取项目,使用这样的切换方法:
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()),但它只会显示默认&#34; case&#34;,然后我有案例1:案例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;
}
}
}
});
}
然而,它仍然不会显示任何内容。
答案 0 :(得分:0)
由此脚本解决:
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;
}
}
}
});