1 IM103 week 10 Enhancing the user interface By the end of lecture you should be able to:  distinguish between heavyweight and lightweight components;

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Corresponds with Chapter 12
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
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.
Lecture 19 Graphics User Interfaces (GUIs)
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
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.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
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.
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 GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Objectives of This Session
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
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.
1 Java Swing - Lecture 2 Components and Containment Boriana Koleva
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
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.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
 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.
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.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUIs Graphical User Interfaces. Everything coming together Known: – Inheritance – Interfaces – Abstract classes – Polymorphism – Exceptions New: – Events.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
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.
Basics of GUI Programming Chapter 11 and Chapter 22.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
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)
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
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:
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
A First Look at GUI Applications
Java GUI.
A Quick Java Swing Tutorial
Timer class and inner classes
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Chapter 12 GUI Basics.
Presentation transcript:

1 IM103 week 10 Enhancing the user interface By the end of lecture you should be able to:  distinguish between heavyweight and lightweight components;  explain the relationship between the AWT and Swing packages;  use a variety of swing components;  add borders and icons to components;  create message boxes and input boxes;  create your own colours and fonts;  use the CardLayout manager;  explain some of the principles behind the creation of good user interfaces.

2 the Swing classes build on the AWT classes to provide enhanced functionality and appearance. The Swing version of the Hostel GUI The Java Swing package

3 with the AWT classes, any component that we create is associated with the corresponding component in the native operating system; for example, when we use methods of an AWT Button object, this communicates with a corresponding object - its peer - provided by the operating system. consequently the button will look like, for example, a Windows button or a Mac button; components that rely on the native operating system are called heavyweight components as they make extensive use of the system's resources; with Swing, most of the components are written in Java and all the code is provided as part of the Swing package; components that are written in Java are called lightweight components. the Swing package, along with the AWT, is part of the Java Foundation Classes (JFC). It must be noted that any JVM needs to be packaged with the all the classes of the JFC in order for Swing programs to work; not all browsers are packaged with Swing. when writing Swing programs you must include the import statement: import javax.swing.*; The Java Swing package … continued

4 javax.swing Java.awt Object Component Container WindowPanel FrameDialog Applet JDialogJWindowJFrameJAppletJComponent Java.lang

5  you can create attractive borders on all the basic components;  you can easily combine text and graphics on components (using the Icon interface);  you can add scrolling facilities to any component (using the JScrollPane class);  you can easily provide message boxes and input boxes (using the JOptionPane class);  the swing equivalent to the AWT Applet class - the JApplet class - has a method that allows you to add menus to objects of this class. Some advantages of using Swing

6 The JFrame class Program 19.1 : running the HostelSwing class import javax.swing.*; import java.awt.*; public class RunHostelSwing { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.setTitle("Student Hostel"); HostelSwing property = new HostelSwing(20); frame.setSize(500,440); frame.getContentPane().setBackground(new Color(0,190,170)); frame.getContentPane().add(property); frame.setVisible(true); }

7 Analysis of Program 19.1 Setting the default close operation: frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); the three possible values are: JFrame.DO_NOTHING_ON_CLOSE JFrame.HIDE_ON_CLOSE (the default); JFrame.DISPOSE_ON_CLOSE.

8 Analysis of Program 19.1 … continued Communicating with the JFrame A JFrame allows more than one container to be added to it: a content pane; an optional JMenuBar; a transparent glass pane that is positioned above the other components and allows layering. frame.getContentPane().setBackground(new Color(0,190,170)); frame.getContentPane().add(property);

9 all the basic components are derived from JComponen t, and inherit its setBorder method, which can be used to add the border of your choice. in the javax.swing.border package is a Border interface; in this package there are eight standard border classes that implement this interface:  BevelBorder ;  SoftBevelBorder ;  LineBorder ;  EtchedBorder ;  TitledBorder ;  MatteBorder ;  CompoundBorder ;  EmptyBorder. The Border interface

10 The Border interface … continued

Creating borders BevelBorder bevel = new BevelBorder(BevelBorder.RAISED); SoftBevelBorder soft = new SoftBevelBorder(SoftBevelBorder.LOWERED); LineBorder line = new LineBorder(Color.black, 2); EtchedBorder etched = new EtchedBorder(EtchedBorder.RAISED); TitledBorder titled = new TitledBorder("Title goes here"); MatteBorder matte = new MatteBorder(new ImageIcon("Circle.gif")); Adding a border myLabel.setBorder(bevel);

