2

我是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";
}
}

但它不起作用,我不明白为什么它不起作用,所以如果有人有时间帮助我,我会很高兴。

谢谢!

4

2 回答 2

1

One fault is here:

public class ButtonListener extends AddToLibrary implements ActionListener {

extending AddToLibrary creates a weird inheritance problem.

The simple solution is to define the ButtonListener inline:

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

One important change is to create one instance of Database to which the strings are added (as Amit Kumar already pointed out).

There were a lot of problems with your code, mostly illegal constructions. My advice is to get a good Java tutorial/book and take notice of how they solve the problems. Also if you use Eclipse (or another modern IDE) it will notify you of any illegal constructions and will try to propose solutions.

One last note, the public Strings and the JTextFields have the same name, this creates a problem for the computer as it does not know which one you are referring to (this is called shadowing). Define unique names for each variable within a class so you do not confuse the compiler or yourself.

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

I have done a little work on your code and I arrived at the following. (It may be improved even further, but this is at least a lot better in terms of legality and readability)

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);
    }
}
于 2010-03-10T00:11:02.687 回答
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对不起,我没有时间为您检查代码,但如果这不起作用,您可以通知。

于 2010-03-09T22:35:50.330 回答