Download presentation
Presentation is loading. Please wait.
1
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
2
GUI API class hierarch – p371 Object Dimension,Font, FontMetrics, Color, Graphics, Component LayoutManager, Container Panel, Window, Jcomponent Applet, Frame, Dialog
3
GUI – Container classes Container classes are GUI components that are used as containers to contain other GUI components Container, JFrame, JDialog, JApplet, and JPanel
4
Java GUI API – container classes Container: used to group components. A layout manager is used to position and place components in a container Frames, panels and applets are examples of containers JFrame: window not contained inside another window. It is the container that holds other Swing user-interface components JDialog: popup window or message box JApplet: subclass of Applet. You must extend JApplet to create a Swing-based Java applet. JPanel: invisible container that holds user-interface components. Panel can be nested. JPanle also can be used as a canvas to draw graphics
5
GUI helper classes Helper classes are not subclass of Component. They are used to describe the properties of GUI components Graphics: abstract class that provides a graphical context for drawing strings, lines, and simple shapes Color: deals with colors of GUI components Font: specifies fonts for the text and drawings on GUI components FontMetrics is an abstract class used to get the properties of the font Dimension: encapsulates the width and height of a component LayoutManager: specify how components are arranged in a container
6
Frame Create a Frame import javax.swing.*; public class MyFrame{ public static void main( String[] args){ JFrame frm = new JFrame(“Test Frame”); frm.setSize(400, 300); frm.setVisible(true); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; }
7
Centering a Frame To display the frame at specific location -up left corner at (x,y) frm.setLocation(x,y); Centering a frame Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int width = screen.width; int height = screen.height; Dimension frmsize = frm.getSize(); int x = (width – frmsize.width)/2; int y = (height – frmsize.height)/2;
8
Adding components to a frame Container container = frm.getContentPane(); JButton btn_ok = new JButton(“OK”); container.add(btn_ok);
9
Layout Managers FlowLayout is the simplest layout manager. The components are arranged in the container from left to right in the order in which they were added public FlowLayout(int align, int hgap, int vgap) – construct a new FlowLayout with the specified alignment, horizontal gap and vertical gap. public FlowLayout( int alignment) – with Hgap and vGap 5 pixels public FlowLayout() – center alignment and 5 pixels gaps.
10
GridLayout The GridLayout manager arranges components in a grid with number of rows and columns defined by the constructor. public GridLayout(int rows, int columns, int hgap, int vgap) public GridLayout(int rows, int columns)- 0 gaps public GridLayout() – one column in a single row
11
The number of row or column can be zero but not both; If 0 rows and 3 columns for a grid with 10 components, GridLayout creates 3 columnds and 4 rows If both the number of rows and columns are nonzero, the number of rows is the dominating parameter; that is, the number of rows is fixed, and the layout manager dynamically calculate the number of columns.
12
example Container container = getContentPane(); container.setLayout(new FlowLayout(FlowLayout.LEFT,10,20)); container.setLayout(new GridLayout(4,3,10,20)); for( int I = 0; I < 10; I++) { container.add(new Jbutton(“Component”+I); }
13
BorderLayout The BorderLayout manager divides the window into five areas: East, South, West, North and Center public BorderLayout(int hgap, int vgap) public BorderLayout – construct a new BorderLayout without horizontal or vertical gaps North and south can stretch horizontally West and east can stretch vertocally Center can stretch both horizontally and vertically to fill any empty space Components are added to a BorderLayout by using add(component, index), where index is a constant BorderLayout.East, South, North, and West
14
panel You can divide window into panels Panels can act like smaller container to group user-interface components You can add button in a panel and add the panel into the frame. You also can use layout manger to set layout of components I the panel
15
panel Create a panel and add buttons to the panel; Panel p1 = new Panel(); p1.setLayout(new GridLayout(3,3)); p1.add(new Button(“OK”); Add the panel to the container container.add(p1);
16
Color class – java.awt.Color Colors are made of red, green and blue, each of which is represented by a byte value 0(darkest)-255(lightest), known as RGB model Constructor: public Color( int r, int g, int b) Color color = new Color(128, 100,100); JButton jbtOK = new JButton(“OK”); jbtOK.setBackground(color); Jbt.setForeground(new Color(100,1,1));
17
Alternative You can use one of 13 standard colors black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yellow They are defined as constants in java.awt.Color jbtOK.setForeground(Color.red);
18
Font class – java.awt.Font Constructor: public Font(String name, int style, int size); You can choose a font name from SansSerif, Serif, Monospaced, Dialog, or DialogInput You can choose a style from Font.PLAIN, Font.BOLD, Font.ITALIC and Font.BOLD + Font.ITALIC Specify a font size of any positive integer. Font font1 = new Font(“SansSerif”, Font.BOLD, 16); Font font2 = new Font(“Serif”, Font.BOLD+Font.ITALIC, 12); JButton jbtOK = new JButton(“OK”); jbtOK.setFont(font1);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.