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.

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

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Introduction to Swing Components Chapter 14. Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
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.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
Graphical User Interfaces
Graphical User Interfaces Java’s AWT and Swing APIs.
TCU CoSc Programming with Java Visual Design (Chapter 5)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
Graphical User Interfaces CS 2110 Spring Ivan Sutherland: “Sketchpad”,
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh 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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
CS3 - AWT/Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
Java Programming Chapter 10 Graphical User Interfaces.
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.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
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.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Manager Summary
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
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.
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.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Java Layouts CSIS 3701: Advanced Object Oriented Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Graphical User Interfaces
Graphical User Interfaces -- GUIs
Swing JComponents.
Modern Programming Language Java
Java Swing.
Graphical User Interface (pronounced "gooey")
Chapter 13: Advanced GUIs and Graphics
Containers and Components
תכנות מכוון עצמים בשפת JAVA
Steps to Creating a GUI Interface
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

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 to do extra credit and have some fun on the project. GUIs are a good example of Object Oriented Programming. GUIs are another example of a container.

09/26/2007 CMSC 341 GUI 3 Java and GUIs There are two packages that generate GUI components in Java.  java.awt  javax.swing The AWT (Abstract Windows Toolkit)  Came first  No platform independence Swing  Part of Java Foundation Classes (released with Java 2)  Built on top of the AWT  Offers platform independence

09/26/2007 CMSC 341 GUI 4 Containers In Java, all GUI objects go into a Container. A top level container can stand alone in a web browser or in an operating system.  JFrame JFrame  JApplet JApplet Some containers may only be added to other containers.  JPanel JPanel

09/26/2007 CMSC 341 GUI 5 JFrame Methods add(Object) - adds objects to the frame. setVisible(boolean) - makes the frame visible setLocation(int x, int y) – aligns top left corner of frame with coordinates on screen setSize(int width, int height) – sets size of frame in pixels setDefaultCloseOperation(Windows.const ants.EXIT_ON_CLOSE);

