2014-05-07 180 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

相关问题