How to get string from JTextField and save it in variable?

泄露秘密 提交于 2021-02-11 10:39:29

问题


I am making a simple kid game that will ask the user to enter his/her name in a JTextField and that name will shown in other class after ending the game.

I made new object and used it to call the method getName but when I call the method it return null

I want it to return the name that the user entered.

This is the code:

package learn_englishTest;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class Home extends JFrame{
    JTextArea welcome_txt,userName_txt;
    JTextField user_name;
    JLabel Background_lbl;
    JButton exit_btn,start_btn;
    JPanel panel;
    Icon Background_icon;
    String name;
    Font userName_font,welcome_font;

    public Home(){
        super("Easy Fun Learning");
        Container c =getContentPane();

        c.setLayout(new BorderLayout());

        panel =new JPanel(null);
        panel.setPreferredSize(new Dimension(650,470));
        
        welcome_txt=new JTextArea("Welcom to Easy Fun Learning ");
        welcome_txt.setEditable(false);
        
         welcome_font = new Font("Verdana", Font.BOLD, 30);
         welcome_txt.setFont(welcome_font);
         welcome_txt.setForeground(Color.pink);
         welcome_txt.setBounds(80, 60, 500, 50);

        userName_font=new Font("Verdana",Font.BOLD,20);
        userName_txt=new JTextArea("Enter Your Name");
        userName_txt.setEditable(false);
        userName_txt.setFont(userName_font);
        userName_txt.setForeground(Color.BLUE);
        userName_txt.setBounds(350, 200, 200, 40);
        
        user_name=new JTextField(10);
        user_name.setBounds(400, 240, 100, 30);

        start_btn=new JButton("Start");
        start_btn.setBounds(480, 360, 100, 20);
        
        exit_btn=new JButton("Exit");
        exit_btn.setBounds(480, 390, 100, 20);
        
        Background_icon=new ImageIcon(getClass().getResource("art.png"));
        Background_lbl=new JLabel(Background_icon);
        Background_lbl.setBounds(0, 80, 450, 450);
        
        panel.add(welcome_txt);
        panel.add(userName_txt);
        panel.add(user_name);
        panel.add(exit_btn);
        panel.add(start_btn);
        panel.add(Background_lbl);

        panel.setBackground(Color.WHITE);

    c.add(panel,BorderLayout.BEFORE_FIRST_LINE);
        
        ButtonHandler handler=new ButtonHandler();
        
        exit_btn.addActionListener(handler);
        start_btn.addActionListener(handler);
    }

    private class ButtonHandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
             if(e.getSource()==exit_btn)
                System.exit(0);
             if(e.getSource()==start_btn){
                 name=user_name.getText();
             
             List list=new List();
             list.setSize(700, 700);
             list.setVisible(true);
             list.setDefaultCloseOperation(EXIT_ON_CLOSE);
             Home.this.setVisible(false);
            }
        }
        }

    @Override
     public String getName(){
                  return name;
            }     
}

回答1:


You forgot to add an ActionListener.

user_name.addActionListener(handler);

I strongly suggest you to follow the Java naming conventions as well.

So user_name should be userName.




回答2:


To get a string from a JTextField, you simply need the following:

String var = jTextFieldName.getText();

This will save whatever is in the JTextField into the var variable.

The getText() is simply a method belonging to the JTextField class and returns whatever text is in it.




回答3:


try something like:

JTextArea userName_txt = new JTextArea("Enter Your Name");
userName_txt.getDocument().addDocumentListener(new DocumentListener() {
    @Override
    public void removeUpdate(final DocumentEvent paramDocumentEvent) {
        name = userName_txt.getText();
    }

    @Override
    public void insertUpdate(final DocumentEvent paramDocumentEvent) {
        name = userName_txt.getText();
    }

    @Override
    public void changedUpdate(final DocumentEvent paramDocumentEvent) {
        name = userName_txt.getText();
    }
});

or rewrite getName() to something like:

public String getName() {
    return userName_txt.getText();
} 

But you basically overwrote Component.getName() in your code which makes no sense at all, better rename your method to getUsername().



来源:https://stackoverflow.com/questions/36936186/how-to-get-string-from-jtextfield-and-save-it-in-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!