Dr. Hikmat A. M. AbdelJaber

Similar documents
Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Laying Out Components. What is Widget Layout?

Starting Out with Java: From Control Structures Through Objects Sixth Edition

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Containers and Components

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Top-Level Containers

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Graphical User Interfaces. Comp 152

Part I: Learn Common Graphics Components

What is Widget Layout? COSC 3461 User Interfaces. Hierarchical Widget Layout. Resizing a Window. Module 5 Laying Out Components

Java. GUI building with the AWT

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Resizing a Window. COSC 3461: Module 5B. What is Widget Layout? Size State Transitions. What is Widget Layout? Hierarchical Widget Layout.

Introduction to the JAVA UI classes Advanced HCI IAT351

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Graphical User Interfaces in Java - SWING

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Graphic User Interfaces. - GUI concepts - Swing - AWT

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Window Interfaces Using Swing Objects

INTRODUCTION TO (GUIS)

2110: GUIS: Graphical User Interfaces

encompass a group of features for building Graphical User Interfaces (GUI).

Systems Programming Graphical User Interfaces

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Agenda. Container and Component

Introduction. Introduction

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Window Interfaces Using Swing Objects

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

DM503 Programming B. Peter Schneider-Kamp.

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Unit 6: Graphical User Interface

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Building Java Programs Bonus Slides

GUI in Java TalentHome Solutions

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

CS 251 Intermediate Programming GUIs: Components and Layout

Chapter 7: A First Look at GUI Applications

Graphical User Interfaces

Using Several Components

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Basicsof. JavaGUI and SWING

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Chapter 12 Advanced GUIs and Graphics

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Together, the appearance and how user interacts with the program are known as the program look and feel.

Dr. Hikmat A. M. AbdelJaber

Graphical User Interface

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

CHAPTER 2. Java Overview

The Java Programming Language Basics. Identifiers, Keywords, and Types. Expressions and Flow Control. Object-Oriented Programming. Objects and Classes

Java Programming Lecture 6

Example: Building a Java GUI

Example: Building a Java GUI

JAVA NOTES GRAPHICAL USER INTERFACES

CS 1316 Exam 3 Fall 2009

JFrame In Swing, a JFrame is similar to a window in your operating system

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

FirstSwingFrame.java Page 1 of 1

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

CS11 Java. Fall Lecture 4

Part 3: Graphical User Interface (GUI) & Java Applets

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

SampleApp.java. Page 1

Lecture 5: Java Graphics

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Layout. Dynamic layout, Swing and general layout strategies

User interfaces and Swing

JFrame & JLabel. By Iqtidar Ali

Graphical User Interfaces (GUIs)

AP CS Unit 11: Graphics and Events

CSIS 10A Assignment 7 SOLUTIONS

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

Object Oriented Design & Patterns. Part 1

Transcription:

Dr. Hikmat A. M. AbdelJaber

JWindow: is a window without a title bar or move controls. The program can move and resize it, but the user cannot. It has no border at all. It optionally has a parent JFrame. JFrame is a resizable, movable window with title bar and close button. Usually it contains JPanels. The entire application is usually a JFrame. JPanel is a region internal to a JFrame or another JPanel. Used for grouping components together. Optionally bounded by a visible border. Lives inside some enclosing Container.

Description. A JPanel is an empty area that can be used either to: 1. Layout other components, including other panels. 2. Draw graphics on. Use a JPanel to layout other components Declare and create the JPanel: Example: JPanel p = new JPanel();

Set the layout and other characteristics Example: p.setlayout(new BorderLayout()); p.setpreferredsize(new Dimension(200, 100)); p.setbackground(color.blue); You may want to set these characteristics in the constructor of a new class that extends JPanel. The default layout for a JPanel is FlowLayout, but it is a good idea to explicitly set it to avoid any problems with future changes in the default.

Add components Use add(...) to add components to a panel. Example: p.add(plusbutton, BorderLayout.NORTH); p.add(minusbutton, BorderLayout.SOUTH);

Constructors The most common JPanel constructor has no parameters. This creates a panel with the default layout (FlowLayout). Call setlayout() to change the layout. JPanel p = new JPanel(); p.setlayout(new FlowLayout()); You can optionally specify the layout in the Jpanel constructor call. JPanel p = new JPanel(new FlowLayout()); However, this doesn't work with BoxLayout, so calling setlayout() is still the preferred style of many programmers

Common Methods p.setlayout(layoutmanager layout); p.setpreferredsize(new Dimension(int width, int height)); p.setbackground(color c); p.add(some component); w = p.getwidth(); // Used for graphics, not components h = p.getheight(); // Used for graphics, not components

Example JPanel p = new JPanel(); p.setlayout(new GridLayout(4,3)); // 4 rows & 3 columns JButton bx = new JButton("Top left"); p.add(bx); // bx goes into first grid element.... // add more elements to the panel. Show the program: Step_by_Step_Package_Ch3 Adapter class: provides a default implementation (with an empty method body) of each method in the interface. Ex: WindowAdapter, KeyAdapter, MouseAdapter, etc.

