问题
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();
}
}
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.
回答1:
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);
}
});
Please take some time to go over Writing Event Listeners
来源:https://stackoverflow.com/questions/26198989/how-to-wait-for-user-input-when-using-jtextfields