Using Several Components

Similar documents
Graphic User Interfaces. - GUI concepts - Swing - AWT

H212 Introduction to Software Systems Honors

DM503 Programming B. Peter Schneider-Kamp.

Introduction. Introduction

User interfaces and Swing

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!

Dr. Hikmat A. M. AbdelJaber

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

Introduction to the JAVA UI classes Advanced HCI IAT351

Top-Level Containers

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

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

Java. GUI building with the AWT

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

Building Java Programs Bonus Slides

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

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

FirstSwingFrame.java Page 1 of 1

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

Programming Languages and Techniques (CIS120)

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

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

Building Graphical User Interfaces. GUI Principles

Window Interfaces Using Swing Objects

Containers and Components

CS 251 Intermediate Programming GUIs: Components and Layout

Agenda. Container and Component

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

Building Graphical User Interfaces. Overview

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

Abstract, Interface, GUIs. Ch. 11 & 16

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

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

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

Part I: Learn Common Graphics Components

Graphical User Interfaces in Java - SWING

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

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

UML and OOD. Some terms related to OOD. Dependency. Coupling. Interfaces. Multiple Interfaces

JLayeredPane. Depth Constants in JLayeredPane

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

1. Swing Note: Most of the stuff stolen from or from the jdk documentation. Most programs are modified or written by me. This section explains the

Window Interfaces Using Swing Objects

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

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

AP CS Unit 11: Graphics and Events

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

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

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

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

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

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

MIT AITI Swing Event Model Lecture 17

Systems Programming Graphical User Interfaces

EPITA Première Année Cycle Ingénieur. Atelier Java - J5

Packages: Putting Classes Together

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

Chapter 9 Designing Graphical User Interfaces (GUIs)

Chapter 7: A First Look at GUI Applications

Graphical User Interfaces. Comp 152


Lecture 5: Java Graphics

CSIS 10A Assignment 7 SOLUTIONS

CS108, Stanford Handout #19. Swing GUI

Java: Graphical User Interfaces (GUI)

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

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

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

No SVN checkout today. Object-Oriented Design

Graphical User Interface (GUI)

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

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Graphical User Interface (GUI)

CS193k, Stanford Handout #3. Swing 1

Swing from A to Z Some Simple Components. Preface

import javax.swing.*; import java.awt.*; import java.awt.event.*;

INTRODUCTION TO (GUIS)

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

1005ICT Object Oriented Programming Lecture Notes

Laying Out Components. What is Widget Layout?

GUI in Java TalentHome Solutions

Graphical User Interfaces (GUIs)

Example: Building a Java GUI

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

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

Graphical User Interfaces

CHAPTER 2. Java Overview

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Example: Building a Java GUI

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

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

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

Window Interfaces Using Swing. Chapter 12

CS11 Java. Fall Lecture 4

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

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

Transcription:

Ch. 16 pt 2 GUIs

Using Several Components How do we arrange the GUI components? Using layout managers. How do we respond to event from several sources? Create separate listeners, or determine the source of the event with a single listener.

A mini calculator

//MiniCalc.java - demo GridLayout import java.awt.*; import javax.swing.*; class MiniCalc { public static void main(string[] args) { JFrame frame = new JFrame("MiniCalc"); Container pane = frame.getcontentpane(); // create the major components JTextField firstnumber = new JTextField(20); JTextField secondnumber = new JTextField(20); JTextField result = new JTextField(20); JButton addbutton = new JButton("Add"); JButton subbutton = new JButton("Subtract");

// there will be 4 rows of 2 components each pane.setlayout(new GridLayout(4, 2)); // add all of the components to the content pane pane.add(new JLabel("Enter a number")); pane.add(firstnumber); pane.add(new JLabel("Enter a number")); pane.add(secondnumber); pane.add(new JLabel("Result")); pane.add(result); pane.add(addbutton); pane.add(subbutton); // setup the listener, listening to the buttons DoMath listener = new DoMath(firstNumber, secondnumber, result); subbutton.addactionlistener(listener); addbutton.addactionlistener(listener); frame.pack(); frame.show();

//DoMath.java - respond to two different buttons import javax.swing.*; import java.awt.event.*; class DoMath implements ActionListener { private JTextField inputone, inputtwo, output; DoMath(JTextField first, JTextField second, JTextField result) { inputone = first; inputtwo = second; output = result; public void actionperformed(actionevent e) { double first, second; first = Double.parseDouble(inputOne.getText().trim()); second = Double.parseDouble(inputTwo.getText().trim()); if (e.getactioncommand().equals("add")) output.settext(string.valueof(first + second)); else output.settext(string.valueof(first - second));

The DoMath object gets access to the input/ output text fields through the ActionEvent parameter passed through the actionperformed() method.

Border Layout Manager

