Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9001953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:30:19+00:00 2026-06-16T00:30:19+00:00

Im new to java and im trying to build a simple GUI with several

  • 0

Im new to java and im trying to build a simple GUI with several buttons and drop down menus. Ive managed to get a working GUI but the real question is about the proper use of the actionListener method. In order to assign an action to a button i inserted each actionListener in its own class, this is the code:

public class GUI implements something
{
    public static ClientGUI App;
    private JFrame chatWin;
    private JMenuBar menubar;
    private JMenu x;
    private JMenu y;
    private JMenuItem exit;
    private JMenuItem about;


    public GUI()
    {

               /* 
                * some code and parameters
                */

            //creating the menu bar
            JMenuBar menubar = new JMenuBar();
            chatWin.setJMenuBar(menubar);

            JMenu x= new JMenu("menu1");
            menubar.add(x);
            JMenuItem exit = new JMenuItem("menu2");
            x.add(exit);

            JMenu y= new JMenu("Help");
            menubar.add(help);
            JMenuItem about = new JMenuItem("inner menu1");
            y.add(about);

            //action listener for the exit button 
            class exitaction implements ActionListener
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.exit(0);
                }
            }
            exit.addActionListener(new exitaction());

            //action listener for the about button
            class aboutaction implements ActionListener
            {
                public void actionPerformed(ActionEvent e)
                {
                    //some code
                }

            }
        }

    public static void main (String args[])
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                App = new GUI();
            }
        });
    }


}

Is that code is considered to be neat and correct? is there anything i could improve or need to change?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:30:20+00:00Added an answer on June 16, 2026 at 12:30 am

    I follow the rule:

    If the action implementation is great, I put in a inner class.

        public MyFrame extends JFrame {
    
           // componentes creating, etc
    
            private static class MyShortAction implements ActionListener {
    
                @Override
                public void actionPerformed(ActionEvent e){
                    // some big implementation
                }
    
            }
    
        }
    

    When short, i put in a anonymous class.

    myButton.addActionListener(new ActionListener() {
    
        @Override
        public void actionPerformed(ActionEvent e){
            // some small code
        }
    
    });
    

    If it is used by other components, I extract to a top level class.

    public class MyAction implements ActionListener {
    
        @Override
        public void actionPerformed(ActionEvent e){
            // some small code
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a simple HTTP Server using Java, using java.net.ServerSocket = new
I am trying to build a simple Java GUI (I've been learning for only
I am new to Java and trying to do a simple program to help
I'm trying out log4j in a simple test app. I create a new Java
I'm working with XSL templates in Java, and I'm trying to build a custom
I'm trying to build a simple web server in Java as practice but I'm
I'm trying to build a simple AWT application in Java. I want all of
I am trying to build a simple app using google app engine, with java
I'm new to Java. I'm simply trying to build a .jar file of my
I'm trying to build a simple paint tool. The mouseDrag events creates a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.