-1

我是一个真正的菜鸟,所以请如果我不问正确的对不起。

所以这里是:

 public class carti extends JFrame {
    JTextField txttitlu;
    JTextField txtautor;
    JTextField txtcat;
    JTextField txtedi;
    JTextField txtpret;
    DefaultListModel model;
    JList lista;
    String editura;
    String titlu;
    String autor;
    String categorie;

这是我拥有的主要类函数,在这个类中,我有 4 个 JTextField,它们通过一个按钮插入到 JList 中(这是添加按钮的脚本“称为”adauga”):

 public void Adauga() {

        autor = txtautor.getText().trim();
        titlu = txttitlu.getText().trim();
        categorie = txtcat.getText().trim();
        editura = txtedi.getText().trim();

        if (autor.equals("")) {
            JOptionPane.showMessageDialog(null, "Nu ati completat Titlu!");
            return;
        }
        if (titlu.equals("")) {
            JOptionPane.showMessageDialog(null, "Nu ati completat Autor!");
            return;
        }
        if (categorie.equals("")) {
            JOptionPane.showMessageDialog(null, "Nu ati completat Categorie!");
            return;
        }
        String numeComplet = autor + " / " + titlu + " : " + categorie;
        model.addElement(numeComplet);

    }

所以我想要的是:“Adauga”按钮添加到我的 JList 以显示在我的扩展新类上,该类称为“detalii”(这里是 si 创建的新类的代码(“detalii”) :

public detalii(){

    this.setTitle("Detalii despre Carte");
    setSize(400, 300);
    setResizable(false);
    setLocation(370, 150);
    setLayout(null);
    JLabel lbltitlu= new JLabel("Titlu" +" : "); 
    lbltitlu.setBounds(15, 5, 120, 25);
    this.add(lbltitlu);
    JLabel lblautor= new JLabel("Autor" +" : "); 
    lblautor.setBounds(15, 25, 120, 25);
    this.add(lblautor);
    JLabel lblcategorie= new JLabel("Categorie" +" : "); 
    lblcategorie.setBounds(15, 45, 120, 25);
    this.add(lblcategorie);
    JLabel lbleditura= new JLabel("Editura" +" : "  ); 
    lbleditura.setBounds(15, 65, 120, 25);
    this.add(lbleditura);

}

感谢您的帮助,如果我的解释没有正确完成,我很抱歉,我真的很菜鸟。希望你能理解,不要浪费你的时间。

4

1 回答 1

0

如果我理解正确,您希望“detalii”类的标签使用“Adauga”方法提供的信息进行更新。我想这样做的一种方法是从“Adauga”方法调用“detalii”中的方法。

这就是我将如何更改“Adauga”:

public void Adauga() {

    autor = txtautor.getText().trim();
    titlu = txttitlu.getText().trim();
    categorie = txtcat.getText().trim();
    editura = txtedi.getText().trim();

    if (autor.equals("")) {
        JOptionPane.showMessageDialog(null, "Nu ati completat Titlu!");
        return;
    }
    if (titlu.equals("")) {
        JOptionPane.showMessageDialog(null, "Nu ati completat Autor!");
        return;
    }
    if (categorie.equals("")) {
        JOptionPane.showMessageDialog(null, "Nu ati completat Categorie!");
        return;
    }
    String numeComplet = autor + " / " + titlu + " : " + categorie;
    model.addElement(numeComplet);

    detalii.pushNewInfo(autor, titlu, categorie, editura);  // Add This Line!

}

我会将“detalii”更改为以下内容:

private JLabel lbltitlu;
private JLabel lblautor;
private JLabel lbleditura;
private JLabel lblcategorie;

public detalii(){

    this.setTitle("Detalii despre Carte");
    setSize(400, 300);
    setResizable(false);
    setLocation(370, 150);
    setLayout(null);
    lbltitlu= new JLabel("Titlu" +" : "); 
    lbltitlu.setBounds(15, 5, 120, 25);
    this.add(lbltitlu);
    lblautor= new JLabel("Autor" +" : "); 
    lblautor.setBounds(15, 25, 120, 25);
    this.add(lblautor);
    lblcategorie= new JLabel("Categorie" +" : "); 
    lblcategorie.setBounds(15, 45, 120, 25);
    this.add(lblcategorie);
    lbleditura= new JLabel("Editura" +" : "  ); 
    lbleditura.setBounds(15, 65, 120, 25);
    this.add(lbleditura);
}

public void pushNewInfo(String author, String title, 
                        String category, String editor) {
    lblautor.setText("Autor : " + author);
    lbltitlu.setText("Titlu : " + title);
    lblcategorie.setText("Categorie : " + category);
    lbleditura.setText("Editura : " + editor);
}

这可能不是最好的解决方案,但鉴于您提供的信息,这就是我将如何解决您的问题。

于 2013-01-21T16:10:36.780 回答