Java Notes

Basic GUI

Next - Big Blob

Sample window

This first example just shows what the user interface will look like. The following examples use this same user interface. This first example has no listeners, so it does nothing.

Main program

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
// structure/CalcV1.java
// Fred Swartz - December 2004

// GUI Organization: All work is in the GUI constructor.
//
//    (1) Main creates a GUI frame.
//    (1) Shows the GUI.  

import javax.swing.*;

public class CalcV1 {
    public static void main(String[] args) {
        JFrame window = new CalcV1GUI();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("Simple Calc");
        window.setVisible(true);
    }
}

GUI

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
// structure/CalcV1GUI.java - Outline for following examples.
// GUI Organization - Pure presentation - no listeners, model, etc.
// Fred Swartz - December 2004

import java.awt.*;
import javax.swing.*;

class CalcV1GUI extends JFrame {
    //... Components
    private JTextField m_totalTf     = new JTextField(10);
    private JTextField m_userInputTf = new JTextField(10);
    private JButton    m_multiplyBtn = new JButton("Multiply");
    private JButton    m_clearBtn    = new JButton("Clear");
    
    /** Constructor */
    CalcV1GUI() {
        //... Initialize components
        m_totalTf.setEditable(false);
        
        //... Layout components.
        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());
        content.add(new JLabel("Input"));
        content.add(m_userInputTf);
        content.add(m_multiplyBtn);
        content.add(new JLabel("Total"));
        content.add(m_totalTf);
        content.add(m_clearBtn);
        
        //... finalize
        this.setContentPane(content);
        this.pack();
    }
}