![](/img/trans.png)
[英]How To Take The Value From A JTextField And Move It Into A Int Variable
[英]Variable won't take data from JTextField
我是編碼的新手,正在通過首先檢查索引文件是否存在(其中有密碼),然后閱讀它的內容並要求輸入密碼來工作,以獲取一種便宜且易於登錄的代碼來工作用戶的用戶名和密碼。 如果該文件不存在或為空,則應提示用戶輸入從那時起要使用的兩個字段,並將它們寫入索引文件。
但是,我遇到的問題是,我設置的用於運行代碼以將這兩個字段寫入文件的按鈕似乎在字符串中找不到任何內容,因此無法寫入任何內容。 我在下面添加了代碼,如果我忘記了任何內容,可以添加更多代碼。
以下是GUI窗口和動作偵聽器的代碼。
public class GUIComponents extends JFrame{
public static String UserN = " ";
public static String PassW = " ";
public static void LoginScreen(){
JFrame LoginFrame = new JFrame("Log in Menu");
JPanel LoginInfo = new JPanel();
JPanel LoginAttempt = new JPanel();
JButton LoginOkay = new JButton("Log in");
LoginListenerClass loginListen = new LoginListenerClass();
LoginOkay.addActionListener(loginListen);
JButton LoginCancel = new JButton("Cancel");
CancelListenerClass cancelListen = new CancelListenerClass();
LoginCancel.addActionListener(cancelListen);
JLabel usern = new JLabel("Username:");
JLabel passw = new JLabel("Password:");
JTextField user = new JTextField(15);
JTextField pass = new JTextField(15);
LayoutManager LoginLayout = new GridLayout(2,2,5,10);
LayoutManager OkayCancel = new FlowLayout();
LoginInfo.setLayout(LoginLayout);
LoginAttempt.setLayout(OkayCancel);
LoginInfo.add(usern);
LoginInfo.add(user);
LoginInfo.add(passw);
LoginInfo.add(pass);
LoginAttempt.add(LoginOkay);
LoginAttempt.add(LoginCancel);
if(Login.firstTimeCheck()==true){
JOptionPane.showMessageDialog(null, "This is your first time using this program. \nPlease enter the Username"
+ " and Password\n you wish to set for all users.");
}
LoginFrame.add(LoginInfo, BorderLayout.CENTER);
LoginFrame.add(LoginAttempt, BorderLayout.SOUTH);
LoginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LoginFrame.setLocationRelativeTo(null);
LoginFrame.pack();
LoginFrame.setVisible(true);
UserN = user.getText();
PassW = pass.getText();
}
}
class LoginListenerClass implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(Login.firstTimeCheck()==false){
Login.passCheck();
}
else{
try {
Login.CreateLogin(GUIComponents.UserN, GUIComponents.PassW);
System.out.println(GUIComponents.UserN+" "+ GUIComponents.PassW);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
class CancelListenerClass implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
下面是登錄功能的代碼:
public class Login {
private static String UserName; //true username
private static String Password; //true password
private static boolean accepted = false;
private static String user="";
private static String pass="";
private static boolean firstTime = true;
Login(){
}
public static void CreateLogin(String user, String pass) throws IOException{
UserName = user;
Password = pass;
try{
FileWriter FW = new FileWriter("Index.txt");
PrintWriter output = new PrintWriter(FW);
output.println(UserName);
output.println(Password);
output.close();
FW.close();
}
catch(IOException e){
e.printStackTrace();
}
}
public static void LogIn(){
try{
FileReader FR = new FileReader("Index.txt");
firstTime = false;
Scanner sc = new Scanner(FR);
user = sc.nextLine();
pass = sc.nextLine();
if(user.equalsIgnoreCase("")){
firstTime = true;
}
if(pass.equals("")){
firstTime = true;
}
}
catch(FileNotFoundException e){
firstTime = true;
}
GUIComponents.LoginScreen();
}
public static void passCheck(){
if(UserName.equalsIgnoreCase(user)){
if(Password.equals(pass)){
accepted = true;
}
}
else{
JOptionPane.showMessageDialog(null,"Your Username and Password do not /n match our files."
+ "Please try again.");
}
}
public static boolean firstTimeCheck(){
return firstTime;
}
}
如果有人能弄清楚我在這種情況下做了什么愚蠢的事情,請告訴我。 如果代碼太長且令人討厭閱讀,我深表歉意。
在您的代碼中:
public static void LoginScreen(){
// ............ code deleted to simplify things
JTextField user = new JTextField(15);
JTextField pass = new JTextField(15);
// ..... code removed to simplify things
LoginFrame.pack();
LoginFrame.setVisible(true);
UserN = user.getText();
PassW = pass.getText();
}
您正在嘗試在創建GUI時且用戶沒有機會在其中輸入任何內容之前從JTextField中提取文本。 要解決此問題,請不要在創建GUI時提取文本,而應該是由於某個事件(例如,在按下JButton時將激活的JButton的ActionListener或在Enter時將激活的JTextFields的ActionListener)當文本字段具有焦點時按。
順便說一句,您的代碼會遭受過度使用靜態修飾符的困擾,建議您在嘗試創建Swing GUI 之前先學習如何創建真正的OOP類和Java基礎知識。 您不會后悔的。
在偵聽器類actionPerformed方法中讀取UserN和PassW值。 否則,您將在初始化時讀取值為空字符串的值。
您定義的變量
public static String UserN = " ";
public static String PassW = " ";
不會從文本字段中獲取最新值。
提議的解決方案(不是理想的,但是對於您的代碼而言)
像在GuiComponents
類中一樣,將Jtext字段設為靜態公共
public static JTextField user; public static JTextField pass;
在您的LoginListener
Login.CreateLogin(GUIComponents.user.getText(),GUIComponents.pass.getText());
System.out.println(GUIComponents.user.getText() + GUIComponents.pass.getText());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.