Transcript Editors
PROGRAMMING
REVIEW
Lab 2
EECS 448
Dr Fengjun Li and Meenakshi Mishra
Software Development
• Science or Art?
• Science
• Parts of it have fixed objective which must be met
• Building new technology to help mankind
• Art
• Most clients just give a rough idea what they want
• The final product is result of programmer’s imaginations
• The more you practice, the better you get at it
• There are basic techniques that can be taught; other than that you are
on your own
Task for Today
• Build a java application for a basic calculator
• The user selects the basic operations from drop-down menu
• The options should be addition, subtraction, multiplication, devision
• The user enters the numbers in two text fields provided
• The result appears in third text field
A tip that always helps
• Approach the problem in small steps
• Breaking the problem down in steps helps debugging
• For example in this case
• First write an application just to launch the application window
• Change your program to include the textfields and labels
• Write a code to just do addition
• Plug in the drop-down menu and adjust the program accordingly
Working with swing
• A package written for developing sophisticated GUI
• Adopts style of the operating system or style unique to Java
• Each style is called look and feel
• Java 7 introduces new look and feel called Nimbus, that is unique to
Java
• Must avail the swing package first
• import javax.swing.*;
• Other packages supporting GUI programming
• java.awt (Abstract Windowing Toolkit)
• java.awt.event (class handling user input)
• Swing generally more powerful than awt
• Not implemented by platform specific code, but written entirely in java
• Supplies plenty of exciting additions like scroll panes, trees, tables etc
• When using Swing component, you work with object of that Swing
class
• All Swing components are subclasses of abstract class JComponent
Creating Interface
• Create a class that represents the main GUI
• In most projects, main interface is a frame (JFrame class)
• Frame is a window that appears whenever you open an application
• Frame has a title bar, Maximize, Minimize, Close buttons and other features
• Users of graphical environments expect to move, resize and close the windows
• To create a graphical Java application, make the interface a subclass of
JFrame
• public class MyFirstGui extends Jframe
• Set title by either setTitle(String) or super(String) in the constructor
• Set size by setSize(int, int) where first number denotes width and
second height in pixels
• Make your frame visible by setVisible(true);
• Set default close operation to exit on closing
setDefaultCloseOption(EXIT_ON_CLOSE)
• Other close options
• DISPOSE_ON_CLOSE
• HIDE_ON_CLOSE
• DO_NOTHING_ON CLOSE
Adding Components
• Class JButton
• Three constructors
• JButton(String) Label on the button
• JButton(Icon) Icon on the button
• JButton(String,Icon) Both icon and Label on the button
• JButton b = new JButton(“OK”);
• Class JPanel
• After creating components, need to add them to a container
• Simplest container
• JPanel p = new JPanel();
• p.add(b);
//add button b to panel p
Adding Components
• Class JTextField
• JTextField() Empty Text field
• JTextField(int) Text Field with specified width
• JTextField(String, int) Text Field with specified width and
text
• setEditable(False) for text fields that do not accept user input
• Class JLabel
• Three Constructors
• JLabel(String) Label with specified text
• JLabel(String, int) Label with specified text and alignment
• SwingConstants.LEFT
• SwingConstants.RIGHT
• SwingConstants.CENTER
• JLabel(String, Icon, int) Label with specified text, icon and
alignment
Adding Components
• Class JComboBox
• Creates a list of items that are hidden when menu is not being used
• addItem(Object) Add item to the list
• getItemAt(int) Returns the text of the list item at specified
index position
• getSelectedIndex() Returns the index position of selected
item
• getSelectedItem() Returns the text of currently selected item
• setSelectedIndex(int) Selects the item at indicated index
position
• setSelectedIndex(object) Selects specified object in the list
Responding to User Inputs
• Use java.awt.event package
• Useful listener interfaces
• ActionListener When user performs an action on a component.
• AdjustmentListener When component is adjusted
• FocusListener Textfield or area loses or gains focus
• ItemListener When items in items such as combo box are
changed
• KeyListener When user enters text using keyboard
• MouseListener Generated by mouse clicks, mouse entering or
leaving a component area
• MouseMotionListener track all movement by mouse over
component
• WindowListener When window is minimized, maximized, moved or
closes
• Implement listener in class definition
• public class t extends JFrame implements ActionListener,
ItemListener { //…}
Setting up Components
• Need to add the attribute of listening to specific objects
• addActionListener() JButton, JCheckBox, JComboBox,
JTextField, JRadioButton and JMenuItem
• addFocusListener() all Swing Component
• addItemListener() JButton, JCheckBox, JComboBox and
JRadioButton
• addKeyListener() all Swing Components
• addMouseListener() all Swing Components
• addMouseMotionListener() all Swing Components
• addTextListener() JTextField and JTextArea
• addWindowListener() JWindow and JFrame
• JButton b = new JButton(‘OK”);
• b.addActionListener();
Event Handling Methods
• Action Events
• ActionListener actionPerformed(ActionEvent)
• Focus Events
• FocustListener focusGained(FocusEvent) and
focusLost(FocusEvent)
• Item Events
• ItemListener itemStateChanged(ItemEvent)
• When a class implements an event interface, must define the
corresponding event handling methods
• public void actionPerformed(ActionEvent e) { //..}
• To know which for which object the event was generated
• Object source=e.getSource();
• If the event was triggered by button b, you can check using
if (source==b) { //..}
Summary of designing application
• Import packages
• Create a Class that extends JFrame and implements all
the user interfaces required
• Make sure to set the frame to visible, set title, declare size, the
•
•
•
•
application exits on closing
Create the components
Add property of user input listening to the components
Add the components to a simple container (optional)
Add the container to the Frame
• Define what to do when the user enters required input
• Define event handling methods