To start I am just playing with buttons and JFrame. The issue is that I can switch from one Jframe to the next with a few lines of code here...
JButton studentLoginButton = new JButton("Student");
studentLoginButton.setBounds(85, 80, 80, 20);
studentLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(studentLoginButton);
BUT when I set the student JFrame as visible and use the code here...
JButton cancelButton = new JButton("Go Back");
cancelButton.setBounds(205, 80, 80, 20);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
StudentFrame.super.setVisible(false);
login1.setVisible(true);
}
});
add(cancelButton);
to cancel and go back it does not work. Nothing shows and the application is not terminated. What could I do to solve this issue? I can provide more code if it helps others find a solution.
It looks like more code would help to here are both classes. LoginFrame.java...
package system;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
StudentFrame student;
public static void main(String[] args) {
new LoginFrame().setVisible(true);
}
LoginFrame(){
super(" User Login ");
setSize(400, 80);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
StudentFrame student = new StudentFrame(null);
setLayout(new FlowLayout());
//student
JButton studentLoginButton = new JButton("Student");
studentLoginButton.setBounds(85, 80, 80, 20);
studentLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(studentLoginButton);
//Dept. staff
JButton deptStaffLoginButton = new JButton("Department Staff");
deptStaffLoginButton.setBounds(85, 80, 80, 20);
deptStaffLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(deptStaffLoginButton);
//Instructor
JButton InstructorLoginButton = new JButton("Instructor");
InstructorLoginButton.setBounds(85, 80, 80, 20);
InstructorLoginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
LoginFrame.super.setVisible(false);
student.setVisible(true);
}
});
add(InstructorLoginButton);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
and then the next is... StudentFrame.java package system;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class StudentFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
static LoginFrame login1;
public static void main(String[] args) {
new StudentFrame(login1).setVisible(true);
}
StudentFrame(LoginFrame login){
super(" User Login ");
setSize(600, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
login1 = login;
// LoginFrame login = new LoginFrame();
JButton loginButton = new JButton("Login");
loginButton.setBounds(85, 80, 80, 20);
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// LoginFrame.super.setVisible(false);
// student.setVisible(true);
}
});
add(loginButton);
JButton cancelButton = new JButton("Go Back");
cancelButton.setBounds(205, 80, 80, 20);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
StudentFrame.super.setVisible(false);
login1.setVisible(true);
}
});
add(cancelButton);
JLabel passLabel = new JLabel("Password: ");
passLabel.setBounds(10, 50, 80, 25);
add(passLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(85, 50, 240, 25);
add(passwordText);
JLabel userLabel = new JLabel("User name: ");
userLabel.setBounds(10, 10, 80, 25);
add(userLabel);
JTextField userNameText = new JTextField();
userNameText.setBounds(85, 10, 240, 25);
add(userNameText);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}