Designing a JAVA GUI

Basic User Interface Tasks: Example Swing objects for input, output, control, guidance: Choosing the top level Window

More Swing Objects:


Creating a frame

            setTitle("My Application");  // sets the title in the blue banner bar
            setSize(800,600); // sets the size of the JFrame
            setLocationRelativeTo(null); //centers application on the screen, must go after setSize

import java.net.*;

public class Test extends javax.swing.JFrame {
   
    /** Creates new form Test */
    public Test() {
        initComponents();
        setTitle("My Application");
        setSize(800,600);
         setLocationRelativeTo(null); // this statement must go after the set size, centers application on the screen
      
         URL iconurl = getClass().getResource("logo.gif"); // capitalization counts on the filename
        ImageIcon ic = new ImageIcon(iconurl); // get file as an imageicon

       setIconImage(ic.getImage());    // tell the frame to set is as its icon
    }
  

Choosing a Layout Manager

 LayoutManagers provide the intelligence to arrange and size components in a container. To change the layout manager used by your JFrame, Open the JFrame item on the Componet Inspector Window (to the right of the display area), then right click on the layout and select "set layout".

There are several different predefined LayoutManagers available for you to use:

Default Layout Managers (Netbeans forces a FreeDesign if you use the GUI designer) Hints on using the NetBeans Form Editor


JFrame

initComponents();
setTitle("My Application");  // set application title
setSize(800,600);
    // set JFrame size, width, height in pixels

JPanel




Containment Hierarchies

Containment Hierarch
figure 9-14

Metric converter
 

Question for thought : How could this application be rewritten to have the same interface, but make the Keypad reusable?

Answer - Create a separate class named Keypad, it would inherit JPanel, reside in its own file (Keypad.java), and handle setting up the buttons on the grid and handling the button events.