CREATING A SIMPLE GUI Mr. Crone. First GUI Today we will use Swing to create the following application:

Slides:



Advertisements
Similar presentations
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Advertisements

Things to mention public static void main(String [] args) imports comments –block comments /* … */ –single-line comments // –javadoc comments and tags.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Java Tutorial – Building GUIs Java with Added Swing Daniel Bryant
JFC and the Swing Package Feb 10, Java Foundation Class (JFC) zAWT (Abstract Window Toolkit) has been used since jdk1.0 Archaic and very poorly.
Object-Oriented Software Engineering Java with added Swing.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Graphical User Interfaces
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L20 (Chapter 24) Multithreading.
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.
Java Swing Joon Ho Cho. What is Java Swing? Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Graphical User Interface (GUI) Design using Swing Course Lecture Slides.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Week 3, Day 1: Processes & Threads Return Quiz Processes Threads Lab: Quiz Lab 3: Strategy & Factory Patterns! SE-2811 Slide design: Dr. Mark L. Hornick.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
1 Java Swing - Components and Containment. 2 Components and Containers Components and Containers Components Components The building blocksThe building.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
GUI programming Graphical user interface-based programming.
Week 3, Day 1: Processes & Threads Processes Threads SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets.
1 Web Based Programming Section 8 James King 12 August 2003.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Concurrent Programming and Threads Threads Blocking a User Interface.
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.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
 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.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
GUIs Graphical User Interfaces. Everything coming together Known: – Inheritance – Interfaces – Abstract classes – Polymorphism – Exceptions New: – Events.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
Review_6 AWT, Swing, ActionListener, and Graphics.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
Basics of GUI Programming Chapter 11 and Chapter 22.
GUI Components. The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be.
Java Swing One of the most important features of Java is its ability to draw graphics.
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)
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
Computer Science 209 Software Development Packages.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
Welcome To java
Java Applet What is a Java Applet? How is applet compiled?
Introduction to Graphical Interface Programming in Java
30 Java Applets.
Graphical user interface-based programming
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
class PrintOnetoTen { public static void main(String args[]) {
Steps to Creating a GUI Interface
CiS 260: App Dev I Chapter 6: GUI and OOD.
CMSC 202 Threads.
Presentation transcript:

CREATING A SIMPLE GUI Mr. Crone

First GUI Today we will use Swing to create the following application:

Creating the Source Code Create a new class called SwingDemo Import the entire Swing Package import javax.swing.*; public class SwingDemo{ }

Creating the Source Code Create a Constructor with no parameters Create a main method import javax.swing.*; public class SwingDemo{ public SwingDemo(){ } public static void main(String args[]){ }

Creating the Source Code Inside the constructor: public SwingDemo(){ JFrame jfrm = new JFrame(“A Simple Swing Program”); jfrm.setSize(275, 100); //x value, y value }

Creating the Source Code Inside the constructor: public SwingDemo(){ JFrame jfrm = new JFrame(“A Simple Swing Program”); jfrm.setSize(275, 100); //x value, y value jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Stops the program from running after closing the window Jlabel jlab = new Jlabel(“Swing powers the modern Java GUI”); }

Creating the Source Code Inside the constructor: public SwingDemo(){ JFrame jfrm = new JFrame(“A Simple Swing Program”); jfrm.setSize(275, 100); //x value, y value jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Stops the program from running after closing the window JLabel jlab = new JLabel(“Swing powers the modern Java GUI”); jfrm.getContentPane().add(jlab); //add label to the content pane jfrm.setVisible(true); //Show the container }

Creating the Source Code Inside the main method: public static void main(String[] args){ public void run(){ new SwingDemo(); }

Creating the Source Code Inside the main method: public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ new SwingDemo(); } }); }

Complete Source Code import javax.swing.*; public class SwingDemo { public SwingDemo(){ JFrame jfrm = new JFrame("A Simple Swing Program"); // top - level container jfrm.setSize(275, 100); // Set the size jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // stop program when closed JLabel jlab = new JLabel("Swing powers the modern Java GUI"); // Create a new Label jfrm.getContentPane().add(jlab); // Add the Label to the Content Pane which is contained within the JFrame jfrm.setVisible(true); // Set the visibility to True } public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ new SwingDemo(); } }); }

The main Method Explained The source code within the main method causes a SwingDemo object to be created on the event-dispatching thread rather than on the main thread To prevent problems with multiple threads, we launch the program from the event-dispatching thread The GUI can be launched from the main method using the main thread, however this will not work with every GUI To remain safe and compatible with all GUI’s, we will continue to use the event-dispatching thread

References: Swing: A Beginner’s Guide (Osborne Mcgraw Hill) - Herbert Schildt