Combining text and graphics with the Icon interface The SquareIcon class import javax.swing.*; import java.awt.*; class SquareIcon implements Icon { private int size; public SquareIcon(int sizeIn) { size = sizeIn; } // all the following methods are required by the Icon interface public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.red); g.fillRect(x, y, size, size); } public int getIconWidth() { return size; } public int getIconHeight() { return size; }

import javax.swing.*; import java.awt.*; import java.awt.event.*; class IconDemo extends JPanel implements ActionListener // adds an icon and text to a component { private JButton button = new JButton(); private SquareIcon icon = new SquareIcon(30); public IconDemo() { button.setMargin(new Insets(0,0,0,0)); button.setIcon(icon); // adds the icon to the button button.setText(" Quit "); // adds the text to the button add(button); button.addActionListener(this); } public void actionPerformed(ActionEvent e) { System.exit(0); } Using the SquareIcon class: the IconDemo class

14 import javax.swing.*; public class RunIconTest { public static void main(String[] args) { JFrame frame = new JFrame(); IconDemo icDemo = new IconDemo(); frame.getContentPane().add(icDemo); frame.setSize(180,100); frame.setVisible(true); } Program 19.2: Running the IconDemo class

15 Result of running Program 19.2

16 allows you to create an icon from a file in.gif or.jpg format; for example, we took a file called "Quit.gif", which consists of a red circle on a white background, and then, in the IconDemo class, replaced this line private SquareIcon icon = new SquareIcon(30); with this one: private ImageIcon icon = new ImageIcon("Quit.gif"); The IconImage class

17 Result of running the new version of program 19.2

18 Creating message boxes and input boxes with the JOptionPane class The JOptionPane Dialogue Types OptionDisplays a list of buttons to enable the user to choose an option. InputAllows the user to enter data via a text field or list. Also provides an "OK" and "Cancel" buttons. MessageDisplays a message and an "OK" button; ConfirmAsks the user a question and provides "Yes" and "No" buttons for the answer.

19 An Option Dialogue A Message Dialogue (information) A Message Dialogue (error) An Input Dialogue A Confirm Dialogue Examples of different types of dialogue box

20 Message types available in the JOptionPane class JOptionPane.PLAIN _ MESSAGE No icon is displayed JOptionPane.INFORMATION _ MESSAGE An information icon is displayed JOptionPane.ERROR _ MESSAGE An error icon is displayed JOptionPane.QUESTION _ MESSAGE A question icon is displayed JOptionPane.WARNING _ MESSAGE A warning icon is displayed

21 Possible return values from the showConfirmDialog method JOptionPane.YES _ OPTION The "Yes" button was pressed JOptionPane.NO _ OPTION The "No" button was pressed JOptionPane.CANCEL _ OPTION The "Cancel" button was pressed JOptionPane.OK _ OPTION The "OK" button was pressed JOptionPane.CLOSED _ OPTION The dialogue was closed by clicking on the cross-hairs

22 Creating new colours : program 19.4 import java.awt.*; import java.swing.*; public class ColourTester { public static void main(String[] args) { Color magenta = new Color(255,0,255); Color cyan = new Color(0,255,255); Color black = new Color(0,0,0); Color purple = new Color(210,100,210); Color orange = new Color(250,150,0); Color brown = new Color(200,150,150); …………………………………………..

…………………………………………………… JFrame frame = new JFrame(); frame.setSize(150,160); frame.setVisible(true); Graphics g = frame.getGraphics(); g.setColor(magenta); g.drawString("This is magenta", 10,40); g.setColor(cyan); g.drawString("This is cyan", 10,60); g.setColor(black); g.drawString("This is black", 10,80); g.setColor(purple); g.drawString("This is purple", 10,100); g.setColor(orange); g.drawString("This is orange", 10,120); g.setColor(brown); g.drawString("This is brown", 10,140); } Creating new colours : program 19.4 … continued

24 Output from program 19.4

25 Creating new fonts : program 19.5 import java.awt.*; import javax.swing.*; public class FontTester { public static void main(String[] args) { Font font1 = new Font("SansSerif",Font.PLAIN,16); Font font2 = new Font("Serif",Font.PLAIN,20); Font font3 = new Font("Monospaced",Font.PLAIN,30); Font font4 = new Font("Dialog",Font.BOLD,20); Font font5 = new Font("DialogInput",Font.BOLD,20); Font font6 = new Font("Serif",Font.ITALIC,30); Font font7 = new Font("Serif",Font.ITALIC + Font.BOLD,16); …………………………………………………………………………………

Creating new fonts : program 19.5 … continued ……………………………………………………………………… JFrame frame = new JFrame(); frame.setSize(260,260); frame.setVisible(true); Graphics g = frame.getGraphics(); g.setFont(font1); g.drawString("This is font1", 10,47); g.setFont(font2); g.drawString("This is font2", 10,75); g.setFont(font3); g.drawString("This is font3", 10,105); g.setFont(font4); g.drawString("This is font4", 10,135); g.setFont(font5); g.drawString("This is font5", 10,165); g.setFont(font6); g.drawString("This is font6", 10,200); g.setFont(font7); g.drawString("This is font7", 10,230); }

27 Output from program 19.5

28 the components in a container that implements a CardLayout policy are arranged like cards in a pack; the CardLayout class provides five methods that allow you to display each "card" in turn: first; next; previous; last; random. this layout policy is useful when you want to break up a screen into different sections, so that the sections can be shown one at a time. The CardLayout manager

Using the CardLayout manager

30 private CardLayout cardLayout = new CardLayout(); Analysis of the Cards class Creating a new CardLayout private JPanel firstCard = new JPanel(); private JPanel secondCard = new JPanel(); private JPanel thirdCard = new JPanel(); Creating the cards centrePanel.setLayout(cardLayout); Setting the layout

31 Analysis of the Cards class … continued Implementing the event handlers public void actionPerformed(ActionEvent e) { if(e.getSource() == nextButton) { cardLayout.next(centrePanel); } else if(e.getSource() == previousButton) { cardLayout.previous(centrePanel); }