I made a Swing application that is pretty simple in functionality. However, in my opinion, the code that it consists of has become quite large and very dirty. All swing components and actions are in one file. So, for example, if I made an even larger application with more functionality, the code would be pretty hard to get through.
So my question is how to create a good code structure. Or if there is a good web page that I can read about it, and if possible, code examples. I checked out Sun's tutorial on Swing, but these are pretty simplistic examples that they showed.
UPDATE: I reflect on time and check out some examples. I do not know if the MVC template was received correctly. Anyway, my idea is to split each JFrame into its own class file. After that, I have one MainFrame, which is the main window for the application. From this JFrame, I create one instance of each JFrame that I have. And call these frames from MainFrame with Actions. I do not know if this is a good idea. However, this makes the code much easier to read anyway.
Here is an example of how I meant
class Main implements ActionListener { private JFrame frame = new JFrame(); private JButton button1 = new JButton(); private JPanel panel = new JPanel(); private FirstFrame frame1 = new FirstFrame(); private SecondFrame frame2 = new SecondFrame(); private ThirdFrame frame3 = new ThirdFrame(); public Main() { button1.addActionListener(this); } public createGUI() { frame.setTitle("Main"); frame.setSize(400,300); panel.add(button); frame.setVisible(true); frame.setLocationRelativeTo(null); } public static void main(String args[]) { new Main().createGUI(); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == button1) { frame1.enable(); } } }
java swing
starcorn
source share