Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with a GUI Concept

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Help with a GUI Concept

    Hi there I'm new to Java so excuse and mistakes I make and help me to to understand certain concepts.
    I have recently decided I wanted to use a GUI as the equivalent as a main menu using JButtons to run other classes main methods but i do not know the method to call out the main method though use of a action listener.
    for example:
    // this action listener would send a message to the screen
    button.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    System.out.println("You clicked the button");
                }
    an example of what I mean is if you had two classes called 'Class1' & 'Class2' and within class one with the main method was a simple system.out.println how could I call the classes main method though the click of a button in a GUI in 'Class2' within the same package.
    public class Class1
    {
        public static void main(String arg[])
        {
        System.out.println("You clicked the button from another class");
    }
    }
    This isn't a very detailed explanation but if you can help me in anyway its very very much appriciated.
    Thanks


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Help with a GUI Concept

    Hello, UnKnown Conceptz, and welcome to the forums!

    Firstly, make sure that you don't post your questions in this subforum; it is meant solely for introducing yourself to other members of the community. The appropriate subforum for you question would be AWT / Java Swing.

    Now, pertaining to your question: the syntax for calling methods in another class is as follows...

    If the method is static, use className.methodName().
    If the method is non-static, use instanceOfClass.methodName().

    main() is a static method, so to call it you must use the name of the class, then a period (.), and then "main()".

    If you have further questions be sure to post in the appropriate forum.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. The Following User Says Thank You to snowguy13 For This Useful Post:

    UnKnown Conceptz (February 23rd, 2012)

Similar Threads

  1. The Concept of Entities -- Can't wrap my mind around it.
    By RaustBD in forum Java Theory & Questions
    Replies: 2
    Last Post: November 21st, 2011, 08:42 PM
  2. Java oops concept video for 10 min
    By jessie143143 in forum The Cafe
    Replies: 1
    Last Post: October 10th, 2011, 06:50 AM
  3. Need help with a interface concept
    By drakenlord in forum Object Oriented Programming
    Replies: 2
    Last Post: September 13th, 2011, 02:17 PM
  4. Doubt in Encapsulation concept
    By nareshn in forum Object Oriented Programming
    Replies: 1
    Last Post: January 19th, 2011, 02:20 AM
  5. [SOLVED] The concept of Server and Client
    By rendy.dev in forum Java Theory & Questions
    Replies: 3
    Last Post: January 18th, 2010, 04:13 AM

Tags for this Thread