All layout managers implement the interface LayoutManager by an object as a parameter to the method setlayout( ). Three ways to arrange components in a GUI: 1. Absolute positioning: Set container s layout to null Reposition all components (by resize event which can be processed by ComponentListener). 2. Visual programming in an IDE. 3. Layout managers: FlowLayout : default for JPanel BorderLayout : default for JFrame GridLayout GridBagLayout

It adds the components from left to right, and the order can be specified by using the index in add method. Ex. add(textfield, 2); The components are CENTER aligned by default. Use setalignment( FlowLayout.LEFT ).CENTER.RIGHT The LayoutManager interface method layoutcontainer(getcontentpane()) is used to rearrange the components on the JFrame based on the adjusted layout.

import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; First Program FlowLayoutFrame.java public class FlowLayoutFrame extends JFrame private JButton leftjbutton = new JButton("Left"); private JButton centerjbutton = new JButton("Center"); private JButton rightjbutton = new JButton("Right"); private FlowLayout layout; public FlowLayoutFrame() super("flowlayout Demo"); layout = new FlowLayout(); // default alignment = CENTER setlayout(layout); add(leftjbutton); add(centerjbutton); add(rightjbutton);

leftjbutton.addactionlistener( new ActionListener() public void actionperformed(actionevent e) layout.setalignment(flowlayout.left); layout.layoutcontainer(getcontentpane()); ); centerjbutton.addactionlistener( new ActionListener() public void actionperformed(actionevent e) layout.setalignment(flowlayout.center); layout.layoutcontainer(getcontentpane()); ); rightjbutton.addactionlistener( new ActionListener() public void actionperformed(actionevent e) layout.setalignment(flowlayout.right); layout.layoutcontainer(getcontentpane()); );

import javax.swing.jframe; Second Program FlowLayoutTest.java public class FlowLayoutTest public static void main(string[] args) FlowLayoutFrame flowlayoutframe = new FlowLayoutFrame(); flowlayoutframe.setdefaultcloseoperation(jframe.exit_on_close); flowlayoutframe.setsize(300, 100); flowlayoutframe.setvisible(true);

Output

The default for JFrame. Arranges the components into 5 regions: NORTH, SOUTH, EAST, WEST, CENTER Show the program new BorderLayout( hgap, vgap); Horizontal gap Vertical gap By default, the component is added to CENTER. Ex. Add(button1); add(button1,borderlayout.center);

import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; public class BorderLayoutFrame extends JFrame implements ActionListener private JButton[] buttons; private final String[] names = "Hide North","Hide South","Hide East", "Hide West","Hide Center"; private BorderLayout layout; public BorderLayoutFrame() super("borderlayout Demo"); layout = new BorderLayout(5,5); // hgap & vgap setlayout(layout); buttons = new JButton[names.length]; for( int count=0; count<names.length; count++) buttons[count] = new JButton(names[count]); buttons[count].addactionlistener(this); First Program BorderLayoutFrame.java

add(buttons[0], BorderLayout.NORTH); add(buttons[1], BorderLayout.SOUTH); add(buttons[2], BorderLayout.EAST); add(buttons[3], BorderLayout.WEST); add(buttons[4], BorderLayout.CENTER); public void actionperformed(actionevent e) for( JButton button: buttons) if(e.getsource()==button) button.setvisible(false); else button.setvisible(true); layout.layoutcontainer(getcontentpane());

import javax.swing.jframe; Second Program BorderLayoutTest.java public class BorderLayoutTest public static void main(string[] args) BorderLayoutFrame borderlayoutframe = new BorderLayoutFrame(); borderlayoutframe.setdefaultcloseoperation(jframe.exit_on_close); borderlayoutframe.setsize(500, 300); borderlayoutframe.setlocation(400, 200); borderlayoutframe.setvisible(true);

Output

Every component have the same width and height. Components are added row after row, from left to right cell. Show the program

import javax.swing.*; import javax.swing.*; public class GridLayoutFrame extends JFrame private final JButton button = new JButton("OK"); private final JLabel label = new JLabel("Label"); private final JTextField textfield = new JTextField("Text Field"); private final JCheckBox checkbox = new JCheckBox("chekBox"); private final JComboBox combobox = new JComboBox(); private final JList list = new JList(); private final GridLayout gridlayout; public GridLayoutFrame() super("gridlayout Demo"); gridlayout = new GridLayout(3,2,15,15);// 3 rows,2 columns,15 hgap,15 vgap setlayout(gridlayout);

add(button); add(label); add(textfield); add(checkbox); add(combobox); add(list); public static void main(string[] args) GridLayoutFrame gridlayout = new GridLayoutFrame(); gridlayout.setdefaultcloseoperation(jframe.exit_on_close); gridlayout.setsize(300, 200); gridlayout.setlocation(400, 200); gridlayout.setvisible(true);

Output

Modify the application by changing the Java icon/image located in left of the title bar to a lock icon/image. Do this task for both frames: password and ABC Bank.