Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.

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.
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.
© 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;
Event Handling Events and Listeners Timers and Animation.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Graphical User Interfaces
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.
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.
Combo Box, Check Boxes, and Radio Buttons. Radio Buttons User can click radio buttons, just like other buttons BUT Radio buttons are mutually exclusive.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
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,
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.
GUI programming Graphical user interface-based programming.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
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.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
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.
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.
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
CHAPTER 10 EVENT HANDLING. CHAPTER GOALS To understand the Java event model To install mouse and action listeners To accept mouse and text input To display.
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.
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.
Java Swing One of the most important features of Java is its ability to draw graphics.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
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)
Event Handling CS 21a: Introduction to Computing I First Semester,
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
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.
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.
Modular Event Handling
CompSci 230 S Programming Techniques
Java Applet What is a Java Applet? How is applet compiled?
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Graphical user interface-based programming
PC02 Consolidation %WITTY_QUOTE%. PC02 Consolidation %WITTY_QUOTE%
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names start with J : JTextField, JLabel, JButton Objects are created by calling a class constructor: JLabel xLabel = new JLabel("x = "); JTextField xField = new JTextField(width); JButton moveButton = new JButton("Move",buttonIcon); JButton anotherButton = new JButton(“go”);

use Control panel These components are placed in a control panel, a container for user interface components. JPanel thePanel = new JPanel (); thePanel.add(xLabel); thePanel.add(xField); thePanel.add(moveButton); thePanel.add(anotherButton); This panel can then be placed in a frame, and the frame shown. JFrame theFrame = new JFrame(); theFrame.setcontentPane(thePanel); theFrame.pack(); theFrame.show(); *it is possible to put the panel directly on the applet, but more GUI experience is needed …

public class myFrapp extends Applet { public myFrapp() { JLabel xLabel = new JLabel("x = "); //instantiate components JTextField xField = new JTextField(5); JLabel yLabel = new JLabel(“y = "); JTextField yField = new JTextField(5); JButton anotherButton = new JButton(“Go"); JPanel thePanel = new JPanel (); //place components on panel thePanel.add(xLabel); thePanel.add(xField); thePanel.add(yLabel); thePanel.add(yField); thePanel.add(anotherButton); JFrame theFrame = new JFrame(); //put panel in frame and show theFrame.setContentPane(thePanel); theFrame.pack(); //adjust components to framesize theFrame.show(); }

Let’s execute applet myFrapp so far…….. ( website: myFrapp1.java )

In order to recognize when the button is pressed, we need an event listener which is registered with the button !! public class myFrapp extends Applet{ public myFrapp() { //instantiate components NEW!! //create a listener class NEW!! //register a listener object with button component //place components on panel //put panel in frame and show }

Button clicks create ActionEvent objects A class which is to be used for creating action listener object must implement the ActionListener interface. *also in java.awt.event package The ActionListener interface contains one abstract method: void actionPerformed(ActionEvent event);

So our listener must implement the ActionListener interface. public class myFrapp extends Applet{ public myFrapp() { //instantiate components //create listener class class BtnListener implements ActionListener{ public void actionPerformed(ActionEvent e){ // code task to be done when button is pushed } //register a listener object with button component ActionListener bltn = new BtnListener(); anotherButton.addActionListener(bltn); //place components on panel //put panel in frame and show }

Let’s finish our applet so that when the user presses the button, the values in the textfields are used to reposition a triangle on the applet.

A Triangle class would be helpful here, so assume: public class Triangle{ private double startx, starty; public Triangle(double sx, double sy) { startx = sx; starty = sy; } public void setTri( double sx, double sy){ startx = sx; starty = sy; } public void draw (Graphics2D g) { // code which draws a triangle at position (startx,starty) using // graphics environment g }

//THIS WILL DRAW the first TRIANGLE public class myFrapp extends Applet{ private Triangle tri = new Triangle(50.00,50.00); public myFrapp() { //instantiate components //create a listener class //register a listener object with button component //place components on panel //put panel in frame and show } public void paint (Graphics g) Graphics2D g2 = (Graphics2D) g; tri.draw(g2); } //now we are ready to write that button listener

The button listener class…… class BtnListener implements ActionListener{ public void actionPerformed(ActionEvent e){ double newx = Double.parseDouble(xField.getText()); * double newy = Double.parseDouble(yField.getText()); tri.setTri(newx,newy); repaint(); } //register a listener object with button component ActionListener bltn = new BtnListener(); anotherButton.addActionListener(bltn); * If declared as local variables, must be declared as final………

Check that this runs correctly………. (website: myFrapp.java)

We now know all we need to write a frame application.. Examine the following applet:

public class myFrapp extends Applet { public myFrapp() { JLabel xLabel = new JLabel("x = "); //create gui components final JTextField xField = new JTextField(5); JLabel yLabel = new JLabel(“y = "); final JTextField yField = new JTextField(5); JButton anotherButton = new JButton(“Go"); //set up listener for button class BtnListener implements ActionListener{ public void actionPerformed(ActionEvent e){ int val1 = Integer.parseInt(xField.getText()); * int val2 = Integer.parseInt(yField.getText()); val1 = val1+val2; xField.setText(Integer.toString(val1)); } ActionListener lstn = new BtnListener(); anotherButton.addActionListener(lstn);

// constructor continued JPanel thePanel = new JPanel (); //place components on panel thePanel.add(xLabel); thePanel.add(xField); thePanel.add(yLabel); thePanel.add(xField); thePanel.add(anotherButton); JFrame theFrame = new JFrame(); //put panel in frame and show theFrame.setContentPane(thePanel); theFrame.pack(); //adjust components to framesize theFrame.show(); } //end constructor } //end applet

Check that this runs correctly………. (website: Step1.java) We really didn’t need the applet at all…………..

public class Step1 extends Applet { public class Step1 { //create gui components //set up listener for button //create panel //place components on panel //create frame //put panel in frame and show } public Step1() {public static void main (String [] args) { now we have a frame application …….. This is what we have so far ……..

Check that this runs correctly………. (website: Step2.java) Now let’s provide a larger output area and customize the frame a bit………

Now the output is sent to a text area….. JLabel xLabel = new JLabel("x = "); final JTextField xField = new JTextField(width); JLabel yLabel = new JLabel("y = "); final JTextField yField = new JTextField(width); final JTextArea sumArea = new JTextArea(10,20); JButton anotherButton = new JButton("Go"); //create listener class class BtnListener implements ActionListener{ public void actionPerformed(ActionEvent e){ int val1 = Integer.parseInt(xField.getText()); int val2 = Integer.parseInt(yField.getText()); val1 = val1 + val2; sumArea.append("The result is: " + val1); }

// put components on panel and set panel color to red //add components to panel JPanel thePanel = new JPanel (); thePanel.setBackground(Color.red); thePanel.add(xLabel); thePanel.add(xField); thePanel.add(yLabel); thePanel.add(yField); thePanel.add(anotherButton); thePanel.add(sumArea);

and program will now exit when frame is closed, Size the frame rather than pack, Set a background color for the frame (frame is UNDER the panel) Place the frame is a particular location JFrame theFrame = new JFrame(); theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Dimension dim = new Dimension(200,300); theFrame.setSize(dim); theFrame.setBackground(Color.cyan);//this is UNDER the panel theFrame.setLocation(0,0); theFrame.setContentPane(thePanel); theFrame.show();

Check that this runs correctly………. (website: Step3.java) Try packing the frame and see what happens……….