Open In App

Redirecting From One Frame to Another Frame in Java AWT

In Java Programs, If we work with the two frames in Java if the user takes the input from one frame then it should redirect to the other frame. Now let’s create an AWT frame in the java using the java.awt package we can maintain connection for the two frames by creating the button using the ActionListener class which is present in the java.awt package by clicking on the button directs to the the second frame we can terminate any frame in the frame by using the dispose() method.

The dispose() Method terminates the frame from the memory.



we will look at a program with the above-explained details about redirecting from one AWT frame to another.

Example of Redirect to other AWT frame

The <a> tag contains the href so that when we click on text between the anchor tag it redirects to the hyperreference given. In a similar way here we work with the AWT Frames instead of Web pages. In place of <a> anchor tag, we are using buttons in our present explanation to redirect to another AWT frame.



Let’s look at an example program in Java.

Program for Frame1 by using AWT

Another important point is that the two classes implementing the two frames should be in the same directory.




// Java AWT Program for Frame 1
import java.awt.*;
import java.awt.event.*;
import java.util.*;
  
// The name of the first frame is GeeksForGeeks
public class GeeksForGeeks
    extends Frame implements ActionListener {
  
    // We create a button using the ActionListener class
    Button button;
  
    GeeksForGeeks()
    {
  
        /* By default the layout of component in AWT frame
        is BorderLayout it occupies the whole frame so we
        use FlowLayout or other Layout */
        this.setLayout(null);
  
        // This WindowAdapter class is used to close the
        // Frame
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        button = new Button("NEXT");
  
        // Font class to set the text of Button name
        button.setFont(
            new Font("Agency FB", Font.BOLD, 32));
  
        // Setting the co-ordinates and the width and height
        // of button in the Frame
        button.setBounds(250, 250, 100, 100);
        button.addActionListener(this);
  
        this.add(button);
    }
    public void actionPerformed(ActionEvent ae)
    {
  
        if (ae.getSource().equals(button)) {
  
            /* If we click the Button object named button
              then it calls the class  Geek Frame when we
              click on the button present in the
              GeeksForGeeks Frame */
            Geek g = new Geek();
            g.setTitle("Geek");
            g.setSize(500, 500);
            g.setVisible(true);
        }
        else {
  
            /* If we click any other button created in the
            GeeksForGeeks Frame other than object named
            button then it will close the frame */
            System.exit(0);
        }
    }
    public static void main(String args[])
    {
  
        GeeksForGeeks gfg = new GeeksForGeeks();
  
        // The title of our Frame is GeeksForGeeks
        gfg.setTitle("GeeksForGeeks");
        gfg.setSize(600, 600);
        gfg.setVisible(true);
    }
}

Output for Frame1:

Explanation of the above Program:

The Output for the Frame GeeksForGeeks is given below

Note: Don’t compile and run Frame1 until you compile Frame2 in Java AWT.

Program For Frame2

An important thing is that the Frame2 class doesn’t have Main Method we need to just compile that so that the Window Tool Kit Method will be called from Frame1 named “GeeksForGeeks” when we press the button “NEXT” which is present in the Frame GeeksForGeeks.




// Java Awt Program for
// Frame 2
import java.awt.*;
import java.awt.event.*;
import java.util.*;
  
public class Geek extends Frame implements ActionListener {
  
    Button back;
  
    Geek(){
        this.setLayout(null);
  
        /*We create a button named BACK in the
        Frame this will be called if we click on the button
        object in the frame GeeksForGeeks [ Previous
        program]*/
        back = new Button("BACK");
        back.setBounds(250, 250, 100, 100);
        back.setFont(new Font("Algerian", Font.BOLD, 22));
        back.addActionListener(this);
  
        // We add the button the Frame named Geek
        this.add(back);
    }
  
    // When the button is clicked this method will be
    // executed
    public void actionPerformed(ActionEvent ae){
        
        // The dispose() Method is used to terminate from
        // the memory when we click on the button
        this.dispose();
    }
}

Explanation of the above Program:

The total detailed explanation of this whole concept is illustrated in the below video just see and understand it, and the output image is also shown.

Output:

When we click on the button of Frame1 then it redirects to the Frame2.

After clicking on the button Back it terminates the Frame2 named “Geek” From the memory. The output is below.

Demo Video:

This is about working with the Multiple Frames using the Abstract Window ToolKit in Java.


Article Tags :