Gui Notes
Gui Notes
component---event--->Listener
(close button)---windowEvent--->WindowListener
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent; // This is the file Ending listener.java
1. JFrame = first window ,we can set the width and height
2. JButton
import javax.swing.JFrame;
import javax.swing.JButton;
**Text Field
e.g.
JTextField text1 = new JTextField("Enter a text");
JTextField text2 = new JTextField("Enter text 2");
**Menu
e.g.
JMeno menu = new JMenu("Menu");
**Menu Bar
e.g.
JMenuBar bar = new JMenuBar();
bar.add(menu);
**Components of GUI
**Components VS Containers
// components-- serve as ways for the user to interact with the GUI program (JButton, JLabel)
// containers-- hold components (e.g. JFrame, JPanel)
1. Containers : can have other containers : each container can have its own Layout manager.
2. components
3. layout manager to position the components inside the container
**Layout Managers
// add method adds components to a container but does not specify how
// e.g. to add a layout manager to JFrame name window;
window.setLayout(new BorderLayout());
// BorderLayout manager places the components that are added to the JFrame object into five
regions N,E,W,S and the CENTER.
**FlowLayout
// FlowLayout manager arranges components one after the other from left to right.
// if it runs out of space is then moves to the next line.
**GridLayout
**JPanels
// Panels simply divide the JFrame and groups smaller objects into larger objects(e.g. buttons
into a panel, then panels into a JFrame)
**Action Commands
// setActionCommand()
// setBackground()
**Containers
JFrame
JPanel
**Components
JButton
JLabel
JTextField
JTextField
JMenu
JMenuItem
JMenuBar
**Layout managers
BorderLayout
FlowLayout
GridLayout
There are constants in the Color class that represent a number of basic colors
The color of a GUI component can be set with setBackground:
button.setBackground(Color.PINK);
**Colors
RGB - RED/GREEN/BLUE
e.g.
float num1 = 2.5f;
double num2 = 2.5;
float num3 = 2.5; // Won't compile, java thinks decimals are doubles
float num3 = (float)2.5;
**JColorChooser
// The class (JColorChooser) from javax.swing can be used to allow a user to choose a color.
**Fonts
**Monospaced
**Window Listeners
Opening
Closing
Minimising
Maximising
Deactivating
Activating
// The WindowListener interface and the WindowEvent class are in the package java.awt.event
// setDefaultCloseOperation
**Icons
// Icons and text may be added to Jbutton and JMenuItem in the same way as they are added
to a JLabel.
// Insets : object of the class insets are used to specify the size of the margin in a button or
menu item.
// arguments given are in Pixels
**Scroll Bars
// When a text area is created, the number of lines that are visible and the number of characters
per line are specified as follows:
In order to combat this we can use scroll bars with the text area.
panel.add(scrolled);
scrolled.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS)
;
scrolled.setHorizontalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AWLAYS);
// A better way to define a swing window is to make it a derived class of the JFrame, this is a
normal way to deine a windowing interface.
e.g.
public class LoginWindow extends JFrame
public class ExitWindow extends JFrame
public class HomePage extends JFrame
Every container and component that can be drawn on the screen has an associated
Graphics object.
The object has date specifying the area of the screen covered by the component.
E.g. the Graphics object for a JFrame specifies drawing takes place inside the window
borders.
// Java has a drawing coordinate system where the origin point (0,0) is at the upper left corner
of the drawing area.
// Units and sizes are in Pixels
// When drawing the imaginary upper left corner that a shape is in is specified
// Swing components have an already defined method called paint.
g.drawOval(...);
g.drawRect(...);
g.drawLine(...);