繁体   English   中英

您如何从 Jtextfield 中的用户那里获得 2 个输入?

[英]How do you obtain 2 inputs from a user in Jtextfield?

我希望用户输入玩家一的名字,然后按回车键,然后文本字段再次为空白,以便用户可以输入玩家 2 的名称。到目前为止,我只能输入玩家的名字,但不能输入玩家二。 出于某种原因,我的代码无法正常工作。 任何帮助将不胜感激。 提前致谢。

import java.lang.*;
import java.util.*;
import java.util.List;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class mainClass extends JPanel implements ActionListener {
    
    //constant variables to use

    static String playerOneName;
    static String playerTwoName;
    static boolean playerOneNameSet;
    static boolean playerTwoNameSet;
    
    
    private static final long serialVersionUID = 1L;
    
    public mainClass() {
        
    }
    
    public void paintComponent(Graphics g) {
        
        //set background color to white
        g.setColor(Color.white);
        g.fillRect(0, 0, getWidth(), getHeight());
        
      }

    public static void initializeBoard() {
    }
    public static void main(String[] args) {
        // title of frame
        JFrame frame = new JFrame("Risk");

            JTextField textField = new JTextField(20);
            frame.add(textField, BorderLayout.SOUTH);
            
            JLabel welcome = new JLabel("");
            welcome.setText("Please Enter name for Player 1 in the text box at the bottom");
            frame.add(welcome,BorderLayout.NORTH);
            
            //action listener listens for enter key
            textField.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    playerOneName= textField.getText();
                    System.out.println(playerOneName);
                    playerOneNameSet = true;
                    System.out.println(playerOneNameSet);
                }
            });
            if(playerOneNameSet == true) {
            JTextField textField2 = new JTextField(20);
            frame.add(textField2, BorderLayout.SOUTH);
            
            JLabel welcome2 = new JLabel("");
            welcome2.setText("Please Enter name for Player 2 in the text box at the bottom");
            frame.add(welcome2,BorderLayout.NORTH);
            
            //action listener listens for enter key
            textField2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    playerTwoName= textField2.getText();
                    System.out.println(playerTwoName);
                    playerTwoNameSet = true;
                    System.out.println(playerTwoNameSet);
                }
            });
            }
            
        
        
        // make sure it closes correctly
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //frame size in pixels
        final int FRAME_WIDTH = 1000;    
        final int FRAME_HEIGHT = 700;
        frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
        
        // makes sure the frame is visible
        frame.setVisible(true);
        mainClass main = new mainClass();
        frame.add(main);
        
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        
    }
}

您的代码中有一个逻辑错误。

如果您只想为两个输入使用一个JTextField ,则不需要创建两个JTextField 只需处理ActionEvent并仅更新该JTextField 这是代码:

    textField.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            if (!playerOneNameSet)
            {
                playerOneName = textField.getText();
                textField.setText("");
                welcome.setText("Please Enter name for Player 2 in the text box at the 
                                 bottom");
                playerOneNameSet = true;
            }
            else
            {
                playerTwoName = textField.getText();
                textField.setText("");       
            }
         }
     });

之后的if部分不是必需的。 它还将消除playerTwoNameSet的使用。

如果你想使用两个JTextField ,那么你必须在一开始就正确地做到这一点,没有任何逻辑缺陷。

详细逻辑错误:

让我尝试向您展示您的程序流程。

    public static void main(String[] args)
    {
        // title of frame
        JFrame frame = new JFrame("Risk");

        .
        .
        .
        .

        
        //action listener listens for enter key
        textField.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                playerOneName= textField.getText();
                System.out.println(playerOneName);
                playerOneNameSet = true;
                System.out.println(playerOneNameSet);
            }
        });

到这里为止,您的代码都很好。 在上述行之后,会发生这种情况

        if(playerOneNameSet == true)    //It is never executed

发生这种情况的原因是playerOneNameSet是一个static变量,其默认值为false 这一行只执行一次。 创建 GUI 后,将不会再次调用 main() 方法,直到您再次运行它。 之后,控件传递给创建的一个JTextField ,当任何ActionEvent生成时也是如此。 之后的if行将永远不会 go 。

我希望我对你有所帮助。 对任何进一步的问题发表评论。

暂无
暂无

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

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