|
||
|
|||
|
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
|
|
||||
|
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. Java 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)
{
frame1.setVisible(false);
System.out.println("Opening JFrame 2");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI2();
}
});
}
});
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)
{
frame2.setVisible(false);
System.out.println("Opening JFrame 1");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI1();
}
});
}
});
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();
}
});
}
}
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight] Forum Tip: Add to peoples reputation ( ) by clicking the button on their useful posts.
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| servlet applet communication | prashanthi_asn | Java Servlet | 0 | 21-05-2009 05:50 AM |
| RLC Circuit Java Applet | syxxpac316 | Java Applets | 6 | 13-05-2009 09:26 PM |
| [SOLVED] merging java classes | madkris | Object Oriented Programming | 11 | 16-03-2009 01:02 PM |
| FileNotFoundError when working with xml files in windows | ochieng | File I/O & Other I/O Streams | 1 | 14-11-2008 11:56 AM |
| Calculator-applet | AnithaBabu1 | Java Applets | 0 | 16-08-2008 04:42 AM |