I am sure at the end of reading answers I will feel like a complete idiot, but I need help anyways. I am trying to create my first Java app with switching between forms. I am using IntelliJ IDE and using provided form designer I created some forms. My wish is to be able to switch between forms when clicking proper buttons. I tried to find answers to my questions but my problems come from my lack of experience and some basic knowledge about how to properly implement forms. I have found this, for example:
btnLogin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
this.setVisible(false);
new FrmMain().setVisible(true); // Main Form to show after the Login Form..
}
});
And I am sure using this would help me achieve what I want to achieve, BUT this is not working. Maily because I have no idea what form's class file actually should contain. While making a single-form app I don't need to extend JFrame in form class, but I suppose (?) I need to when I am trying to switch between forms with this method? I don't know what should appear in contructors, I don't know if I need main functions (don't really think so) in classes other than the starting form's class. I will paste what I have now. Feel free to educate me on what should it look like. Right now I am unable to close the menu form and while opening the other form there are no contents (buttons etc, don't know why).
First form:
package ImplementedUCs;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MenuForm extends JFrame
{
private JPanel MenuPanel;
private JButton loginButton;
private JButton profileButton;
private JButton conferencesButton;
private JButton usersButton;
private JButton settingsButton;
private JButton exitButton;
private JLabel titleLabel;
private JLabel logLabel;
private JLabel userLabel;
public static boolean loginStatus = false;
public static UserTypes loginType = UserTypes.NOT_LOGGED_IN;
public static MenuForm myMenuForm;
public MenuForm()
{
myMenuForm = this;
logLabel.setText("You are currently not logged in!");
userLabel.setText("Please log in to use this application!");
loginButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// want to close/hide menu form here
new LoggingForm().setVisible(true);
}
});
}
public static void main(String[] args)
{
JFrame menuFrame = new JFrame("MenuForm");
menuFrame.setContentPane(new MenuForm().MenuPanel);
menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuFrame.pack();
menuFrame.setTitle("ConferenceStation v1.0");
menuFrame.setSize(800,600);
menuFrame.setLocationRelativeTo(null);
menuFrame.setResizable(false);
menuFrame.setVisible(true);
}
}
Second form:
package ImplementedUCs;
import javax.swing.*;
public class LoggingForm extends JFrame
{
private JPanel loggingPanel;
private JTextField loginField;
private JPasswordField passwordField;
private JButton remindButton;
private JButton noAccButton;
private JButton loginButton;
private JLabel titleLabel;
private JPanel properPanel;
private JLabel loginLabel;
private JLabel passwordLabel;
private JLabel noAccLabel;
private JLabel forgotLabel;
public LoggingForm()
{
this.setTitle("ConferenceStation v1.0");
this.setSize(800,600);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
}
}
I am really trying to understand how it should look like and how to make it work, please, I need help.