从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()),但它只会显示默认的“ 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;
}
}
}
});