GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;

Slides:



Advertisements
Similar presentations
G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)
Advertisements

Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Graphical User Interfaces
TCU CoSc Programming with Java Visual Design (Chapter 5)
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Corresponds with Chapter 12
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 13 GUI Basics.
1 GUI Basics First three slides are repeated from previous lecture notes! You will probably (in fact should) never write Java codes like the program examples.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
1 UFCE3T-15-M Object- oriented Design and Programming Block 3 GUI Programming Jin Sa.
GUI Layout Managers Arkadiusz Edward Komenda. Outline Components and Containers Layout Managers Flow Layout Grid Layout Border Layout Nested Containers.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Lesson 5 GUI Programming AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 11 Getting Started.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 11 Getting Started with GUI Programming 百闻不如一见.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Chapter 8: Getting Started with Graphics Programming Graphics Class Hierarchy Graphics Class Hierarchy Frames Frames The relationship between frame and.
1 Chapter 10 Getting Started with Graphics Programming F Graphics Class Hierarchy F Frames –Creating frames, centering frames, adding components to frames.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Creating GUIs in Java Using.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 12 GUI Basics.
GUI Basics.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
MIT-AITI 2004 – Lecture 16 Introduction to Swing.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
3461 Laying Out Components Interior Design for GUIs.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
MIT AITI 2003 Lecture 16. Swing Part I Let’s swing and have some fun !!!
Basics of GUI Programming Chapter 11 and Chapter 22.
1 Chapter 2 Getting Started with GUI Programming.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 12 GUI Basics.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 7 A First Look at GUI Applications Layout Managers.
Chapter 8 Getting Started with Graphics Programming Graphics Class Hierarchy Graphics Class Hierarchy Frames Frames  Creating & centering frames, adding.
Object-Orientated Analysis, Design and Programming
Reference: COS240 Syllabus
Modern Programming Language Java
Chapter 14 GUI Basics.
Java Applets.
Tim McKenna Layout Mangers in Java Tim McKenna
Java Applets.
Chapter 10 Getting Started with Graphics Programming
Chapter 12 GUI Basics.
Chapter 12 GUI Basics.
Graphical User Interface
Presentation transcript:

GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;

GUI API class hierarch – p371 Object Dimension,Font, FontMetrics, Color, Graphics, Component LayoutManager, Container Panel, Window, Jcomponent Applet, Frame, Dialog

GUI – Container classes Container classes are GUI components that are used as containers to contain other GUI components Container, JFrame, JDialog, JApplet, and JPanel

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

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

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) ;  }

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;

Adding components to a frame Container container = frm.getContentPane(); JButton btn_ok = new JButton(“OK”); container.add(btn_ok);

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.

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

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.

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); }

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

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

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);

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));

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);

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);