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.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
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.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
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.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
1 UFCE3T-15-M Object- oriented Design and Programming Block 3 GUI Programming Jin Sa.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Advanced Java and Android Programming Day 1: Introduction to GUI Programming Intro to GUI Programming1.
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.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
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, 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.
1 Why layout managers Can we perform layout without them? –Yes. A container’s layout property can be set to null. Absolute positioning: specify size and.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Creating GUIs in Java Using.
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.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
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,
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
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.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Basics of GUI Programming Chapter 11 and Chapter 22.
1 Chapter 2 Getting Started with GUI Programming.
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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Chapter 7 A First Look at GUI Applications Layout Managers.
Object-Orientated Analysis, Design and Programming
Reference: COS240 Syllabus
Graphical User Interfaces
Modern Programming Language Java
Tim McKenna Layout Mangers in Java Tim McKenna
Chapter 13: Advanced GUIs and Graphics
Chapter 12 GUI Basics.
Chapter 12 GUI Basics.
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

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 GUI components: ButtonLabelText FieldCheck Box Radio Buttons Combo Box

10/24/20153 What is AWT? The Abstract Window Toolkit was a part of Java from the beginning import java.awt.*; All AWT components must be mapped to platform specific components using peers –The look and feel of these components is tied to the native components of the window manager AWT components are considered to be very error prone and should not be used in modern Java applications

10/24/20154 What is AWT? The same application using only AWT components, running on X-Windows:

10/24/20155 What is Swing? With the release of Java 2, the AWT user interface components were replaced with Swing Swing is built on top of AWT to give a more flexible, robust library –Lightweight components don’t rely on the native GUI –Heavyweight components do depend on the target platform because they extend AWT components Swing components are directly painted onto the canvas using Java code

10/24/20156 Java GUI API Object Dimension Font FontMetrics Color Graphics ComponentContainer LayoutManager Panel Applet WindowFrame Dialog JComponent JDialog JFrame JApplet AWT: java.awt Swing: javax.swing Heavyweight Lightweight

10/24/20157 Java GUI API JComponent AbstractButton JLabelJListJComboBoxJPanelJOptionPaneJScrollBarJSlider JTabbedPaneJSplitPaneJLayeredPaneJSeparatorJScrollPaneJRootPane JToolBarJMenuBarJPopupMenuJFileChooserJColorChooserJToolTip JTreeJTableJTableHeaderJInternalFrameJProgressBarJSpinner JTextComponent JEditorPane JTextField JTextArea JMenuItem JButton JToggleButton JCheckBoxMenuItem JMenu JRadioButtonMenuItem JCheckBox JRadioButton JPasswordField

10/24/20158 Container Classes Container classes are GUI components that are used as containers to contain other GUI components –For Swing use: Component, Container, JFrame, JDialog, JApplet, Jpanel –JFrame is a window not contained inside another window –JDialog is a temporary popup window or message box –JApplet is an applet that can run in a web browser –JPanel is an invisible, nest-able container used to hold UI components or canvases to draw graphics A layout manager is used to position and place components in a container

10/24/20159 Frames You need a frame to hold the UI components Title bar Content pane 400 pixels wide 300 pixels high (100, 100)

10/24/ Content Pane The content pane is the part of the frame where the UI components will be placed It is a java.awt.Container object Glass pane Layered pane Menu bar Content pane

10/24/ Adding Components to a Frame UI components can be add ’ed to the content pane after they are created Here, the OK button is centered in the frame and occupies the whole frame, no matter how it is resized

10/24/ Layout Managers There are three basic layout managers which control how UI components are organized on the frame –FlowLayout –GridLayout –BorderLayout Once created, the layout can be set in the content pane using setLayout As the window is resized, the UI components reorganize themselves based on the rules of the layout

10/24/ FlowLayout With flow layout, the components arrange themselves from left to right in the order they were added Rows/buttons are left aligned using FlowLayout.LEFT Vertical gap of 20 pixels Horizontal gap of 10 pixels

10/24/ Extending JFrame public class GUIMain extends JFrame { // construct GUI interface with components public GUIMain() { // set the layout manager Container container = getContentPane(); container.setLayout(…); // create UI components and add container.add(…) } // GUIMain // create instance of GUIMain and set // frame behaviors public static void main(String args[]) { GUIMain frame = new GUIMain(); frame.setTitle(…); … } // main } // GUIMain

10/24/ GridLayout With grid layout, the components arrange themselves in a matrix formation (rows, columns) Either the row or column must be non-zero The non-zero dimension is fixed and the zero dimension is determined dynamically The dominating parameter is the rows

10/24/ GridLayout 2,4 0,4 4,4 10, 10

10/24/ BorderLayout With border layout, the window is divided into five areas: Components are added to the frame using a specified index: container.add(new JButton(“East”), BorderLayout.EAST); BorderLayout.NORTH BorderLayout.WESTBorderLayout.CENTERBorderLayout.EAST BorderLayout.SOUTH

10/24/ BorderLayout

10/24/ BorderLayout The components stretch in this manner: –North and South stretch horizontally –East and West stretch vertically –Center can stretch in both directions to fill space The default location for a component is BorderLayout.CENTER If you add two components to the same location, only the last one will be displayed It is unnecessary to place components to occupy all areas

10/24/ Color The color of GUI components can be set using the java.awt.Color class Colors are made of red, green and blue components which range from 0 (darkest shade) to 255 (lightest shade) Each UI component has a background and foreground: Color color = new Color(128, 0, 0); JButton button = new JButton(); button.setBackground(color);// reddish button.setForeground(new Color(0, 0, 128)); // blueish

10/24/ Color There are 13 constant colors defined in Color : –BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW

10/24/ Panels Write a program to organize the components for a microwave oven: The problem is we want to use different layouts for different components text field 12 buttons button

10/24/ Panels The window can be subdivided into different panels The panels act as sub-containers for grouping UI components 12 buttons text field button Panel 1 uses a grid layout Panel 2 uses a border layout: text: North Panel 1: Center The content pane uses a border layout: Panel2: East Button: Center

10/24/ Fonts You can create a font using the Font class public Font(String name, int style, int size); The standard fonts are “ SansSerif”, “ Serif”, “ Monospaced”, “ Dialog”, or “ DialogInput” The styles are Font.PLAIN, Font.BOLD, Font.ITALIC, and Font.BOLD + Font.ITALIC Font font1 = new Font(“SansSerif”, Font.BOLD, 16); Font font2 = new Font(“Serif”, Font.ITALIC, 12); JButton button = new JButton(“OK”); button.setFont(font1);

10/24/ Graphics Graphics can be drawn using a class which extends JPanel Swing will call the paintComponent method to draw: protected void paintComponent(Graphics g); There are a variety of drawing methods: drawString(String string, int x, int y); drawLine(int x1, int y1, int x2, int y2); drawRect(int x, int y, int w, int h); drawOval(int x, int y, int w, int h); drawPolygon(int[] xpoints, int[] ypoints, int npoints);

10/24/ Graphics filled rectangle unfilled rectangle line filled oval filled polygon unfilled oval filled arc unfilled arc string