[英]JTextField how to read in text
In my GUI.java class, I have the following function: 在我的GUI.java类中,我具有以下功能:
public void setChat(String text)
{
chat.append(text);
}
public void getInput(String text)
{
String read = input.getText();
}
I am trying to use my getInput() method to set a username I will be using for the rest of my code, but I just can't seem to read it in correctly from the TextField in my Client.java. 我试图使用我的getInput()方法来设置将在其余代码中使用的用户名,但是似乎无法从Client.java中的TextField正确读取它。 I'm having a lot of trouble getting the original: 获取原始照片时遇到很多麻烦:
String username = gui.getInput(); 字符串用户名= gui.getInput();
(That certainly didn't work either). (那当然也不起作用)。 Can someone help me read in a TextField entry and assign it to my username String? 有人可以帮助我阅读TextField条目并将其分配给我的用户名String吗?
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String [] args) throws IOException
{
GUI gui = new GUI();
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
//GUI gui = new GUI();
}
});
String username = "";
gui.setChat(" Enter the username you would like to use for the duration of the chat session.\n");
if (username == "")
{
System.out.println("Press Any Key To Continue...");
new java.util.Scanner(System.in).nextLine();
gui.getInput(username);
gui.setChat(" Your username is now: " + username + ".\n");
}
}
}
EDIT 1 编辑1
GUI.java class: GUI.java类:
String getInput(String text)
{
return input.getText();
}
Client.java class: Client.java类:
String username;
gui.setChat(" Enter the username you would like to use for the duration of the chat session.\n");
username = gui.getInput();
System.out.println("Press Any Key To Continue...");
new java.util.Scanner(System.in).nextLine();
gui.setChat(" Your username is now: " + username + ".\n");
Errors: 错误:
Client.java:19: error: method getInput in class GUI cannot be applied to given types;
username = gui.getInput();
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
1 error
try 尝试
public void getInput(){
return input.getText();
}
then in the main 然后在主要
username = "" + gui.getInput();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.