Jump to content

Recommended Posts

Posted (edited)

I'm currently trying to make a GUI for a program that has multiple interfaces i.e. One interface is open at any one time that contains buttons, depending on the button that's clicked, another interface opens. From what I can gather the easiest way to do this is to create all my separate interfaces, get them all to be setVisible(false) apart from the main one. When a button is clicked, the appropriate interface is setVisible(true). I tried doing this by creating a new instance of the interface every time the button is clicked, and setVisible(false) when I'm done with it. This doesn't seem to be very efficient as there could be 100's of the interface running in the background invisible. So I figured making one instance of the interface was better, and to hide and un-hide it.

 

Anyway, I'm struggling to figure out how to make it so that when I click on a button on the main interface, that the interface in a separate class becomes visible.

 

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class guitest extends JFrame{

private static final int WIDTH=500;
private static final int HEIGHT=500;

private JButton signIn, signOut;

private signinButtonHandler sibHandler;

public guitest(){
	
	signIn = new JButton("Sign In");
	signOut = new JButton("Sign Out");
	
	setTitle("GUI Test");
	setSize(WIDTH,HEIGHT);
	setVisible(true);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setLocationRelativeTo(null);
	
	Container pane = getContentPane();
	pane.setLayout(new GridLayout(4, 2));
	
	pane.add(signIn);
	sibHandler = new signinButtonHandler();
	signIn.addActionListener(sibHandler);
	pane.add(signOut);
}

void signIn_method(){
	
	visitor = new JButton("Visitor");
	staff = new JButton("Staff");
	
	setTitle("GUI Test");
	setSize(WIDTH,HEIGHT);
	setVisible(false);
	setLocationRelativeTo(null);
	
	Container pane = getContentPane();
	pane.setLayout(new GridLayout(4, 2));
	
	pane.add(visitor);
	pane.add(staff);
	
}

public static void main(String[] args) {
	
	guitest obj = new guitest();
	signIn gui1 = new signIn();
	gui1.signIn_method();

}

private class signinButtonHandler implements ActionListener{
	public void actionPerformed(ActionEvent e){

		
	}
}

}

class signIn extends JFrame{

private static final int WIDTH=500;
private static final int HEIGHT=500;

private JButton visitor, staff;

void signIn_method(){
	
	visitor = new JButton("Visitor");
	staff = new JButton("Staff");
	
	setTitle("GUI Test");
	setSize(WIDTH,HEIGHT);
	setVisible(false);
	setLocationRelativeTo(null);
	
	Container pane = getContentPane();
	pane.setLayout(new GridLayout(4, 2));
	
	pane.add(visitor);
	pane.add(staff);
	
}

}

Edited by Kineas
Posted
I'm no Java master but you should just be able to to call the setVisible() method from within your ActionListener. You will need to be able to access a reference to the gui you want to show/hide from within the ActionPerformed() method. Probably the easiest way to do this is to make the other gui's properties of the main gui and then to initialise them from within the constructor of the main gui rather than separately within the main method.. That way you will have a reference to them to use from within the ActionListener.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...