When you sit down to write the first time a less serious project this question arises. How to correctly separate the logic of the program and the graphical interface? Explain, please, on the fingers. Suppose there is a main class Main that I want to inherit from JFrame, but I don’t want to heavily pollute the constructor. Therefore, I create the second class CreateGUI, so that later it’s just nice to add methods to the Main constructor. This is normal?. What is the best way to work with these two classes? Thank you in advance.
2 answers
Well, if you do everything at all with beauty =) you can get acquainted with MVC =) @ nk32 , I would not generalize the whole GUI and extends JFrame
, usually JFrame
can only contain the main window view.
|
Usually the entire GUI is simply rendered into a separate class like this. If there is, for example, a large menu, you can make a separate class for the menu. Work not with methods, but with objects.
public class Main { public static void main(String[] args) throws Exception { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new GUI(); } }); } } public class GUI extends JFrame { public GUI() { // ..... } }
|