我是一个真正的菜鸟,所以,如果我不认真地抱歉,那就好了。
所以这是:
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个JTextFields通过按钮插入JList(这里是添加按钮的脚本" caled" adauga&#34 ):
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);
}
所以我想要的是:什么按钮&#34; Adauga&#34;正在添加到我的JList中以显示在我的扩展新类中,该类被称为&#34; detalii&#34;(这里是新创建的类的代码(&#34; detalii&#34;):< / p>
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);
}
感谢您的帮助,如果我的解释没有正确完成,我很抱歉,我真的很棒。希望你能理解,不要浪费你的时间。
答案 0 :(得分: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);
}
这可能不是最好的解决方案,但鉴于您提供的信息,这就是我解决您问题的方法。