2010-03-09 302 views
2

我是一個初學java的人,並且遇到了一個我無法解決的問題。用actionlistener添加到數組

我想添加字符串到我的數組,我測試了我的數組,以便工作。但我的問題是,我創建了一個actionlistener並試圖從另一個類中獲取文本,然後將其添加到數組中。

我Buttonlistener:

public class ButtonListener extends AddToLibrary implements ActionListener { 
public void actionPerformed(ActionEvent e) { 
    Database dt = new Database(); 
    dt.add(textType, textTitle, textSort, textDesc); 
} } 

我有一個朋友,誰告訴我,我創造我每次按下按鈕時,一個新的數據庫,但我該怎麼做,如果我只是想「負荷」嗎?可以清除該數據庫是我的數組的類名。

這個更「有趣」的部分是,當我在eclipse中運行它時,它調試器沒有向我顯示任何明確的錯誤,並且由於我在java中的知識有限,這對我來說太過於了。

我buttonlistener是歌廳從AddToLibrary的信息,它看起來像這樣:

public class AddToLibrary extends JPanel{ 
public String textTitle; 
public String textSort; 
public String textDesc; 
public String textType; 

public AddToLibrary() { 
    // Förklarande text 
    JLabel titel = new JLabel("Titel"); 
    JLabel sort = new JLabel("Genre"); 
    JLabel desc = new JLabel("Beskriving"); 

    // Textrutor 
    JTextField textTitel = new JTextField(null, 20); 
    textTitel.setToolTipText("ex. Flickan som lekte med elden"); 
    JTextField textSort = new JTextField(null, 10); 
    textSort.setToolTipText("ex. Skräck, Action"); 
    JTextField textDesc = new JTextField(null, 15); 
    textDesc.setToolTipText("ex. Stieg Larsson"); 

    // Knappar 
    JButton addButton = new JButton("Lägg till"); 
    addButton.addActionListener(new ButtonListener());  //Lyssna på knapp 

    // Combobox 
    JComboBox comboBox = new JComboBox(); 
    comboBox.addItem("Film"); 
    comboBox.addItem("CD"); 
    comboBox.addItem("Bok"); 
    comboBox.addItem("Annat"); 

    // Lägg till i panelen 
    add(titel); 
    add(textTitel); 
    add(sort); 
    add(textSort); 
    add(desc); 
    add(textDesc); 
    add(comboBox); 
    add(addButton); 


} 

public String getTitelText(JTextField titelText) { 
    textTitle = "" + titelText.getText(); 
    return textTitle; 
} 

public String getDescText(JTextField descText) { 
    textDesc = "" + descText.getText(); 
    return textDesc; 
} 

public String getSortText(JTextField sortText) { 
    textSort = "" + sortText.getText(); 
    return textSort; 
} 

public String getTypeText(JComboBox comboBox) { 
    return textType = "" + (String) comboBox.getSelectedItem() + ".png"; 
} 
} 

但它不工作,我無法理解爲什麼它不是工作,所以如果任何人有一定的時間來幫我,我會很高興。

謝謝!

回答

0
public class ButtonListener extends AddToLibrary implements ActionListener { 
    private Database dt = new Database(); 
    public void actionPerformed(ActionEvent e) { 
     dt.add(textType, textTitle, textSort, textDesc); 
    } 
} 

應該工作。或者更好的是,數據庫應該在AddToLibrary中創建,並在其構造函數中傳遞給ButtonListener。對不起,我沒有時間檢查你的代碼,但如果這不起作用,你可以通知。

+0

感謝您的嘗試,但它仍然不會工作,但感謝試試吧! – user290030 2010-03-09 22:53:09

1

一個缺點是在這裏:

public class ButtonListener extends AddToLibrary implements ActionListener { 

延伸AddToLibrary創建一個奇怪的繼承問題。

簡單的解決方案是定義ButtonListener在線:

final Database dt = new Database(); 
addButton.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      dt.add(getTypeText(comboBox), getTitelText(textTitel), getSortText(textSort), getDescText(textDesc)); 
     } 
}); // Lyssna på knapp 

一個重要的改變是創建向其中添加字符串數據庫的一個實例(如阿米特庫馬爾已經指出的那樣)。

你的代碼有很多問題,主要是非法構造。我的建議是獲得一個好的Java教程/書,並注意他們如何解決問題。此外,如果您使用Eclipse(或其他現代IDE),它會通知您任何非法構造,並嘗試提出解決方案。

最後一個注意事項是,公共字符串和JTextField具有相同的名稱,這會造成計算機的問題,因爲它不知道您指的是哪一個(稱爲陰影)。爲一個類中的每個變量定義唯一的名稱,以免混淆編譯器或自己。

=====================================

我做了一些在你的代碼上工作,我到達了以下。(它甚至可以進一步提高,但是這至少是好了很多,在合法性和可讀性方面)

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class AddToLibrary extends JPanel { 
    private static final long serialVersionUID = 1L; 

    private Database database = new Database(); 

    private JComboBox comboBox = new JComboBox(new String[]{"Film", "CD", "Bok", "Annat"}); 
    private JButton addButton = new JButton("Lägg till"); 

    private JTextField textTitel = new JTextField(null, 20); 
    private JTextField textSort = new JTextField(null, 10); 
    private JTextField textDesc = new JTextField(null, 15); 

    private JLabel titel = new JLabel("Titel"); 
    private JLabel sort = new JLabel("Genre"); 
    private JLabel desc = new JLabel("Beskriving"); 

    public AddToLibrary() { 
     textTitel.setToolTipText("ex. Flickan som lekte med elden"); 
     textSort.setToolTipText("ex. Skräck, Action"); 
     textDesc.setToolTipText("ex. Stieg Larsson"); 

     addButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       database.add(comboBox.getSelectedItem() + ".png", 
          textTitel.getText(), 
          textSort.getText(), 
          textDesc.getText() 
       ) 
      } 
     }); // Lyssna på knapp 

     // Lägg till i panelen 
     add(titel); 
     add(textTitel); 
     add(sort); 
     add(textSort); 
     add(desc); 
     add(textDesc); 
     add(comboBox); 
     add(addButton); 
    } 
} 
+0

啊,很難相信它幾乎一樣,它在你的路上更加美麗,謝謝你花時間! – user290030 2010-03-10 01:24:30