Hey everyone, I've been writing code in Java for awhile now, however I feel as though I lack in code "design" - that is, creating packages and separating classes and such. I'm just not sure when to create what.
So, I'm practicing :)
I am setting out to write a Calculator (with a Swing GUI), and I want to create a really good design for it.
So far, I have created a package with the following:
com.kevin.Calculator
> com
> kevin
> calculator
> ui
Swing_Calculator.java
>utilities
I want to separate out the UI from the functionality (the math).
So far so good?
My Swing_Calculator.java is, right now, creating the GUI elements, as well as running the user interface. However, should I be "breaking down" the creation of the GUI elements into more classes? As I said earlier - I don't seem to know when it is best to do modularize. Here is my code so far:
/**
*
*/
package com.kevin.calculator.ui;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Panel;
import javax.swing.*;
/**
* @author Kevin
*
*/
public class Swing_Calculator {
/**
* Creates the calculator's GUI and shows it on screen.
*/
private static void create_GUI() {
// Create and set up the window.
JFrame frame = new JFrame("KBehr's Kalculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Sets the size of the frame
frame.setSize(300, 350);
// Centers the frame on the screen
// Should be called AFTER the frame size has been set
frame.setLocationRelativeTo(null);
// Creates a new Panel Object
JPanel panel = new JPanel();
// Sets the size of the panel
panel.setPreferredSize(new Dimension(300, 350));
// Creates a GridLayout object
// http://java.sun.com/docs/books/tutorial/uiswing/layout/grid.html
GridLayout button_grid = new GridLayout(0,3);
// Adds the GridLayout to the Panel
panel.setLayout(button_grid);
// Adds the Panel to the Frame
frame.getContentPane().add(panel);
// Displays the window.
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
create_GUI();
}
});
}
}
Can anyone offer some insight? Thanks!