我是一個初學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";
}
}
但它不工作,我無法理解爲什麼它不是工作,所以如果任何人有一定的時間來幫我,我會很高興。
謝謝!
感謝您的嘗試,但它仍然不會工作,但感謝試試吧! – user290030 2010-03-09 22:53:09