© L.Lúcio, 2004 1 An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;

Slides:



Advertisements
Similar presentations
Drawing in a frame – Java GUI
Advertisements

Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Graphical User Interfaces
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
Intro to Java 2 By Geb Thomas Based on the Java TutorialJava Tutorial.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
GUI programming Graphical user interface-based programming.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
CS Lecture 01 Frames and Components and events Lynda Thomas
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
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 – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
Review_6 AWT, Swing, ActionListener, and Graphics.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Graphical User Interfaces
Java Applet What is a Java Applet? How is applet compiled?
Graphical User Interfaces -- GUIs
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Graphical user interface-based programming
A Quick Java Swing Tutorial
Advanced Programming in Java
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes; u Platform independent (looks the same everywhere); u Shouldn’t mix AWT and Swing components in the same container; u Class hierarchy roughly the same (In Swing starts with “J”) n AWT tutorial: n Swing tutorial :

© L.Lúcio, All GUIs have a top-level container n In Swing: JFrame, JDialog, JApplet n In a GUI all components are structured hierarchically starting from the top-level container:

© L.Lúcio, How to make a simple “Hello World” n Extends class JFrame (could also instantiate one); n Adds a simple label component with text. import javax.swing.*; public class HelloWorld extends JFrame { public HelloWorld() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Hello World"); this.getContentPane().add(label); } public static void main(String[] args) { HelloWorld test = new HelloWorld(); test.setUndecorated(false); test.pack(); test.setVisible(true); }

© L.Lúcio, Adding a grid of Buttons n Use JButton components; n Need a way of distributing the buttons in a grid-like layout: u Use the GridLayout class from AWT; import javax.swing.*; import java.awt.*; public class GridLayoutDemo extends JFrame { static final int n = 4; public GridLayoutDemo() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel= new JPanel(); GridLayout grid = new GridLayout(n, n); panel.setLayout(grid); for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++) panel.add(new JButton("0")); this.setContentPane(panel); }...

© L.Lúcio, What do the Buttons do? n For the moment nothing… n Need to associate actions to each of the buttons: u Example: each button will count the number of times it was clicked. Button Click Listener ActionPerformed n Listener classes implement the AWT interface ActionListener overriding the ActionPerformed method;

© L.Lúcio, What do the Buttons do? (2) n Declare the class as a listener:... import java.awt.event.*; public class GridLayoutDemo extends JFrame implements ActionListener{ n Associate the this object to each button as a listener for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ int k = i * n + j; JButton oneButton = new JButton("0"); oneButton.addActionListener(this); oneButton.setActionCommand(String.valueOf(k)); buttonList.add(oneButton); panel.add(oneButton); } n All the buttons are put in an ArrayList (by number)

© L.Lúcio, What do the Buttons do? (3) n Implement the ActionPerformed method: public void actionPerformed(ActionEvent e) { JButton button = (JButton)buttonList.get(Integer.parseInt(e.getActionCommand())); int previous = Integer.parseInt(button.getText()); System.out.println(previous); button.setText(String.valueOf(++previous)); } n Get the button from the list according to its command string and increment its label.