JAVA GUI (lecture 10)
GUI Packages java.awt javax.swing
Abstract Window Toolkit
The java.awt package was the primary repository for classes that are used to create a GUI in java but many of the classes it defines have been superseded in Java 2 by javax javax.swing swing
However the Swing classes are generally derived from, and depend on, fundamental classes within java.awt, so these cant be i ignored. d
GUI Class Hierarchyy ((Swing) g) Dimension Font
Classes in the java.awt package
LayoutManager 1
H Heavyweight i ht
FontMetrics Object
Color
Panel
Applet
JApplet
Window
Frame
JFrame
Dialog
JDialog
Graphics Component
Container
*
Swing Components in the javax.swing package
JComponent
Lightweight 4
Container Classes Dimension
Classes in the java.awt package
LayoutManager 1
Font
Heavyweight
FontMetrics Object
Color
Panel
Applet
JApplet
Window
Frame
JFrame
Dialog
JDialog
Graphics Component p
Container
*
Container classes can contain other GUI components.
JComponent
JPanel
Swing Components in the javax.swing package
Lightweight
5
Swing GUI Components JCheckBoxMenuItem
Ab AbstractButton
JComponent
JMenuItem
JMenu
JButton
JRadioButtonMenuItem
JToggleButton
JCheckBox JRadioButton
JEditorPane JTextComponent
JTextField
JPasswordField
JTextArea
JLabel
JTabbedPane JToolBar JTree
JComboBox
JList
JSplitPane JMenuBar
JTable
JPanel
JLayeredPane JJPopupMenu p p
JTableHeader
JOptionPane JSeparator JJFileChooser C
JInternalFrame
JScrollBar
JSlider
JScrollPane
JRootPane
JColorChooser JProgressBar
JToolTip p
JSpinner 6
Components ¾ A component represents a graphical entity of one kind or another that can be displayed p y on screen ¾ All co container a e a and d visual sua co components po e s inherit e from java.awt.Component ¾ Key ey Classes: C asses Window, do , JJFrame, a e, JJDialog, a og, JApplet etc
JPasswordField JButton JCheckBox
JSlider
JList JFrame
JMenu
JSpinner JApplet JDialog
Creating GUI Objects // Create a button with text OK Jbutton jbtOK = new JButton("OK"); // Create a label with text "Enter your name: " Jlabel jlblName = new JLabel("Enter your name: ");
Label
Text field
Check Box
Radio Butto n
Button
// Create a text field with text "Type Name Here" JTextField jtfName = new JTextField("Type Name Here");
Combo Box
// Create a check box with text bold JCheckBox jchkBold = new JCheckBox("Bold"); // Create a radio button with text red JRadioButton jrbRed = new JRadioButton("Red"); // Create a combo box with choices red, green, and blue JComboBox jcboColor = new JComboBox(new String[]{"Red", "Green", "Blue"}); 9
Frames
Frame is a window that is not contained inside another window Frame is the basis to contain other user interface window. components in Java GUI applications.
The JFrame class can be used to create windows windows.
For Swing GUI programs, use JFrame class to create widows. widows
10
Framing g a Window Example p import javax.swing.JFrame; public class TryWindow { // The window object static JFrame aWindow = new JFrame("This is the Window Title"); public static void main(String[] args) { int windowWidth = 400; // Window width in pixels int windowHeight g = 150;; // Window height g in pixels p aWindow.setBounds(50, 100, windowWidth, windowHeight);
// Set position // and size
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ( aWindow.setVisible(true); }}
// Display the window
Creating g Frames import javax.swing.*; public bli class l MyFrame { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation( p ( JFrame.EXIT_ON_CLOSE); } }
13
Adding Components into a Frame Title bar
// Add a button into the frame frame.getContentPane().add( new JButton("OK")); JButton( OK ));
Content pane
14
Content Pane Delegation in JDK 1.5 Title bar
Content pane
// Add a button into the frame frame.getContentPane().add( new JButton("OK")); JButton( OK ));
// Add a button into the frame frame.add( new JButton("OK"));
15
JFrame Class javax.swing.JFrame +JFrame()
Creates a default frame with no title.
+JFrame(title: String)
Creates a frame with the specified title. title
+setSize(width: int, height: int): void
Specifies the size of the frame.
+setLocation(x: int, y: int): void
Specifies the upper-left corner location of the frame.
+setVisible(visible: boolean): void
Sets true to display the frame.
+setDefaultCloseOperation(mode: int): void
Specifies the operation when the frame is closed.
+setLocationRelativeTo(c: Component): void
Sets the location of the frame relative to the specified component. If the component is null, the frame is centered on the screen.
+pack(): void
Automatically sets the frame size to hold the components in the frame.
16
Components ¾ There are several methods that are available for components ¾ setForeground( ) and setBackground( ) ¾ setFont(( ) ¾ setEnabled( ) ¾ setSize(( ) and setBounds(( ) ¾ setVisible( )
Containers
The Container class is abstract and most commonly used concrete subclasses are JApplet, JApplet JFrame JFrame, JDialog and Panel All the classes derived from the Container can contain other objects of the classes derived from Component
Swing provides containers such as
top t
level: l l frames, f dialogs di l
intermediate level: panel, scroll pane, tabbed pane, ...
other Swing components: buttons, buttons labels, labels ...
Containers
Descendents of the java.awt.Container class Use a layout manager to position and size the components contained in them. C Components are added to a container using one of the various forms of its add method
Depending
container
on which layout manager is used by the
panel.add(component);
Top Level Containers
Every program that presents a Swing GUI contains at least one top-level container. A Top level container provides the support that Swing components need to perform their painting and event-handling. Swing provides three top-level top level containers:
JFrame
(Main window)
JDialog (Secondary window)
JApplet (An applet display area within a browser window)
Layout y Managers g
Java’s layout managers provide a level of abstraction b t ti tto automatically t ti ll map your user interface on all window systems.
The UI components are placed in containers. Each container has a layout y manager g to arrange g the UI components within the container.
Layout managers are set in containers using the setLayout(LayoutManager) method in a container. 21
Layout managers ¾ Java GUI reside in applets or in frames ¾ There are several layout manager classes in the AWT and swing ¾ java.awt.LayoutManager is an interface not a class
Layout Management (cont)
The Java platform supplies five commonly used layout managers:
FlowLayout
BorderLayout
BoxLayout
GridLayout
GridBagLayout
Flow Layout Manager ¾ It is the default layout manager for panels and applets ¾ It always arranges the components in horizontal rows while honoring each components preferred size ¾ The components always appear left to right in the order in which they were added to their container ¾ Within every row the components are evenly spaced and the cluster of components is centered ¾ To change this default behavior , you can use setLayout( ) − setLayout needs a parameter of type object of Layout Manager − setLayout(new FlowLayout(FlowLayout.RIGHT)) ¾ By default it leaves a gap of 5 pixels between components in both horizontal and vertical directions
FlowLayout Example import javax.swing.Jframe; import javax.swing.Jbutton; import java.awt.Dimension; java awt Dimension; import java.awt.FlowLayout;
import java.awt.Toolkit; import java.awt.Container; java awt Container;
public class TryFlowLayout { // The window object static JFrame aWindow = new JFrame("This is a Flow Layout"); public static void main(String[] args) { int windowWidth = 400; int windowHeight = 150;
// Window width in pixels // Window height in pixels
aWindow.setBounds(100, aWindow setBounds(100 100, 100 windowWidth, windowHeight);
// Set position // and size
aWindow setDefaultCloseOperation(JFrame EXIT ON CLOSE); aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout();
// Create a layout manager
Container content = aWindow.getContentPane(); // Get the content pane content.setLayout(flow);
// Set the container layout mgr
// Now add six button components for(int i = 1; i