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

将值从一个对象传递到另一个java,java,swing,class,object,Java,Swing,Class,Object,我有一个界面,有两个按钮,一个用来保存单词,另一个用来存储字母 为了允许我管理我将用按钮1输入的单词,我有一个类,word类 在这个类中,有getter和setter以及方法 “table”方法允许我检索将获取按钮1的值,然后将其保存为选项卡char[] 我希望我可以使用相同的字符数组[](button1),在我的第二个按钮中使用相同的值 总之,我想在按钮2上使用按钮1中输入的单词 但我不知道怎么做 //按钮1 final JFrame popup = new JFrame();

我有一个界面,有两个按钮,一个用来保存单词,另一个用来存储字母

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

在这个类中,有getter和setter以及方法

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

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

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

但我不知道怎么做

//按钮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);
    }
}              
我的班级呢 公营交通部{

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();
}
私有字符串mot;
私有字符[]选项卡;
//吸气剂
公共字符串getMot(){
返回mot;
}
公共void setMot(字符串mot){
this.mot=mot;
}
//褶皱构造器
公共Mot(字符串Mot,字符[]选项卡){
this.mot=mot;
this.tab=tab;
}
//建筑商
公共交通部{
}
//methodes
公共无效附加(){
for(int i=0;i

}我的建议是制作一个简单的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);
       }

}

谢谢你!我必须看看如何制作一个控件:DJOptionPane可以返回null。这就是为什么我建议您将while语句更改为while(flag=(key==null))。通过这种方式,您可以将一个新值添加到您的标志和用户已添加输入的检查中。