09/26/2007 CMSC 341 GUI 6 JFrame Code import javax.swing.*; import java.awt.*; public class UpperCaseConverter extends JFrame { public UpperCaseConverter(String name){ super(name); setLocation(300, 100); setSize (400,300); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String args[]){ UpperCaseConverter ucc = new UpperCaseConverter("Convert to Upper Case"); ucc.setVisible(true); } Constructor sets the title, size and location of the JFrame Instantiates JFrame and makes it visible Makes program end when window is closed

09/26/2007 CMSC 341 GUI 7 JFrame Example The code on the previous page renders the following:

09/26/2007 CMSC 341 GUI 8 LayoutManagers Every container has an underlying default LayoutManager. LayoutManager The LayoutManager determines  the size of the objects in the container and  how the objects will be laid out. The default LayoutManager for a JFrame is a BorderLayout. BorderLayout

09/26/2007 CMSC 341 GUI 9 BorderLayout Divides container into five regions  BorderLayout.NORTH  BorderLayout.SOUTH  BorderLayout.CENTER  BorderLayout.EAST  BorderLayout.WEST One component per region Component takes size of region Center region is greedy Components are added to center by default

09/26/2007 CMSC 341 GUI 10 BorderLayout Code import java.awt.*; import javax.swing.*; public class BorderLayoutExample extends JFrame { public BorderLayoutExample(String name) { super(name); setSize(300,300); add(new JButton("North"), BorderLayout.NORTH); add(new JButton("South"), BorderLayout.SOUTH); add(new JButton("East"), BorderLayout.EAST); add(new JButton("West"), BorderLayout.WEST); add(new JButton("Center"), BorderLayout.CENTER); } public static void main(String args[]) { BorderLayoutExample b = new BorderLayoutExample("BorderLayoutExample"); b.setVisible(true); } Specialized add method for adding to regions

09/26/2007 CMSC 341 GUI 11 BorderLayoutExample

09/26/2007 CMSC 341 GUI 12 JPanel However, we want to put several buttons in the North region of the GUI, but BorderLayout only allows one component per region… Add a second level container like a JPanel.JPanel JPanels have a FlowLayout manager by default.FlowLayout

09/26/2007 CMSC 341 GUI 13 FlowLayout Lays components in a fluid direction as determined by its orientation. By default, orientation is L -> R, T -> B. Possible to set the horizontal and vertical width between components. Components take preferred size.  For buttons, preferred size is the size of the text within them.

09/26/2007 CMSC 341 GUI 14 JPanel and FlowLayout Code //omitting code here from previous example public class UpperCaseConverter extends JFrame { //Since we are expecting to make these components to //react to user interaction we make them object data JButton upper; JButton clear; public UpperCaseConverter(String name){ //omitting code here from previous example JPanel top; top = new JPanel(); upper = new JButton("UPPER"); clear = new JButton("CLEAR"); top.add(upper); top.add(clear); add(top, BorderLayout.NORTH); } //omitting code here from previous example }

09/26/2007 CMSC 341 GUI 15 JPanel and FlowLayout Example Code on previous page renders as follows: But, we also need a text field to enter text.

09/26/2007 CMSC 341 GUI 16 Second JPanel public class UpperCaseConverter extends JFrame { //code omitted from previous example JTextField input; public UpperCaseConverter(String name){ //code omitted from previous example JPanel bottom = new JPanel(); JLabel label = new JLabel("Enter text ->"); input = new JTextField(20); bottom.add(label); bottom.add(input); add(bottom, BorderLayout.SOUTH); } //code omitted from previous example } JLabelJLabel may also take an Icon or both a String and Icon in its constructor JTextFieldJTextField takes an int which indicates the number of characters to be displayed

09/26/2007 CMSC 341 GUI 17 Second JPanel Example How would we add a JTextArea to the center of our frame?JTextArea

09/26/2007 CMSC 341 GUI 18 JTextArea Add JTextArea reference to object data so that it can be referenced by all member methods. Instantiate JTextArea reference in constructor method and add reference to the center of the JFrame. JTextArea output; output = new JTextArea(10, 20); add(output); Declare outside of methods so object data Constructor for JTextArea takes number of rows and columnsJTextArea

09/26/2007 CMSC 341 GUI 19 JTextArea Example Next time, we will make this GUI functional.

09/26/2007 CMSC 341 GUI 20 JComponent Methods There exists several JComponent methods that allow you to change the look of a component  setBackground(Color)  setForeground(Color)  setFont(Font)  setPreferredSize(Dimension)  setAlignmentX(float)  setAlignmentY(float) Values for all the arguments of these methods are already defined in Java.

09/26/2007 CMSC 341 GUI 21 More LayoutManagers Seven Basic Layout Managers in Java  BorderLayout  BoxLayout  CardLayout  FlowLayout  GridLayout  GridBagLayout  OverlayLayout We will only focus on two more of these.  GridLayout GridLayout  BoxLayout BoxLayout

09/26/2007 CMSC 341 GUI 22 GridLayout Creates a grid with number of rows and columns given in the constructor One component per cell Cells of equal size Component take the size of the cell

09/26/2007 CMSC 341 GUI 23 GridLayout Code import java.awt.*; import javax.swing.*; public class ButtonGrid extends JFrame { public ButtonGrid() { super("Button Grid Example"); setLayout(new GridLayout(3,2)); setSize(300,400); add(new JButton("1")); add(new JButton("2")); add(new JButton("3")); add(new JButton("4")); add(new JButton("5")); add(new JButton("6")); } public static void main(String arg[]){ ButtonGrid bg = new ButtonGrid(); bg.setVisible(true); } Compare the order in which the buttons are added to the GUI on the next page. The setLayout method changes a container’s LayoutManager.

09/26/2007 CMSC 341 GUI 24 GridLayout Example

09/26/2007 CMSC 341 GUI 25 BoyLayout Components are arranged either vertically or horizontally depending on parameter  BoxLayout.X_AXIS  BoxLayout.Y_AXIS  BoxLayout.LINE_AXIS  BoxLayout.PAGE_AXIS Components will not wrap even if container is resized Allows for filler (“glue”) between components to make them space evenly within container Part of javax.swing package

09/26/2007 CMSC 341 GUI 26 BoxLayout Code import java.awt.*; import javax.swing.*; public class ButtonBox extends JFrame { public ButtonBox() { super("Button Box Example"); JPanel p = new JPanel(); JButton b1 = new JButton("B1"); JButton b2 = new JButton("B2"); JButton b3 = new JButton("B3"); b1.setAlignmentX(Component.CENTER_ALIGNMENT); b2.setAlignmentX(Component.CENTER_ALIGNMENT); b3.setAlignmentX(Component.CENTER_ALIGNMENT); p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS)); setSize(300,400); p.add(Box.createGlue()); p.add(b1);p.add(b2)p.add(b3); p.add(Box.createGlue()); add(p); } //main goes here } Constructor for a BoxLayout takes an instance of its container Component method to align the button horizontally Here is the “glue”

09/26/2007 CMSC 341 GUI 27 BoxLayout Example

09/26/2007 CMSC 341 GUI 28 Calculator What do we need for a Calculator GUI?  16 JButtons Numbers 0-9 Operators + - x / =.  3 JTextFields 2 operands 1output Which need to respond to events?

09/26/2007 CMSC 341 GUI 29 Declare Object Data import java.awt.*; import javax.swing.*; public class Calculator extends JFrame { JButton [] numbers = new JButton[10]; JButton plus; JButton minus; JButton mult; JButton div; JButton equals; JButton dot; JTextField output; JTextField operand1; JTextField operand2; }

09/26/2007 CMSC 341 GUI 30 Constructor Constructor is where everything will be created. Before beginning decide  how to break up your frame into panels,  which LayoutManager goes where,  what components will go where.

09/26/2007 CMSC 341 GUI 31 Instantiate Object Data public Calculator() { super("My Calculator"); numbers = new JButton[10]; for(int i = 0; i < 10; i++) numbers[i] = new JButton("" + i); plus = new JButton("+"); minus = new JButton("+"); mult = new JButton("x"); div = new JButton("/"); equals = new JButton("="); dot = new JButton("."); operand1 = new JTextField(10); operand2 = new JTextField(10); output = new JTextField(21); setSize(300,400); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } Setting properties for the frame, too

09/26/2007 CMSC 341 GUI 32 Top Panel Need to split the top panel into a grid with two panels. Why? JPanel top = new JPanel(); top.setLayout(new GridLayout(2,1)); add(top, BorderLayout.NORTH); JPanel input = new JPanel(); input.add(operand1); input.add(operand2); top.add(input); JPanel results = new JPanel(); results.add(output); top.add(results);

09/26/2007 CMSC 341 GUI 33 Rendering of Previous Code

09/26/2007 CMSC 341 GUI 34 The Center Panel The center will also consist of a grid with four rows and four columns. What happens if we add buttons directly to grid? What can we do to get our desired effect? What do we want the calculator to do when we resize?

09/26/2007 CMSC 341 GUI 35 Panels of Panels Often GUI programmers create methods to create Panels. private JPanel getRow(JButton b1, JButton b2, JButton b3, JButton b4) { JPanel row = new JPanel(); row.setLayout(new BoxLayout(row,BoxLayout.X_AXIS)); row.add(b1); row.add(b2); row.add(b3); row.add(b4); return row; }

09/26/2007 CMSC 341 GUI 36 Panels of Panels (cont.) Several calls to the method are made from the constructor. JPanel center = new JPanel(); center.setLayout(new GridLayout(4,1)); center.add(getRow(numbers[7], numbers[8], numbers[9], plus)); center.add(getRow(numbers[4], numbers[5], numbers[6], minus)); center.add(getRow(numbers[1], numbers[2], numbers[3], mult)); center.add(getRow(dot, numbers[0], equals, div)); add(center);

09/26/2007 CMSC 341 GUI 37 Calculator Adding the previous code, the calculator now renders like so.

09/26/2007 CMSC 341 GUI 38 Adding Glue Adjust the Panel method to incorporate some glue. private JPanel getRow(JButton b1, JButton b2, JButton b3, JButton b4) { JPanel row = new JPanel(); row.setLayout(new BoxLayout(row,BoxLayout.X_AXIS)); row.add(Box.createHorizontalGlue()); row.add(b1);row.add(b2);row.add(b3);row.add(b4); row.add(Box.createHorizontalGlue()); return row; }

09/26/2007 CMSC 341 GUI 39 Almost there. Now it looks like so.

09/26/2007 CMSC 341 GUI 40 Small Changes and Viola setSize(225,300) operand1 = new JTextField(7); operand2 = new JTextField(7); output = new JTextField(15); Next class we will make it functional using Java’s Event Delegation Model.