Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Code Design Question and Example
    primarykey
    data
    text
    <p>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.</p> <p>So, I'm practicing :)</p> <p>I am setting out to write a Calculator (with a Swing GUI), and I want to create a really good design for it.</p> <p>So far, I have created a package with the following:</p> <pre><code> com.kevin.Calculator &gt; com &gt; kevin &gt; calculator &gt; ui Swing_Calculator.java &gt;utilities </code></pre> <p>I want to separate out the UI from the functionality (the math).</p> <p>So far so good?</p> <p>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:</p> <pre><code> /** * */ 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(); } }); } } </code></pre> <p>Can anyone offer some insight? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload