IEEM 110 Computing in Industrial Applications Basic User Interface in Java.

Slides:



Advertisements
Similar presentations
Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
Advertisements

Graphical User Interfaces
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
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.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
Java Swing 4 th April 2008 CS 180 Department of Computer Science, Purdue University.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
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 Programming Chapter 10 Graphical User Interfaces.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
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.
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.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
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
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
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.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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 Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
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.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Computer Science 209 GUIs Model/View/Controller Layouts.
Basics of GUI Programming Chapter 11 and Chapter 22.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Graphical User Interface (GUI)
Getting Started with GUI Programming Chapter 10 CSCI 1302.
GUIs & Event-Driven Programming Chapter 11 Review.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
Chapter 7 A First Look at GUI Applications Layout Managers.
Object-Orientated Analysis, Design and Programming
Graphical User Interfaces -- GUIs
Swing JComponents.
Modern Programming Language Java
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Timer class and inner classes
LayoutManagers The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes.
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

IEEM 110 Computing in Industrial Applications Basic User Interface in Java

Outline What is Graphical User Interface (GUI) What is Graphical User Interface (GUI) Window, Frame and Panel Window, Frame and Panel Labels and Text Inputs Labels and Text Inputs Action Listener Action Listener Layout Management Layout Management Sample Program Sample Program

What is Graphical User Interface? A method for: A method for: - providing input to a computer program - the computer program to display some output/data on the screen

GUI in Java Frame, Window and Panel Frame, Window and Panel An instance of a Frame = = A Window with border, title, buttons on top-right side to close window, … An instance of a Frame = = A Window with border, title, buttons on top-right side to close window, … Frames are example of containers Frames are example of containers can contain other components such as buttons, text fields can contain other components such as buttons, text fields A simple frame class A simple frame class Class SimpleFrame extends JFrame { public SimpleFrame(){ setSize( WIDTH, HEIGHT ); setTitle("Testing"); } public static final int WIDTH = 200; public static final int HEIGHT = 200; } Specify the size of the windowSpecify the title of the window

Window, Frame and Panel What is Panel? What is Panel? Panel act as a smaller containers for components Panel act as a smaller containers for components A simple Panel code put in the frame class A simple Panel code put in the frame class Container contentPane = getContentPane(); JPanel myPanel = new JPanel(); JLabel myLabel = new JLabel("You say:"); myPanel.add(myLabel); contentPane.add(myPanel, BorderLayout.SOUTH); Put the panel at the SOUTH area of the window

Window, Frame and Panel Example of frame class using a panel Example of frame class using a panel class MyFrame extends JFrame { public MyFrame() { setTitle("Testing");setSize(WIDTH,HEIGHT); Container contentPane = getContentPane(); JPanel myPanel = new JPanel(); myPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); myPanel.add(new Button("Button 1")); contentPane.add(myPanel); } public static final int WIDTH = 200; public static final int HEIGHT = 200; } Add a panel with a button

Window, Frame and Panel How to display the frame we have made? How to display the frame we have made? Add the code to the main function Add the code to the main function MyFrame mingsFrame = new MyFrame(); MyFrame mingsFrame = new MyFrame(); mingsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mingsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mingsFrame.Show(); mingsFrame.Show(); Define the window will close when button is pressed

GUI in Java Text Input / Label Text Input / Label Two components are used to get text input Two components are used to get text input Text fields in JTextfield for single line input Text fields in JTextfield for single line input Text Area in JTextArea for multiple lines input Text Area in JTextArea for multiple lines input Methods use in the Text Input Methods use in the Text Input void setText(String t) void setText(String t) Change the text of a text component Change the text of a text component String getText() String getText() Return the text contained in this text component Return the text contained in this text component

Text Input / Label Text Fields Text Fields How to add a text field to a window? How to add a text field to a window? JTextField(String text, int cols) JTextField(String text, int cols) text is the text to display, cols is the number of columns in the field text is the text to display, cols is the number of columns in the field Unfortunately, a “ column ” is a rather imprecise measurement in Java Unfortunately, a “ column ” is a rather imprecise measurement in Java Example: Example: JPanel myPanel = new JPanel(); JPanel myPanel = new JPanel(); JTextField myTextField = new JTextField("Hello", 10); JTextField myTextField = new JTextField("Hello", 10); myPanel.add(myTextField); myPanel.add(myTextField);

Text Input / Label Labels Labels How to add a label to a window? How to add a label to a window? JLabel(String text) JLabel(String text) text is the text to display text is the text to display Example: Example: JPanel myPanel = new JPanel(); JPanel myPanel = new JPanel(); JLabel myLabel = new JLabel("You say:"); JLabel myLabel = new JLabel("You say:"); JTextField myTextField = new JTextField("Hello", 10); JTextField myTextField = new JTextField("Hello", 10); myPanel.add(myLabel); myPanel.add(myLabel); myPanel.add(myTextField); myPanel.add(myTextField);

