Hi, I'm trying to make a menu and when you click on one of the JButtons, it'll go from my first class's GUI to my second class's GUI. But when I run the program, and I press the button algorithm, it changes to a blank screen, and not to the second GUI. I tested my first GUI to go to a simpler one that had a couple of buttons and it worked, but with my actual GUI, it does not work and so I think there's something wrong with the code in SecondaryFrame.

This is the code for the first GUI's actionPerformed method

public void actionPerformed(ActionEvent event)
	{

		if(event.getSource() == algorithm)
		{
	
			CryptoSecondaryFrame secondary = new CryptoSecondaryFrame();
			secondary.setVisible(true);
			this.setVisible(false);
		}
	}

Right now, the code in SecondaryFrame is set up to go back to but eventually, I'll be adding the other buttons to go to another GUI.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CryptoSecondaryFrame extends JPanel implements ActionListener 

{
	
	private JLabel heading;
	private JPanel primary, rightSubPanel, leftSubPanel;
	private JButton publicKey, symmetric, hash;
	
	public CryptoSecondaryFrame()
	{
		setLayout(new BorderLayout());
		
		//heading label and panel
		heading = new JLabel("Cryptography", 

SwingConstants.CENTER);
		JPanel headingPanel = new JPanel();
		headingPanel.setPreferredSize(new Dimension(700, 40));
		headingPanel.setBackground(new Color(65,105,225));
		
		primary = new JPanel();
		primary.setPreferredSize(new Dimension(710, 400));
		primary.setBackground(Color.LIGHT_GRAY);
		primary.setLayout(new BorderLayout());
		
		//setting up the buttons
		publicKey = new JButton("Public Key Algorithms");
		publicKey.addActionListener(this);
		symmetric = new JButton("Symmetric Algorithms");
		hash = new JButton("Hash Functions");
	
		//right sub panel
		rightSubPanel = new JPanel();
		rightSubPanel.setPreferredSize(new Dimension(400, 300));
		rightSubPanel.setBackground(Color.cyan);
		rightSubPanel.setLayout(new BorderLayout());

		//left sub panel		
		leftSubPanel = new JPanel();
		leftSubPanel.setPreferredSize(new Dimension(300, 300));
		leftSubPanel.setBackground(Color.gray);
		leftSubPanel.setLayout(new BoxLayout(leftSubPanel, BoxLayout.Y_AXIS));
		
		//adding the buttons to left sub panel
		leftSubPanel.add(Box.createRigidArea(new Dimension(0, 20)));
		leftSubPanel.add(publicKey);
		leftSubPanel.add(Box.createRigidArea(new Dimension(0, 20)));
		leftSubPanel.add(symmetric);
		leftSubPanel.add(Box.createRigidArea(new Dimension(0, 20)));
		leftSubPanel.add(hash);
					
		//adding the panels
		add(headingPanel, BorderLayout.NORTH);
		add(leftSubPanel, BorderLayout.WEST);
		add(rightSubPanel, BorderLayout.EAST);
		headingPanel.add(heading, BorderLayout.NORTH);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent event)
	{
		if (event.getSource() == publicKey)
		{
			CryptoMainFrame main = new CryptoMainFrame();
			main.setVisible(false);
			this.setVisible(true);
		}
	}
}

You call it a CryptoSecondaryFrame but actually it's a JPanel - you can't display that unless you add it to a frame or window of some sort.

Thanks, it worked. Not sure why but it didn't work adding them a JFrame but did with a JPanel.

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.