簡體   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