我正在制作一个简单的儿童游戏,要求用户输入他/她的名字
JTextField中,并且该名称将在游戏结束后显示在其他班级中
我创建了一个新对象,并用它来调用方法getName,但是当我调用该方法时,它将返回null
我希望它返回用户输入的名称
请任何建议?

这是代码:



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。

user_name.addActionListener(handler);


我强烈建议您也遵循Java naming conventions

所以user_name应该是userName

10-05 23:36