2014-05-07 103 views
1

我有兩個按鈕的接口,一個用於保存單詞,另一個用於存儲字母。將值從一個對象傳遞到另一個對象java

爲了讓我管理單詞1,我有一個班級,單詞班。

在這個類中,有getter和setters以及方法。

方法「表」允許我檢索值,我會得到我的按鈕1,然後將其保存爲一個選項卡char []

我希望我能採取同樣的char array [](按鈕1),在我的第二個按鈕的值相同

綜上所述,我想用在按鈕1輸入的詞,按鈕2

但我不知道它是如何做到的?

// BUTTON 1

final JFrame popup = new JFrame(); 
    //create new instance of JButton 
    final Mot monMot = new Mot(); 

    newButton.addActionListener(new java.awt.event.ActionListener() { 
     @Override 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 

      String name = JOptionPane.showInputDialog(popup, "Enter one word", null); 
      monMot.setMot(name); 

      monMot.tableau(); 
      try { 
       monMot.affichage(); 
      } catch (Exception e) { 
       System.out.println(e); 
      } 
     } 

//按鈕2一鍵

final JFrame popup = new JFrame(); 
    Mot monMot = new Mot(); 

    boolean flag = false; 


    String key = JOptionPane.showInputDialog(popup, "Enter one key",null); 


    try { 
     while (flag == false) { 
      if (key.length() == 1) { 
       flag = true; 
      } else { 
       key = JOptionPane.showInputDialog(popup, "Enter one key",null); 
      } 
     } 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
}    

而且我 類public class蒙特{

private String mot; 
private char[] tab; 
//getter et setter 

public String getMot() { 
    return mot; 
} 

public void setMot(String mot) { 
    this.mot = mot; 
} 
//constructeur plein 

public Mot(String mot, char[] tab) { 
    this.mot = mot; 
    this.tab = tab; 
} 
//constructeur vide 
public Mot() { 
} 
//methodes 

public void affichage() { 
    for (int i = 0; i < this.tab.length; i++) { 
     System.out.println(this.tab[i]); 
    } 
} 
     //placage de chaque lettre dans un tableau 
public void tableau() { 
    this.tab = this.mot.toCharArray(); 
} 

}

+0

的JOptionPane可以與空返回。這就是爲什麼我建議你將while語句改爲while(flag =(key == null))。通過這種方式,您可以爲用戶添加輸入的旗幟和檢查添加新值。 – bitli

回答

0

我的建議是做一個簡單的MVC。

我不知道我理解你。

控制器將執行兩個按鈕操作。您應該將字符串存儲在您的模型中或執行按鈕1操作時的任何內容,並且在執行按鈕2操作時應該獲取存儲的字符串。

示例代碼:

public class Controller { 

     private View view; 
     private Model model; 

     //constructor will get the view and the model, and adds ActionHandlers 
     public Controller(final Model amodel, final View aview) { 
       this.view=aview; 
       this.model=amodel; 
       addOneButtonActionHandler(); 
       addSecondButtonActionHandler(); 
     } 

     public void addOneButtonActionHandler(){ 

       ActionListener actionHandler= new ActionListener() { 

         @Override 
         public void actionPerformed(final ActionEvent e) { 
          //some action to get the String from user (?) 
          model.storeItem(string); 
         } 
       }; 

       view.addActionToOneButton(actionHandler); 
     } 

     public void addSecondButtonActionHandler(){ 

       ActionListener actionHandler= new ActionListener() { 

         @Override 
         public void actionPerformed(final ActionEvent e) { 
          //some action to get the stored String from Model 
          //String key =model.getStoredItem(); 
         } 
       }; 

       view.addActionToSecondButton(actionHandler); 
     } 

} 
+0

謝謝你!我必須觀察如何製作控制器:D – Skunk

相關問題