Java Applet program to create different screen and link them together
Hi,
I'm very new to java, and currently learning some of the basics.
What i need help on is how to program in java so that i can change from one window to another. Not really sure how to word this but here goes:
For example, when i start the java applet, a login screen (window1) will appear, when a user enters username and password, when the user is verified, it "switches" to a different window (window2) to access the data.
Any help much appreciated.
Thanks in advance,
-channi3
Re: Java Applet Switching To Different Windows?
Hello channi3 and welcome to the forums.
Take a look at this code example and compile it.
There is a main JFrame with a button on it. When this button is clicked, JFrame1 closes and opens JFrame2.
Its just a matter of invoking the run method for each form when an action is performed and setting the visibility of the previous form to false.
Code :
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Channi3 {
public static JFrame frame1 = new JFrame("Java Frame1");
public static JFrame frame2 = new JFrame("Java Frame2");
//Form1
private static void createAndShowGUI1() {
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("HI! IM JFRAME1 - CLICK ME TO OPEN FRAME 2");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
[B]frame1.setVisible(false);[/B]
System.out.println("Opening JFrame 2");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
[B]createAndShowGUI2();[/B]
}
});
}
});
frame1.getContentPane().add(button);
frame1.pack();
frame1.setVisible(true);
}
//Form2
private static void createAndShowGUI2() {
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("HI! IM JFRAME2 - CLICK ME TO OPEN FRAME 1");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
[B]frame2.setVisible(false);[/B]
System.out.println("Opening JFrame 1");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
[B]createAndShowGUI1();[/B]
}
});
}
});
frame2.getContentPane().add(button);
frame2.pack();
frame2.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI1();
}
});
}
}