Hi, so I want to make this kind of "webpage" thing with GUI and how can go to another "page" not open another frame when I click on one of the buttons. Like in my program one of the buttons are "Sign In" and when the user clicks it I want it to go to another page, and the user can also go "back" to the previous page. How do I do that?
As you can also see from my program right now, there are buttons but the buttons doesn't go/do anything when it is clicked.

Thanks.

as you can also see from my program right now,

import javax.swing.*; 
import java.awt.*;
import java.awt.event.*;   
  
  public class firstPage implements ActionListener {
  JFrame frame;
  JPanel contentPane; 
  JLabel label; 
  JButton button;
  JTextField name; 
  JLabel welcome; 

  
  public firstPage(){ 
	/* Create and set up the frame */
	frame = new JFrame("Sign In page"); 
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	
	
	/* Creates and set up the size and colour of the frame */ 
	contentPane = new JPanel(); 
	contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS)); 
	contentPane.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50)); 
	contentPane.setBackground(Color.black);	 
	

	/* creates a button in the center */ 
	button = new JButton("Enter"); 
	button.setAlignmentX(JButton.CENTER_ALIGNMENT); 
	button.setActionCommand("Enter"); 
	button.addActionListener(this);  
	contentPane.add(button); 
	
	button = new JButton("Sign In"); 
	button.setAlignmentX(JButton.CENTER_ALIGNMENT); 
	button.setActionCommand("Sign In"); 
	button.addActionListener(this); 
	contentPane.add(button); 
	
	button = new JButton("Sign Up"); 
	button.setAlignmentX(JButton.CENTER_ALIGNMENT); 
	button.setActionCommand("Sign Up"); 
	button.addActionListener(this); 
	contentPane.add(button); 
	
 /* Creates a button for exit */ 
	button = new JButton("Exit"); 
	button.setAlignmentX(JButton.CENTER_ALIGNMENT); 
	button.setActionCommand("Exit"); 
	button.addActionListener(this); 
	contentPane.add(button); 
   

	frame.setContentPane(contentPane); 
	frame.pack();
	frame.setVisible(true); 
  } 
  
	public void actionPerformed(ActionEvent event) { 
	String eventName = event.getActionCommand();  
	
	if (eventName.equals("Enter")) {; 
	} else { 
	  button.setActionCommand("Enter"); 
	} 
  }
  private static void runGUI() { 
	JFrame.setDefaultLookAndFeelDecorated(true); 
	
	firstPage greeting = new firstPage(); 
  } 
  
  public static void main(String[] args) { 

	javax.swing.SwingUtilities.invokeLater(new Runnable() { 
	  public void run() { 
		runGUI(); 
	  } 
	}); 
  } 
}

Recommended Answers

All 6 Replies

If you want to navigate thru different pages, add all the componentss of 1 page in 1 panel each. Now add all the panels to your JFrame .
You can either use CardLayout to swap between the panels or panel.setVisible() method to make the required panel visible.
You can implements method with your buttons ActionListener. You will have to add back buttons on all panels and then call each ActionListener respectively

1.first you create FirstPage.java (Frame)

2.create Loign.java page

3.you will call Login.main(null) when clicking Button of FirstPage and also call dispose(); in button's actionperformed method..

4.when you clicking back button of login page you do no.3 step in current page...

what do you mean? can you show me the code?

package test.iecanvas;

/****************************************************************/
/*                      FirstPage	                            */
/*                                                              */
/****************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * Summary description for FirstPage
 *
 */
public class FirstPage extends JFrame
{
	// Variables declaration
	private JLabel jLabel1;
	private JButton jButton1;
	private JPanel contentPane;
	// End of variables declaration


	public FirstPage()
	{
		super();
		initializeComponent();
		//
		// TODO: Add any constructor code after initializeComponent call
		//

		this.setVisible(true);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always regenerated
	 * by the Windows Form Designer. Otherwise, retrieving design might not work properly.
	 * Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
	 * to retrieve your design properly in future, before revising this method.
	 */
	private void initializeComponent()
	{
		jLabel1 = new JLabel();
		jButton1 = new JButton();
		contentPane = (JPanel)this.getContentPane();

		//
		// jLabel1
		//
		jLabel1.setText("Welcome");
		//
		// jButton1
		//
		jButton1.setText("GoTo");
		jButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jButton1_actionPerformed(e);
			}

		});
		//
		// contentPane
		//
		contentPane.setLayout(null);
		addComponent(contentPane, jLabel1, 130,117,60,18);
		addComponent(contentPane, jButton1, 114,188,83,28);
		//
		// FirstPage
		//
		this.setTitle("FirstPage - extends JFrame");
		this.setLocation(new Point(0, 0));
		this.setSize(new Dimension(390, 300));
	}

	/** Add Component Without a Layout Manager (Absolute Positioning) */
	private void addComponent(Container container,Component c,int x,int y,int width,int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
	}

	//
	// TODO: Add any appropriate code in the following Event Handling Methods
	//
	private void jButton1_actionPerformed(ActionEvent e)
	{
		Login.main(null);
dispose();
	}

	//
	// TODO: Add any method code to meet your needs in the following area
	//






























 

