Results 1 to 4 of 4
  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Question How to call a class when push Button?

    I have made a GUI screen.
    And there is text and a button on it.
    Now when I click on that button, I ould like another GUI screen to be opened and close the first one.
    Now I have these 2 screens already programmed (2nd is not completly finished).
    But I don't get it haw to activate the 2nd screen...when you push the button.

    2nd screen:
    Java Code:
     
    	public static void secondFrame() {
    		Frame frame2 = new Frame("Frame 2");
    		JLabel name = new JLabel("Name: ");
    		frame2.setSize(500,100);
    		frame2.setVisible(true);
    	}

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you extended from JFrame you can just call the constructor in the buttons action handle.

    Java Code:
    new Frame2();
    Also, how would you create a frame in main? Do that in your action handling for the button.

  3. #3
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    1. Create a button // JButton but = new JButton("But1");
    2. Assign an action listener to the button but.addActionListener(new But1Listener());
    I like to create separate listener files for each button. if you decide to do that:
    3. Create a file that implements action listener
    Java Code:
    public class But1Listener implements ActionListener{
    
    public void actionPerformed(ActionEvent e){
     Frane1.showFrame2();
    }
    }
    4. in showFrame2 method you should create your second frame:
    Java Code:
    public void showFrame2(){
    secondFrame sf = new secondFrame();
    }
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Now when I click on that button, I ould like another GUI screen to be opened and close the first one.
    Take a look at CardLayout.

    db

Similar Threads

  1. how do i call a class from main class
    By revanger in forum New To Java
    Replies: 7
    Last Post: 07-20-2010, 05:32 PM
  2. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  3. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  4. How can I call method from class in other class??
    By Hisham in forum New To Java
    Replies: 6
    Last Post: 02-14-2010, 03:49 PM
  5. SWT push button
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-31-2007, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •