Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm coding a little java gui register project for practice. I've built the register Jframe and i want to move to a login Jframe by pressing a button. my question is, i remember in C# asp.net for example if you want to move to another page you would write
C#
Response.Redirect("nextPage.aspx");

Is there a way in java to do the same?

What I have tried:

i couldn't find answer for this over the internet.
Posted

1 solution

In Java, you can do something similar by using the 'CardLayout' for switching between different panels using Java Swing method. Example code -
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame extends JFrame {
    private CardLayout cardLayout;
    private JPanel cardPanel;

    public MainFrame() {
        setTitle("Login App");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create the CardLayout Method...
        cardLayout = new CardLayout();
        cardPanel = new JPanel(cardLayout);

        //Create and add your login panel...
        JPanel loginPanel = createLoginPanel();
        cardPanel.add(loginPanel, "login");

        //Create and add your next page panel...
        JPanel nextPagePanel = createNextPagePanel();
        cardPanel.add(nextPagePanel, "nextPage");

        add(cardPanel);

        //Create a button to switch to the next page...
        JButton nextPageButton = new JButton("Go to Next Page");
        nextPageButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(cardPanel, "nextPage");
            }
        });

        //Add the button to the login panel...
        loginPanel.add(nextPageButton);
    }

    private JPanel createLoginPanel() {
        JPanel loginPanel = new JPanel();
        //Add your login components here...
        return loginPanel;
    }

    private JPanel createNextPagePanel() {
        JPanel nextPagePanel = new JPanel();
        //Add components for the next page here...
        return nextPagePanel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
}


You can learn more at - Java Swing Tutorial | Tutorial[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900