简体   繁体   English

如何将文本字段值存储到变量中以在Java swing中进一步使用它?

[英]How to store text field value in to a variable for further use of it in java swing?

initialized a variable "input" and i want store the text in to it. 初始化变量“输入”,我想将文本存储在其中。 so that i can perform search operation using that variable. 这样我就可以使用该变量执行搜索操作。 by the below code it is not taking the variable input. 通过下面的代码,它不接受变量输入。 thanks inadvance. 提前致谢。 please anyone help me 请任何人帮助我

String input; 字符串输入;

    jb.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae) 
        {
            input = jt.getText(); // problem occurs here
            jl.setText(input);  
        }
    });

NOTE: if you want to use the object anywhere of your program please declare it at the top of your program. 注意:如果要在程序的任何位置使用该对象,请在程序顶部声明它。 Like that 像那样

import java.awt.EventQueue;

public class MyExample {

    private JFrame frame;
    String input;

You shouldn't declare type of 'input' at out of an anonymous class. 您不应该在匿名类之外声明“输入”的类型。 In this way, you have an error like 'The final local variable input cannot be assigned, since it is defined in an enclosing type'. 这样,您会遇到类似“无法分配最终局部变量输入,因为它是用封闭类型定义的”之类的错误。 In order to avoid this error, please make sure that you write the code in this way, 为了避免发生此错误,请确保以这种方式编写代码,

        jb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String input = jt.getText();
                jl.setText(input);
            }
        });

So you can put the input into JLabel. 因此,您可以将输入放入JLabel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java Swing Gui中从用户输入存储变量以备将来使用? - How to store variables for further use from User-input in Java Swing Gui? 如何在 java swing 中一次按下按钮仅清除文本字段中的一个值? - How to clear only one value from text field in one press on button in java swing? 有什么方法可以使用java将数组数据存储在变量中并进一步使用该变量 - Is there any way through which i can store array data in variable using java and use that variable further Java GUI Swing 文本字段 - Java GUI Swing Text Field Java Swing文本字段高度 - Java Swing Text Field Height java - 如何将字符串作为文本字段中的输入并将其存储在变量中,但在java netbeans的文本字段中显示另一个字符串? - How to take a string as an input in a text field and store it in a variable but show another string in the text field in java netbeans? 如何在swing中将swing文本字段中的文本添加到mysql表中? - How to add text from swing text field to mysql table in Java? 如何在用 Java Swing 设计的桌面应用程序中存储静态变量? - How to store a static variable in desktop application designed in Java Swing? 在Swing Java中将值打印到变量中 - Printing the value in a variable in Swing Java 如何在Java的字段中存储变量类型的对象? - How to store an object of variable type in a field in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM