本文介绍了如何从JTextField获取字符串并将其保存在变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作一个简单的儿童游戏,要求用户在JTextField
中输入他/她的名字,并且该名字将在游戏结束后显示在其他班级中.
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.
我创建了一个新对象,并用它来调用方法getName
,但是当我调用该方法时,它会返回null
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.
这是代码:
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;
}
}
推荐答案
您忘记添加ActionListener.
You forgot to add an ActionListener.
user_name.addActionListener(handler);
我强烈建议您也遵守 Java命名约定
所以user_name
应该是userName
.
这篇关于如何从JTextField获取字符串并将其保存在变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!