//============================= Testing ================================//
//=                                                                    =//
//= The following main method is just for testing this class you built.=//
//= After testing,you may simply delete it.                            =//
//======================================================================//
	public static void main(String[] args)
	{
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		try
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch (Exception ex)
		{
			System.out.println("Failed loading L&F: ");
			System.out.println(ex);
		}
		new FirstPage();
	}
//= End of Testing =


}
package test.iecanvas;

/****************************************************************/
/*                      Login	                            */
/*                                                              */
/****************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * Summary description for Login
 *
 */
public class Login extends JFrame
{
	// Variables declaration
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JTextField jTextField1;
	private JPasswordField jPasswordField1;
	private JButton jButton1;
	private JButton jButton2;
	private JPanel contentPane;
	// End of variables declaration


	public Login()
	{
		super();
		initializeComponent();
		//
		// TODO: Add any constructor code after initializeComponent call
		//

		this.setVisible(true);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always regenerated
	 * by the Windows Form Designer. Otherwise, retrieving design might not work properly.
	 * Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
	 * to retrieve your design properly in future, before revising this method.
	 */
	private void initializeComponent()
	{
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jTextField1 = new JTextField();
		jPasswordField1 = new JPasswordField();
		jButton1 = new JButton();
		jButton2 = new JButton();
		contentPane = (JPanel)this.getContentPane();

		//
		// jLabel1
		//
		jLabel1.setText("UserName");
		//
		// jLabel2
		//
		jLabel2.setText("jLabel2");
		//
		// jTextField1
		//
		jTextField1.setText("jTextField1");
		jTextField1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jTextField1_actionPerformed(e);
			}

		});
		//
		// jPasswordField1
		//
		jPasswordField1.setText("jPasswordField1");
		jPasswordField1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jPasswordField1_actionPerformed(e);
			}

		});
		//
		// jButton1
		//
		jButton1.setText("LogIn");
		jButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jButton1_actionPerformed(e);
			}

		});
		//
		// jButton2
		//
		jButton2.setText("Back<<");
		jButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jButton2_actionPerformed(e);
			}

		});
		//
		// contentPane
		//
		contentPane.setLayout(null);
		addComponent(contentPane, jLabel1, 33,96,58,18);
		addComponent(contentPane, jLabel2, 31,124,60,18);
		addComponent(contentPane, jTextField1, 97,96,100,22);
		addComponent(contentPane, jPasswordField1, 80,121,100,22);
		addComponent(contentPane, jButton1, 69,200,83,28);
		addComponent(contentPane, jButton2, 188,200,83,28);
		//
		// Login
		//
		this.setTitle("Login - extends JFrame");
		this.setLocation(new Point(0, 0));
		this.setSize(new Dimension(390, 300));
	}

	/** Add Component Without a Layout Manager (Absolute Positioning) */
	private void addComponent(Container container,Component c,int x,int y,int width,int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
	}

	//
	// TODO: Add any appropriate code in the following Event Handling Methods
	//
	private void jTextField1_actionPerformed(ActionEvent e)
	{
		System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");
		// TODO: Add any handling code here

	}

	private void jPasswordField1_actionPerformed(ActionEvent e)
	{
		System.out.println("\njPasswordField1_actionPerformed(ActionEvent e) called.");
		// TODO: Add any handling code here

	}

	private void jButton1_actionPerformed(ActionEvent e)
	{
		System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
		// TODO: Add any handling code here

	}

	private void jButton2_actionPerformed(ActionEvent e)
	{
		FirstPage.main(null);
		dispose();
	}

	//
	// TODO: Add any method code to meet your needs in the following area
	//






























 

//============================= Testing ================================//
//=                                                                    =//
//= The following main method is just for testing this class you built.=//
//= After testing,you may simply delete it.                            =//
//======================================================================//
	public static void main(String[] args)
	{
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		try
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch (Exception ex)
		{
			System.out.println("Failed loading L&F: ");
			System.out.println(ex);
		}
		new Login();
	}
//= End of Testing =


}

ohh thanks. I'm gonna try this and analyze it. one more question, how come it's in a package though? what if i don't want it in package? thanks !

i tried it for you in my eclipse..

please delete package as i declared and make it correct for you..


after it is ok then please mark it as resolved...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.