CPSC 233 Tutorial #17. GUI programming Objective – To understand better how to create simple interactive GUI programs.

Slides:



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

GUI Tutorial Day 3. Custom Dialog Create, display, hide, retrieve information.
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.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Chapter 121 Window Interfaces Using Swing Chapter 12.
Final Review. Review A Binary Search divides a sorted data structure into two equal parts and each time throws away the part that cannot contain the item.
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.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
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.
Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 13 l HTML l Applets Applets and HTML.
Programming With Java ICS201 University Of Ha’il1 Chapter 17 Graphical User Interfaces Swing I.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.
Chapter 12- GUI’s, Java, and Swing.. Overview n What are GUI’s n How Java does GUI’s- Swing n Buttons n Containers n Text I/O and Swing n Review.
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
GUI programming Graphical user interface-based programming.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
CS Lecture 01 Frames and Components and events Lynda Thomas
More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
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.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-2 ( Book Chapter 14) GUI and Event-Driven Programming.
1 Lecture 25 Listening to buttons and mice Quotes by Tony Hoare There are two ways of constructing a software design: (1) make it so simple that there.
Computer Science 209 GUIs Model/View/Controller Layouts.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Creating a GUI Class An example of class design using inheritance and interfaces.
GUI Tutorial 1. A Bit of Philosophy on what to Teach  There are numerous libraries, frameworks, options  Modern GUIs are often developed using XML (e.g.,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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.
GUI Programming in Java Hao Jiang Boston College April, 2009.
CSC 222: Computer Programming II
GUIs and Events Rick Mercer.
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 -- GUIs
Swing JComponents.
A Quick Java Swing Tutorial
Ellen Walker Hiram College
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Chapter 7-2 (Book Chapter 14)
The calculator – Java GUI
A Quick Java Swing Tutorial
어서와 Java는 처음이지! 제13장 실전프로젝트.
Window Interfaces Using Swing Objects Chapter 12
Chapter 7-2 (Book Chapter 14)
CiS 260: App Dev I Chapter 6: GUI and OOD.
Final Review.
Presentation transcript:

CPSC 233 Tutorial #17

GUI programming Objective – To understand better how to create simple interactive GUI programs.

import javax.swing.*; import java.awt.*; public class Example { JFrame frame; JButton b; public static void main(String[] args) { Example gui = new Example(); gui.go(); } public void go() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); b = new JButton("A"); b.addActionListener(); frame.getContentPane().add(BorderLayout.SOUTH, b); frame.setSize(200,100); frame.setVisible(true); } Be the compiler class BListener extends ActionListener { public void actionPerformed(ActionEvent e) { if (b.getText().equals("A")) b.setText("B"); else b.setText("A"); } import java.awt.event.*; new BListener() implements

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener{ public static final int WIDTH = 400; public static final int HEIGHT = 200; public static final int NUMBER_OF_DIGITS = 30; private JTextField ioField; private double result = 0.0; public Calculator() { setTitle("Simplified Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); setLayout(new BorderLayout()); JPanel textPanel = new JPanel(); textPanel.setLayout(new FlowLayout()); ioField = new JTextField("Enter numbers here.", NUMBER_OF_DIGITS); ioField.setBackground(Color.WHITE); textPanel.add(ioField); add(textPanel, BorderLayout.NORTH); JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(Color.GRAY); buttonPanel.setLayout(new FlowLayout()); Display 17.19

JButton addButton = new JButton("+"); addButton.addActionListener(this); buttonPanel.add(addButton); JButton subtractButton = new JButton("-"); subtractButton.addActionListener(this); buttonPanel.add(subtractButton); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(this); buttonPanel.add(resetButton); add(buttonPanel, BorderLayout.CENTER); } public static void main(String[] args) { Calculator aCalculator = new Calculator(); aCalculator.setVisible(true); } public void actionPerformed(ActionEvent e) { try { assumingCorrectNumberFormats(e); } catch (NumberFormatException e2) { ioField.setText("Error: Re-enter Number."); } public void assumingCorrectNumberFormats(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("+")){ result = result + stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } else if (actionCommand.equals("-")){ result = result - stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } else if (actionCommand.equals("Reset")){ result = 0.0; ioField.setText ("0.0"); } else ioField.setText("Unexpected error."); } private static double stringToDouble(String stringObject){ return Double.parseDouble(stringObject.trim()); }

Questions Why did we make the text field ioField an instance variable but did not make instance variables of the other buttons, addButton, subtractButton, or resetButton?

What would happen if the user running the GUI were to run the GUI and simply click the addition button without typing anything into the text field?

What would happen if the user running the GUI were to type the number 10 into the text field and then click the addition button three times?

What would happen if you change the main method to: public static void main(String[] args) { Calculator calculator1 = new Calculator(); Calculator1.setVisible(true); Calculator calculator2 = new Calculator(); Calculator2.setVisible(true); } If you click the close-window button in one of the windows, will one window go away or will both windows go away?

Exercise Add multiplication and division to the simple calculator Create a real simple calculator with digits and operators as separate buttons

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener{ public static final int WIDTH = 400; public static final int HEIGHT = 200; public static final int NUMBER_OF_DIGITS = 30; private JTextField ioField; private double result = 0.0; public Calculator() { setTitle("Simplified Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); setLayout(new BorderLayout()); JPanel textPanel = new JPanel(); textPanel.setLayout(new FlowLayout()); ioField = new JTextField("Enter numbers here.", NUMBER_OF_DIGITS); ioField.setBackground(Color.WHITE); textPanel.add(ioField); add(textPanel, BorderLayout.NORTH); JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(Color.GRAY); buttonPanel.setLayout(new FlowLayout()); Display 17.19

JButton addButton = new JButton("+"); addButton.addActionListener(this); buttonPanel.add(addButton); JButton subtractButton = new JButton("-"); subtractButton.addActionListener(this); buttonPanel.add(subtractButton); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(this); buttonPanel.add(resetButton); add(buttonPanel, BorderLayout.CENTER); } public static void main(String[] args) { Calculator aCalculator = new Calculator(); aCalculator.setVisible(true); } public void actionPerformed(ActionEvent e) { try { assumingCorrectNumberFormats(e); } catch (NumberFormatException e2) { ioField.setText("Error: Re-enter Number."); } public void assumingCorrectNumberFormats(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("+")){ result = result + stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } else if (actionCommand.equals("-")){ result = result - stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } else if (actionCommand.equals("Reset")){ result = 0.0; ioField.setText ("0.0"); } else ioField.setText("Unexpected error."); } private static double stringToDouble(String stringObject){ return Double.parseDouble(stringObject.trim()); }

import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class TextFieldDemo extends JFrame implements ActionListener{ public static final int WIDTH = 400; public static final int HEIGHT = 200; public static final int NUMBER_OF_CHAR = 30; private JTextField name; public TextFieldDemo() { super("Text Field Demo"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(2,1)); JPanel namePanel = new JPanel(); namePanel.setLayout(new BorderLayout()); namePanel.setBackground(Color.WHITE); name = new JTextField(NUMBER_OF_CHAR); namePanel.add(name, BorderLayout.SOUTH); JLabel nameLabel = new JLabel("Enter your name here"); namePanel.add(nameLabel, BorderLayout.CENTER); add(namePanel); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.setBackground(Color.GREEN); JButton actionButton = new JButton("Click me"); actionButton.addActionListener(this); buttonPanel.add(actionButton); JButton clearButton = new JButton("Clear"); clearButton.addActionListener(this); buttonPanel.add(clearButton); add(buttonPanel); } public void actionPerformed(ActionEvent e){ String actionCommand = e.getActionCommand(); if (actionCommand.equals("Click me")) name.setText("Hello " + name.getText()); else if (actionCommand.equals("Clear")) name.setText(""); else name.setText("Unexpected error."); } public static void main(String[] args) { TextFieldDemo gui = new TextFieldDemo(); gui.setVisible(true); } Display 17.17

Questions What is the difference between an object of the class JTextArea and an object of the JTextField?

What would happen if when running the GUI and you enter your name, then click the “Click me” button three times?

import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class TextFieldDemo extends JFrame implements ActionListener{ public static final int WIDTH = 400; public static final int HEIGHT = 200; public static final int NUMBER_OF_CHAR = 30; private JTextField name; public TextFieldDemo() { super("Text Field Demo"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(2,1)); JPanel namePanel = new JPanel(); namePanel.setLayout(new BorderLayout()); namePanel.setBackground(Color.WHITE); name = new JTextField(NUMBER_OF_CHAR); namePanel.add(name, BorderLayout.SOUTH); JLabel nameLabel = new JLabel("Enter your name here"); namePanel.add(nameLabel, BorderLayout.CENTER); add(namePanel); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.setBackground(Color.GREEN); JButton actionButton = new JButton("Click me"); actionButton.addActionListener(this); buttonPanel.add(actionButton); JButton clearButton = new JButton("Clear"); clearButton.addActionListener(this); buttonPanel.add(clearButton); add(buttonPanel); } public void actionPerformed(ActionEvent e){ String actionCommand = e.getActionCommand(); if (actionCommand.equals("Click me")) name.setText("Hello " + name.getText()); else if (actionCommand.equals("Clear")) name.setText(""); else name.setText("Unexpected error."); } public static void main(String[] args) { TextFieldDemo gui = new TextFieldDemo(); gui.setVisible(true); } Display 17.17