How do I organize my java GUI?
I'm creating a game in Java for fun, and I'm trying to decide how to organize my classes for the GUI So far, all classes with only swing components and layouts (no logic) are in a package called "UI" I now need to add the listener (i.e. actionlistener) to the component (i.e. button) Listeners need to communicate with the game class
At present, I have: game Java – add a panel to its creation framework
import javax.swing.*; import ui.*; public class Game { private JFrame frame; Main main; Rules rules; Game() { rules = new Rules(); frame = new JFrame(); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); main = new Main(); frame.setContentPane(main.getContentPane()); show(); } void show() { frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { new Game(); } }
Rules. Java - game logic
UI package – all classes create a new panel that is exchanged with the main frame content pane main Java (main menu) – create a panel containing components
Where do I put the function of the main class now? In game class? Class alone? Or is the whole organization wrong?
thank you
Solution
First of all: you did a good job Trying to keep your code in order will help you program But remember this: developing good code is more than just organizing and classifying source code For example... Are you using any type of UML model? Are you applying any design patterns? Are your classes really highly cohesive? How about coupling?
All this will guide you through the process of writing good code, which seems to be what you want The result of all this will make your code organized and easy to maintain