M
M
Mimocodil2021-12-29 20:53:29
Java
Mimocodil, 2021-12-29 20:53:29

How to organize the code correctly?

I am making a menu for the program and there are many similar places:

// ...
menuFile = new JMenu("Файл");
menuBar.add(menuFile);

itemCreate = new JMenuItem("Создать");
itemCreate.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
    // код
  }
});
menuFile.add(itemCreate);
// ...


In total there are 5 menu items and something like 40 buttons. I found lambdas on the Internet, which slightly simplified the creation of each menu item by moving the code into a separate method:

// ...
itemCreate = new JMenuItem("Создать");
itemCreate.addActionListener(e -> onCreate());
menuFile.add(itemCreate);
// ...


However, the main class still seems too long and overloaded to me. How is this problem usually solved? Is it okay to move the button methods to a separate class and make them static? Or is it better to create a separate package with a class for each button, overriding the main JMenuItem class?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question