简体   繁体   中英

Store input from JTextField in Variable

I have been trying to use the input stored in the JTextField for computation, but can't seem to have it actually stored in a variable. Sure, I understand that I can use .getText(), however there is an issue accessing the text outside the actionListener.

          textInput.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){

                    String input = textInput.getText();
                    //How do I get the 'input' variable outside this method? 
               } 
           });

//You defined input inside method that is why you can't access outside the method.

      String input="";
 textInput.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){

                        input = textInput.getText();
                        //How do I get the 'input' variable outside this method? 
                   } 
               });

Apply this code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM