2013-05-11 69 views
2

好吧,我現在有問題的是,我在「公共類MagazineView擴展的Applet {」得到一個錯誤,我得到的是「空白字段的jList可能尚未初始化」LinkedList的插入使用頭插入件

這裏我的GUI小程序

public class MagazineView extends Applet { 
Button button, button2; 
final MagazineList Jlist; 


public void init() { 
JFrame frame = new JFrame(); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
//NORTH PANEL 
JPanel northPanel = new JPanel(new BorderLayout()); 
JPanel topPanel = new JPanel(new BorderLayout()); 
JLabel label = new JLabel("Add Magazine:  "); 
final JTextField text1 = new JTextField(); 
JButton button = new JButton("List Magazines"); 


button.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
      final String name=text1.getText(); 
      Magazine mag = new Magazine(name); 
      Jlist.insert(mag); 
     } 

}); 

    topPanel.add(label, BorderLayout.LINE_START); 
    topPanel.add(text1, BorderLayout.CENTER); 
    topPanel.add(button, BorderLayout.LINE_END); 
    //CENTER PANEL 
    JPanel centerPanel = new JPanel(new BorderLayout()); 
    JTextArea atext = new JTextArea(); 
    centerPanel.add(atext, BorderLayout.CENTER); 

    //BOTTOM PANEL 
    JPanel bottomPanel = new JPanel(new BorderLayout()); 
    JLabel label2 = new JLabel("Delete All:  "); 
    JTextField text2 = new JTextField(); 
    JButton button2 = new JButton("Delete Magazine"); 
    bottomPanel.add(label2, BorderLayout.LINE_START); 
    bottomPanel.add(text2, BorderLayout.CENTER); 
    bottomPanel.add(button2, BorderLayout.LINE_END); 

    northPanel.add(topPanel, BorderLayout.NORTH); 
    northPanel.add(centerPanel, BorderLayout.CENTER); 
    northPanel.add(bottomPanel, BorderLayout.SOUTH); 

    //Frame 
    frame.add(northPanel); 
    frame.setSize(600, 400); 
    frame.setVisible(true); 

    } 


} 

這裏是我的方法

 public class MagazineList { 
     private MagazineNode list; 

     public MagazineList(){ 
      list=null; 
     } 
     public void insert (Magazine mag){ 
    MagazineNode node = new MagazineNode(mag); 
    node.next = list; 
    list = node; 
    } 
     public void add (Magazine mag){ 
      MagazineNode node = new MagazineNode (mag); 
      MagazineNode current; 

      if(list==null) 
       list = node; 
      else 
      { 
       current = list; 
       while(current.next !=null) 
        current = current.next; 
       current.next = node; 
      } 
    } 

    public void DeleteNode(Magazine mag) 
    { 
     if(list == null) throw new RuntimeException("Cannot delete, Empty List"); 

     if(list.magazine.equals(mag)) 
     { 
      list = list.next; 
      return; 
     } 

     MagazineNode cur = list; 
     MagazineNode prev = null; 

     while(cur != null && !cur.magazine.equals(mag)) 
     { 
      prev = cur; 
      cur = cur.next; 
     } 

     if(cur == null) throw new RuntimeException("Cannot delete, not in list"); 

     //delete cur node 
     prev.next = cur.next; 
    } 

    public String toString(){ 
     String result =""; 

     MagazineNode current = list; 

     while (current !=null){ 
      result += current.magazine + "\n"; 
      current = current.next; 
     } 

     return result; 
    } 

    private class MagazineNode { 
     public Magazine magazine; 
     public MagazineNode next; 

     public MagazineNode(Magazine mag){ 
      magazine = mag; 
      next = null; 
     } 
    } 
} 
+0

這是什麼問題?你看到什麼行爲,你不應該看到?你有什麼嘗試?什麼是具體的錯誤信息或行爲?沒有什麼能繼續下去,我們無法幫助你。 – Gian 2013-05-11 02:15:06

+1

請發佈確切的錯誤信息或寫出確切的問題。 – 2013-05-11 02:17:23

+0

沒有錯誤。我只是不知道在public void Insert(Magazine mag){list = new MagazineNode(mag);}中寫入什麼代碼;} 爲了讓節點插入新的字符串; 或如何通過該方法 public void actionPerformed(ActionEvent e){e.getSource()== button){e.getSource()== button){0} {0} {0} \t \t \t \t} – Shayster 2013-05-11 02:20:07

回答

0

如果插入在頭結點,你並不需要遍歷列表。你的list變量是鏈表的頭部,所以你的新節點指向它並使它成爲頭部。

/** 
* Add a new node containing the input Magazine to the front of the linked list. 
* 
* @param mag A magazine to put at the head of the list. 
*/ 
public void add (Magazine mag){ 
    MagazineNode node = new MagazineNode(mag); 
    node.next = list; 
    list = node; 
} 

你可能想名字list改爲head。請注意,這不需要任何if語句。順便說一下,你不必顯示GUI代碼。