user442471
user442471

Reputation: 713

putting code in the main method

I have been coding a java project in netbeans and I finally have everything working. Thanks in part to the support of this forum. However, I have been coding depending on what i needed my button or textarea to do. I have nothing under main method, just under the objects that are performing actions. I have found out this is not the standard. So, I need guidance on how this should work under the main method.

Upvotes: 0

Views: 481

Answers (2)

Chris Nava
Chris Nava

Reputation: 6802

Event driven applications don't tend to do much in their main() method. This is by design as the main thing the program is doing is waiting for user interaction or other events.

Indeed, if you want to reuse your code objects you should keep all the logic not directly concerned with starting from the command line in other methods.

Upvotes: 0

Qwerky
Qwerky

Reputation: 18445

Button and textarea? Sounds like you've written a swing app. Swing apps tend to be event driven, ie the app does things depending on what buttons get pressed, which again sounds like what you've written.

Swing apps tend to get launched by the main method like this;

/**
 * Main method
 * @param args
 */
public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            createAndShowGUI();
        }
    });
}

private static void createAndShowGUI()
{
    //create your top level container and its components and set it visible
}

That's pretty much what your main method should look like.

Upvotes: 3

Related Questions