将值从一个对象传递给另一个java

时间:2014-05-07 10:56:50

标签: java swing class object

我有一个带有两个按钮的界面,一个用于保存单词,另一个用于存储一个字母。

为了允许我管理我将用按钮1输入的单词,我有一个班级,单词类。

在本课程中,有getter和setter以及方法。

方法“table”允许我检索我将获得按钮1的值,然后将其保存为选项卡char []

我希望我可以使用相同的char array [](button1),在第二个按钮中使用相同的值

总之,我想在按钮2上使用按钮1中输入的单词。

但我不知道怎么办?

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

// BUTTON 2 ONE KEY

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

我的班级 公共课Mot {

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

}

1 个答案:

答案 0 :(得分:0)

我的建议是制作一个简单的MVC。

我不确定我理解你。

控制器将实现两个按钮操作。您应该将String存储在模型中,或者在执行Button 1操作时将其存储,并且在执行Button 2操作时应该获取存储的String。

示例代码:

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

}
相关问题