好吧,所以我試圖從列表中放入一個數組中輸出當前字符串... 但是,當我點擊列表時,我得到一個NullPointerException ...:\JLIST上的Java空指針異常
幫助? :)
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class lisTry extends JApplet implements MouseListener {
public static String newline;
public static JList list;
public void init() {
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("Debbie Scott");
listModel.addElement("Scott Hommel");
listModel.addElement("Alan Sommerer");
JList list = new JList(listModel);
this.getContentPane().add(list);
list.addMouseListener(this);
String newline = "\n";
list.setVisible(true);
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) {
int index = list.getSelectedIndex();
System.out.println("You clicked on: " + index);
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void paint(Graphics g) {
}
}
謝謝。
請注意:'>'用於標記塊引用;爲了標記代碼,您將縮進4個空格。 (我已經爲你做了這個改變。) – BoltClock 2010-07-16 18:49:28