2013-01-21 58 views
-1

我是一個真正的noob,所以請如果我不是非常抱歉地問。如何在java中的另一個類中調用字符串

所以在這裏,它是:

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; 

這是主要的類函數,我有,在這個類我有AR通過一個按鈕(在這裏插入一個JList 4個JTextFields將是該腳本添加按鈕「caled‘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要顯示在我的擴展新類,被稱爲‘資訊這’(這裏它是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); 

} 

謝謝你的幫助和對不起,如果我的解釋不正確做我真的noob。希望你能理解並不會浪費你的時間。

+0

有什麼問題嗎? –

+0

問題是我如何從「detalii」中添加到JLabels中,我在主要類中調用「carti」的值,並且按鈕Adauga提供給我在同一個類中的JList希望你明白) – zaporojan

回答

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! 

} 

而且我會改變「資訊這」以下內容:

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); 
} 

這可能不是最好的解決辦法,但是這是根據您提供的信息,我將如何解決您的問題。

+0

謝謝你的信息,但它說:不能對類型detalii的非靜態方法pushNewInfo(String,String,String,String)進行靜態引用。如果我沒有發佈足夠的細節,我會發布更多。對不起,我正在盡力解釋,非常感謝你的幫助。 – zaporojan

+0

啊,對不起。您需要將「static」關鍵字添加到「pushNewInfo」方法中,如下所示: public static void pushNewInfo(String author ... 希望對您有所幫助! – Katalyst

相關問題