我是java初学者,遇到了一个我无法解决的问题。
我正在尝试将字符串添加到我的数组中,我已经测试了我的数组以便工作。但我的问题是我创建了一个 actionlistener 并试图从另一个类中获取文本,然后将其添加到数组中。
我的按钮监听器:
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";
}
}
但它不起作用,我不明白为什么它不起作用,所以如果有人有时间帮助我,我会很高兴。
谢谢!