Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Help switching from window to window

Started by Grahf750 Feb 22, 2005 at 3:17 PM 3 replies 850+ views
Original Post
Grahf750
Grahf750
alright I got the gui stuff down with buttons, text fields, ect. now I am having troubles with the actions affecting the rest of my code. I built the class that does my gui seperate from the rest of my code then just created an object for it in the beginning of my other code. I have a switch so that if the player clicks one of the three buttons on the main menu it will take you to a different screen. So when you click the button it sets the variable selection to 1, 2, or 3 depending on which button you click. I have a getSelection method in the gui class which I then use with my switch statement but it automatically runs before the person can press a button and so it returns 0. I was thinking I could put everything in my main menus actionPerformed() but that didn't seem right to me. I have been trying many different things and I have looked on google and some books for some help but I think I am just totally lost. [Edited by - Grahf750 on February 22, 2005 3:44:16 PM]
H_o_p_s
H_o_p_s
I am using to JFrames, and when I press 'T' it sets the visibility of the Tools windows to true, and then sets the visibility of the Main window to false... No problems here..

case KeyEvent.VK_T:	if (j.t.isVisible())		j.t.setVisible(false);		j.f.setVisible(true);	else {		j.t.setVisible(true);		j.f.setVisible(false);	}


I have the main application class 'j' and then two frames 't' and 'f' where 'f' is the main frame, and 't' is the tools frame. In order for the the actions in t to interact with data in j I do this for the constructor in t:
class Tools {	J_app j;	public Tools(J_app j) {		this.j = j;		//Add buttons and stuff	}	//Add listeners and such}


Hope this helps!
BRING BACK THE BLACK (or at least something darker)
Grahf750
Grahf750
I don't really understand your second part could you explain it a little more? Maybe with a small but more in depth example. I did fix the first part. My second object I created was a panel and not a jframe :).
H_o_p_s
H_o_p_s
This should be easier to understand:
class J_app {   public MainFrame f; //an extended JFrame with GUI information in the constructor   public Tools     t; //an extended JFrame with GUI information in the constructor   public static void main(String[] args) {      f = new MainFrame(this);      t = new Tools(this);   }   class MainFrame extends JFrame implements ActionListener {      J_app   j;      JButton btToggle;            public MainFrame(J_app j) {         this.j = j;         btToggle = new JButton( "Toggle"  );         btToggle.addActionListener( this );         this.getContentPane().add( btToggle );         this.setVisible(true);      }      public void actionPerformed( ActionEvent e ) {         if (e.getSource() == btToggle) {            if (j.t.isVisible()) {               j.t.setVisible(false);               j.f.setVisible(true);            } else {               j.t.setVisible(true);               j.f.setVisible(false);            }         }      }   }   class Tools extends JFrame implements ActionListener {      J_app   j;      JButton btToggle;            public MainFrame(J_app j) {         this.j = j;         btToggle = new JButton( "Toggle"  );         btToggle.addActionListener( this );         this.getContentPane().add( btToggle );      }      public void actionPerformed( ActionEvent e ) {         if (e.getSource() == btToggle) {            if (j.t.isVisible()) {               j.t.setVisible(false);               j.f.setVisible(true);            } else {               j.t.setVisible(true);               j.f.setVisible(false);            }         }      }   }}

I think that works....
BRING BACK THE BLACK (or at least something darker)
Addictman
Addictman
Depending on how you implement your application and its gui,
CardLayout might be what you are looking for.
Development blog, The Omega Sector -- http://www.omegasector.org

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.