A Guide to Programming in Java - Peel My Class Sites

eyelashesnectarineSoftware and s/w Development

Nov 3, 2013 (3 years and 11 months ago)

65 views

Slide
1

GUI


Graphical User Interface (pronounced "gooey")


Typically used with applications written for Windows
or
OS X


Event
-
driven, responding to events such as a mouse
click

Slide
2


Creating a Swing GUI


A GUI must begin with a top
-
level container, such as
a frame, for holding a content frame


A content frame, such as a panel, holds and displays
components and gets added to a container


Components include labels

Slide
3


A Swing GUI

frame

panel

label

Slide
4


The
JFrame

Class


Part of the java.swing package


The JFrame class is used to create a frame for a GUI


JFrame methods include:


setDefaultLookAndFeelDecorated()


setDefaultCloseOperation()


getContentPane()


setContentPane()


pack()


setVisible()

Slide
5


The
JPanel

Class


Part of the java.swing package


The JPanel class is used to create a content frame
for a GUI


JPanel methods include:


add()


remove()

Slide
6


The
JLabel

Class


Part of the java.swing package


The JLabel class is used to create an object for
displaying text


JLabel constructors and methods include:


JLabel()


setText()

Slide
7


The
JButton

Class


Part of the java.swing package


The JButton class is used to create an object that
can be clicked by the user. Provides a means of
communication between the user and the interface.


Requires a listener object to determine when the
user has clicked the button


JButton constructors and methods include:


JLabel()


setActionCommand()


getActionCommand()


addActionListener()

Slide
8


Handling Events


A listener object responds to an event by executing
an event handler named actionPerformed()


The actionPerformed() method has a parameter for
the action command string, which is passed by the
event


The component requiring a listener must set the
action command and add an action listener


The class containing a listener must implement the
ActionListener interface

Slide
9


Component Layout


FlowLayout manager places components one next to
the other in a row


BoxLayout manager places components one after the
other in a column


GridLayout manager places components into a grid
of rows and columns


Borders and alignment can be changed to control
layout within a layout manager

Slide
10


The
JTextField

Class


Part of the java.swing package


The JTextField class is used to create an object
where users can enter data


JTextField constructors and methods include:


JTextField()


getText()


addActionListener()

Slide
11


Processing Numeric Data


Data typed into a text field is a string


Class methods in the Double and Integer classes can
be used to convert string data to numeric data and
the reverse


Double class methods include:


parseDouble()


toString()


Integer class methods include:


parseInteger()


toString()

Slide
12


The
JComboBox

Class


Part of the java.swing package


The JComboBox class is used to create an object
where users select a choice from a list of choices


JComboBox constructors and methods include:


JComboBox()


setSelectedIndex()


getSelectedItem()


setEditable()


addActionListener()

Slide
13


Adding Color


Swing Components have methods for changing their
colors:


setBackground()


setForeground()


The methods for changing color require a parameter
from the Color class


How the color changes varies between components

Slide
14


Adding Images to Applications


Many Swing components support images


GIF and JPG image formats work best


The JLabel and JButton classes include the setIcon()
method for adding an image to a component


The JLabel and JButton constructors may also specify an
image

Slide
15


Nested Classes


A class within a class is called a nested class


Nested classes are members of the class they are
within


The class containing the nested class is called the
outer class

Slide
16


A Nested Class

public class MyApp {


outer class data members




outer class constructors and methods




class MyListener implements ActionListener {



public void actionPerformed(ActionEvent event) {








}


}

}

outer class

nested class