I am writing a java application that makes use of swing. The two main methods i am using are called Main and logInScreen. I have a variable in login screen that is initially set to "Empty", but will then be set to "Y" if a button is pressed using a setter(inside of an action listener). The problem i'm having is that i can't access this value in the main method by using a getter. Does anyone know what the problem is?
Here is the relevent code from main:
while (cont == true) {
answer = login.getEntered();
if (answer.equals("Y")) {
cont = false;
} else {
cont = true;
}
}
Here is the code for the GUI:
package com.company;
/*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class logInScreen extends JFrame {
private static String usernameInput = "";
private static String passwordInput = "";
private static JPanel panel = new JPanel();
private static String entered = "";
public logInScreen() {
setEntered("empty");
this.setTitle("Log In");
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
usernameArea();
passwordArea();
button("Log In");
createNewAccount();
this.add(panel);
this.setVisible(true);
}
public static void usernameArea() {
JTextArea username = new JTextArea(1, 15);
username.setText("");
username.setLineWrap(true);
JScrollPane pane = new JScrollPane(username, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(pane);
}
public static void passwordArea() {
JTextArea password = new JTextArea(1, 15);
password.setText("");
password.setLineWrap(true);
JScrollPane pane2 = new JScrollPane(password, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(pane2);
}
public void button(String text) {
JButton btn = new JButton();
btn.setText(text);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setEntered("Y");
System.out.println(getEntered());
}
});
panel.add(btn);
}
public static void createNewAccount(){
JTextArea label = new JTextArea(1,30);
label.setLineWrap(true);
label.setEditable(false);
label.setText(" New User?");
panel.add(label);
JButton newAccount = new JButton();
newAccount.setText("Create New Account!");
newAccount.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createLoginScreen newAccount = new createLoginScreen();
}
});
panel.add(newAccount);
}
public void setEntered(String val){
entered = val;
}
public String getEntered() {
return entered;
}
public String getUsernameInput() {
return usernameInput;
}
public String getPasswordInput() {
return passwordInput;
}
public void close() {
this.setVisible(false);
this.dispose();
}
}