GUI in Java Action Listener Action Listener We need an event handler to get the user input, such as when they click on a command button We need an event handler to get the user input, such as when they click on a command button In java, we use Action Listener for this purpose In java, we use Action Listener for this purpose JButton button1= new JButton("Check"); add(button1); button1.addActionListener(new ActionListener() { button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { myTextField.setText( " Hi " );} myTextField.setText( " Hi " );}});

GUI in Java Layout Management: Layout Management: manage the position of text, I/O on the GUI window manage the position of text, I/O on the GUI window Components in container are managed by layout manager Components in container are managed by layout manager All positioning is relative to the boundary of screen All positioning is relative to the boundary of screen Two kinds of manager: Two kinds of manager: Flow Layout Manager Flow Layout Manager Border Layout Manager Border Layout Manager

Layout Management Flow Layout Manager Flow Layout Manager Adds components in their natural size in a horizontal line Adds components in their natural size in a horizontal line If too many components to fit in one row If too many components to fit in one row Flow Layout will use multiple rows Flow Layout will use multiple rows Components within each row are Components within each row are centered Constructors apply to the Flow Layout Constructors apply to the Flow Layout FlowLayout(int align, int hgap, int vgap) FlowLayout(int align, int hgap, int vgap) align control the position of the components (LEFT, RIGHT, CENTER) align control the position of the components (LEFT, RIGHT, CENTER) hgap is the horizontal gap, vgap is the vertical gap (defaults to 5 pixels) hgap is the horizontal gap, vgap is the vertical gap (defaults to 5 pixels) Flow Layout apply on the Window containers Flow Layout apply on the Window containers

Layout Management Flow Layout Manager Flow Layout Manager Example1: Example1: setLayout(new FlowLayout(FlowLayout.LEFT)); add(new Button("Button 1")); add(new Button("2")); add(new Button("Long-Named Button 3")); Example2: Example2: setLayout(new FlowLayout(FlowLayout.RIGHT,30,0)); add(new Button("Long-Named Button 1")); add(new Button("Button 2")); add(new Button( “ 3"));

Layout Management Border Layout Manager Border Layout Manager This layout has five areas This layout has five areas North, South, East, West and Center North, South, East, West and Center When you enlarge a Container, then the Center area grabs as much of the newly available space When you enlarge a Container, then the Center area grabs as much of the newly available space Constructors apply to the Border Layout Constructors apply to the Border Layout BorderLayout(int hgap, int vgap) BorderLayout(int hgap, int vgap) hgap is the horizontal gap, vgap is the vertical gap (defaults to 0 pixels) hgap is the horizontal gap, vgap is the vertical gap (defaults to 0 pixels) Border Layout apply on the Panel containers Border Layout apply on the Panel containers

Layout Management Border Layout Manager Border Layout Manager Example1: Example1: setLayout(new BorderLayout()); setLayout(new BorderLayout()); add("North", new Button("North")); add("North", new Button("North")); add("South", new Button("South")); add("South", new Button("South")); add("Center", new Button("Center")); add("Center", new Button("Center")); add("West", new Button("West")); add("West", new Button("West")); Example2: Example2: setLayout(new BorderLayout(5,10)); setLayout(new BorderLayout(5,10)); add("North", new Button("North")); add("North", new Button("North")); add("South", new Button("South")); add("South", new Button("South")); add("Center", new Button("Center")); add("Center", new Button("Center")); add("West", new Button("West")); add("West", new Button("West"));

Sample Program import java.awt.*; import java.awt.event.*; import javax.swing.*; class Test { public static final void main(String args[]){ MyFrame frame = new MyFrame(); MyFrame frame = new MyFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show();} frame.show();}}

Sample Program (Cont ’ ) class MyFrame extends JFrame {public MyFrame() { setTitle("Sample Program"); setSize(WIDTH,HEIGHT); Container contentPane = getContentPane(); JPanel myPanel1 = new JPanel(); JPanel myPanel2 = new JPanel(); JLabel myLabel1 = new JLabel("You say:"); myTextField = new JTextField("", 10); JLabel myLabel2 = new JLabel("You said:"); myLabel3 = new JLabel(""); myPanel1.add(myLabel1);myPanel1.add(myTextField);myPanel2.add(myLabel2);myPanel2.add(myLabel3);

Sample Program (Cont ’ ) JButton buttonCheck= new JButton("Check"); myPanel1.add(buttonCheck); buttonCheck.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String a = myTextField.getText(); String a = myTextField.getText(); myLabel3.setText(a);}}); myLabel3.setText(a);}});contentPane.add(myPanel1,BorderLayout.NORTH);contentPane.add(myPanel2,BorderLayout.SOUTH);} public static final int WIDTH = 250; public static final int HEIGHT = 140; public static JTextField myTextField; public static JLabel myLabel3; }