class BorderLayoutTest { // import statements omitted public static void main(string[] args) { JFrame frame = new JFrame("BorderLayout"); Container pane = frame.getcontentpane(); pane.add( new JButton("North Button"),BorderLayout.NORTH); pane.add( new JButton("South Button"),BorderLayout.SOUTH); pane.add( new JButton("East Button"), BorderLayout.EAST); pane.add( new JButton("West Button"), BorderLayout.WEST); pane.add( new JButton("Center Button"),BorderLayout.CENTER); frame.pack(); frame.setvisible(true);

FlowLayout Layout Managers Adjusts layout as window size changes (default) GridLayout Displays components of specified rows and columns GridBagLayout Lays components in a grid; some components span mulrple cells. This is a sophisrcated layout manager. zybooks has a good example BorderLayout Arranges components in N, S, E, W, and Center

BoxLayout Layout Managers Puts components in a single row or column. CardLayout Lets you implement an area that contains different components at different Rmes. A CardLayout is oyen controlled by a combo box. GroupLayout See java documentaron for details SpringLayout Lets you specify precise relaronships between the edges of components under its control.

Drawing with Swing Drawing is done on an instance of class java.awt.graphics (or java.awt.graphics2d). Using Graphics, units are always in terms of pixels. The origin is in the upper ley corner with posirve direcrons being down and to the right.

Drawing with Swing Many useful methods for drawing provided by java.awt.graphics class. See documentaron hzps://docs.oracle.com/javase/7/docs/api/ java/awt/graphics.html

(0,0) (0, w) (50,20) (150,60) (h, 0) (h, w) 14

//StarTest.java - display a starburst import java.awt.*; import javax.swing.*; class StarTest { public static void main(string[] args) { JFrame frame = new JFrame("StarTest"); Container pane = frame.getcontentpane(); Star star = new Star(); pane.add(star); frame.pack(); frame.setvisible(true);

class Star extends JComponent {//import statements omitted public void paintcomponent(graphics g) { for (double angle = 0; angle < Math.PI; { double x1, x2, y1, y2; angle = angle + Math.PI / 16) // compute coordinates of endpoints of a line // cosine and sine range from -1 to 1 // multiplying by RADIUS changes the // range to be from -RADIUS to RADIUS // adding RADIUS gives the final range of // 0 to 2 * RADIUS x1 = Math.cos(angle) * RADIUS + RADIUS; y1 = Math.sin(angle) * RADIUS + RADIUS; x2 = Math.cos(angle + Math.PI) * RADIUS +RADIUS; y2 = Math.sin(angle + Math.PI) * RADIUS +RADIUS; g.drawline((int)x1, (int)y1, (int)x2, (int)y2);

// make the JComponent big enough to show the image public Dimension getminimumsize() { return new Dimension(2 * RADIUS, 2 * RADIUS); public Dimension getpreferredsize() { return new Dimension(2 * RADIUS, 2 * RADIUS); private static final int RADIUS = 100; getminimumsize() and getpreferredsize() determine the size of the JComponent.

NesRng Containers

class Nesting { //import statements omitted public static void main(string[] args) { JFrame frame = new JFrame("Nesting containers"); Container pane = frame.getcontentpane(); JPanel panel = new JPanel(); panel.setlayout(new GridLayout(4, 1)); panel.add(new JButton("One")); panel.add(new JButton("Two")); panel.add(new JButton("Three")); panel.add(new JButton("Four")); pane.add(panel, BorderLayout.WEST); pane.add(new Star(), BorderLayout.CENTER); frame.pack(); frame.setvisible(true);

Mouse Input This next program will allow us to draw on the screen with a mouse.

//SimplePaint.java - a program to draw with the mouse import java.awt.*; import javax.swing.*; class SimplePaint { public static void main(string[] args) { JFrame frame = new JFrame("SimplePaint"); Container pane = frame.getcontentpane(); DrawingCanvas canvas = new DrawingCanvas(); PaintListener listener = new PaintListener(); canvas.addmousemotionlistener(listener); pane.add(canvas); frame.pack(); frame.setvisible(true);

// DrawingCanvas.java - a blank Canvas import java.awt.*; import javax.swing.*; class DrawingCanvas extends JComponent { //these methods are needed to give the component a size public Dimension getminimumsize() { return new Dimension(SIZE, SIZE); public Dimension getpreferredsize() { return new Dimension(SIZE, SIZE); private static final int SIZE = 500;

//PaintListener.java - do the actual drawing import java.awt.*; import java.awt.event.*; class PaintListener implements MouseMotionListener { public void mousedragged(mouseevent e) { DrawingCanvas canvas = (DrawingCanvas)e.getSource(); Graphics g = canvas.getgraphics(); g.filloval(e.getx() - radius, e.gety() radius, diameter, diameter); public void mousemoved(mouseevent e){ private int radius = 3; private int diameter = radius * 2;