使用JTextFields时如何等待用户输入 [英] How to wait for user input when using JTextFields

查看:122
本文介绍了使用JTextFields时如何等待用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近用System.out.println();构建并完成了Java程序的第一阶段,该程序可与IDE一起使用.什么都不是.现在,我想给该程序一个GUI,我遇到了一个问题.这是我的MCV示例(或者至少我认为是这样,如果不是,请告诉我).

I recently built and completed the first stage of a program in java that worked with IDE using System.out.println(); and what not. Now, I want to give this program a GUI and I ran into an issue. Here is my MCV example (or at least I think it is, let me know if it is not).

import java.net.*;
import java.net.UnknownHostException;
import java.net.NoRouteToHostException;
import java.io.*;
import net.sf.json.*;
import org.apache.commons.lang.exception.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import java.awt.Component;
import java.awt.Container;
import java.util.Scanner;


public class WOT_PlayerComp_V2
{

   public static JFrame f;
   public static JLabel resultLabel;
   public static JPanel p;
   public static JTextField t;
   public static String comparisonResults = "";
   public static String holder = "";
   public static String playerName = "";
   public boolean gameIsStillRunning = true;

   public static void testGUI()
   {
      f = new JFrame();

      addComponentsToPane(f.getContentPane());



      f.setSize(300, 400);
      f.setLocation(200, 200);
      // f.setResizable(false);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setVisible(true);

   }

   public static void addComponentsToPane(Container c)
   {
      c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));

      p = new JPanel();
      t = new JTextField(15);
      //t.setDocument(new JTextField_wLimit(50));
      //You can ignore this line, it is what I used to limit the input of my textfield
      resultLabel = new JLabel();


      //html makes the br have purpose, p makes it wrap
      resultLabel.setHorizontalAlignment(SwingConstants.CENTER);
      resultLabel.setVerticalAlignment(SwingConstants.TOP);
      resultLabel.setSize(50, 50);

      c.add(t);
      c.add(resultLabel);


   }

   public WOT_PlayerComp_V2()
   {

      testGUI();

   //... Lots of irrelevant code

      resultLabel.setText("Enter your username.");

      while(gameIsStillRunning)
      {

      //irrelevant code

         playerName = t.getText();//scans user input
         t.setText("");

      //more irrelevant code
      }

      //if(playerName == null || playerName.equals(""))
      //^^ it shouldn't go into here, I want my while loop to wait 
      //for user input like it did when i was using a Scanner object
      //

   }

   public static void main(String[] args)
   {

      WOT_PlayerComp_V2 w = new WOT_PlayerComp_V2();

   }

}

基本上,在我使用JTextField对象之前,我使用的是Scanner对象. Scanner对象实际上将停止我的while循环并运行该方法,然后继续.我想知道是否可以使用JTextField.我希望能够在中间停止for循环并等待用户输入.上面的MCV示例似乎没有什么大问题,但是我使用的代码很大程度上基于输入,并且会响应用户输入null或空字符串.结果,它将按照预期的方式执行操作,响应正确的用户输入,并且仅持续进行一次迭代,然后将继续从JTextField获取输入,因为它处于while循环中.那是我的问题.

Basically, prior to me using a JTextField object, I was using a Scanner object. The Scanner object would actually stop my while loop and run that method, then continue. I want to know if this is possible with a JTextField. I want to be able to stop the for loop in the middle and wait for the user input. It doesn't seem as big a problem with the MCV example I have above, but the code I am using is based heavily on input and will respond to a user entering null or empty strings. As a result, it will do what it was meant to do, respond to correct user input, and this will only last for one iteration, then it will keep taking input from the JTextField, since it is in a while loop. That's my problem.

推荐答案

GUI程序不能像在控制台程序中那样工作.没有正在等待用户输入" ,就像使用Scanner一样.使用Swing,事件会通过用户的不同交互来触发.这是您的工作,程序员需要通过将适当的侦听器添加到触发您要侦听的事件的组件中来侦听这些事件.例如,您可以在文本字段上侦听ActionEvent(当用户点击 enter 时会触发该事件.您可以在文本字段中添加ActionListener来侦听这些事件.当该事件发生时,您可以进行一些处理.

GUI programs don't work like this, as they do in console programs. There is no "waiting for user input" like using Scanner. With Swing, events are fired by different interations from the user. It is your job, as the programmer to listen for these events by adding the appropriate listener to the component that fires the event you want to listen for. For instance, you can listen for an ActionEvent on the text field (the event is fired when the user hits enter. You add an ActionListener to the text field that will listener for those events. You can do then do some processing when that event occurs.

final JTextField field = new JTextField(20);
field.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        String text = field.getText();
        System.out.println(text);
        field.setText(text);
    }
});

请花一些时间浏览编写事件监听器

这篇关于使用JTextFields时如何等待用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