Unformatted text preview:

COMP 14 Introduction to ProgrammingLast TimeExample: LowerUpperCaseBasicsJLabel and JTextFieldUsing ButtonsAdding ButtonsButtons and EventsActionListenerSlide 10Slide 11Slide 12actionPerformedExitHandler.actionPerformedClearHandler.actionPerformedButtonHandler.actionPerformedSlide 17AppletsApplet MethodsConverting Apps to AppletSlide 21Want to Learn More?Writing Web PagesTagsVery Simple Web PageWhat Do The Tags Mean?Color and ImagesIgnores White SpaceAdding White SpaceOther TagsHierarchical StructureListsTablesLinksInserting ImagesWant To Learn More?Applets and the WebSlide 38To doThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian IlieCOMP 14Introduction to ProgrammingAdrian IlieJuly 21, 2005The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie2Last Time•Create a Java window extending JFrame.•Add labels with JLabel•Add Images with ImageIcon•Setting colors and layoutThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie3Example: LowerUpperCaseThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie4Basics•Layout: GridLayout(3,2)•JLabel to output result•JTextField to input sentence•4 buttonsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie5JLabel and JTextField•Very similar. With JTextField we can also type data from outside and access it in the program.JLabel output=new JLabel();JTextField input=new JTextField();…String inputStr=input.getText();output.setText(“OUTPUT RESULT”);The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie6Using Buttons•Create JButton objects•Buttons generate events when clicked•Add event handlers•Inside event handler, code operations to be executed when button is clickedThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie7Adding Buttons•To create a button, we use the JButton class•Add button to the content pane•Change text of the button with the setText method•Enable/disable the button with setEnabled methodJButton toLower = new JButton (“To Lower Case");toLower.setText(“Convert to Lower Case");content.add(toLower);toLower.setEnabled(false);The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie8Buttons and Events•Pressing buttons triggers action events•Setup a listener for the event♦actionPerformed method from ActionListener class♦ActionListener class from the java.awt.event package•something else to importThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie9ActionListener•Special type of class, called interface•Interface - class that contains only the method headings (no method bodies)public interface ActionListener{public void actionPerformed (ActionEvent e);}The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie10ActionListener•In order to handle an event, define a class that will implement ActionListener.•Make the class ButtonHandler an internal class of our main class.•In ButtonHandler, code the method actionPerformedprivate class ButtonHandler implements ActionListenerThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie11ActionListener•Declare a ButtonHandler that will be a data member of the main class.•Instantiate the ButtonHandler (e.g. in the constructor)•Once the JButton object is created, register the action listener:private ButtonHandler toLowerHandler;toLowerHandler=new ButtonHandler();toLower.addActionListener(toLowerHandler);The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie12Example: LowerUpperCase•Declare buttons: toLowerButton, toUpperButton, exitButton, clearButton•Instantiate buttons•Add buttons to the content pane•Implement ActionListener classes that will handle events: ButtonHandler, ExitHandler, ClearHandler•Register action listenersThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie13actionPerformed•Variables we want to access inside the actionPerformed methods, must be declared as member variables of the main class♦not local to constructor•Make input, output, and the buttons member variablesThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie14ExitHandler.actionPerformed•We simply need to exit the systemSystem.exit(0);The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie15ClearHandler.actionPerformed•Clear JTextField input and JLabel outputinput.setText(“”);output.setText(“”);setVisible(true);The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie16ButtonHandler.actionPerformed•How do we know which button we pressed (toLower or toUpper)?public void actionPerformed(ActionEvent e){JButton pressed=(JButton)(e.getSource());if(pressed==toLower)…else if(pressed==toUpper)…}The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie17Example: LowerUpperCase•Finish exampleThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie18Applets•A Java application is a stand-alone program with a main method•A Java applet is a Java program that is intended to be transported over the web and executed using a web browser♦an applet doesn't have a main method♦needs an extra import statement:import java.applet.Applet;The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie19Applet Methods•Several methods from Applet class that are invoked automatically at certain points in an applet's life♦init - executed only once when the applet is initially loaded♦start - called when applet becomes active (when browser loads / returns to the page)♦stop - called when applet becomes inactive (when browser leaves the page)♦paint - automatically executed and is used to draw the applets contentsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie20Converting Apps to Applet•See Ch 13, pgs. 855-858 (745-748)•Extends JApplet instead of JFrame•No main method•No constructor♦put code from constructor in init() method•No setVisible, setTitle, or setSize methodsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie21Example: LowerUpperCase•Convert to AppletThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie22Want to Learn More?•Creating a GUI with Swing•Creating Appletshttp://java.sun.com/docs/books/tutorial/uiswing/http://java.sun.com/docs/books/tutorial/applet/The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie23Writing Web Pages•Web pages are written in a "markup" language called HTML (HyperText Markup Language)•HTML is NOT a programming language.•HTML just tells the computer how to format text and images--it's like using Word, but having to type in what you want things to look like.The UNIVERSITY


View Full Document

UNC-Chapel Hill COMP 14 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?