Download presentation
Presentation is loading. Please wait.
Published byElwin Newman Modified over 9 years ago
1
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010
2
What Is an Applet? a special Java class that doesn't run in a DOS window runs in a web browser on client, in a window, on a HTML page Java has security features to keep the applet from acting like a virus on the client machine
3
Creating an Applet need import statements to bring in classes, etc. that Applet will use import java.applet.*; import java.awt.*; put extends Applet after class name has Applet functionality, plus what you add has a public void init() method has no main() method // code below
4
GUI Components components are objects (or like objects) GUI means graphic user interface graphic is the opposite of text you see something other than just plain text interface is what the user works with when using a computer program
5
GUI Components for Applets some "heavyweight" (platform dependent) components in Java Label: writes text on the Applet Button: push button to trigger action TextField: one line input box TextArea: multi-line input box others: scrollbar, etc. (similar to many components in Windows programs)
6
"Heavyweight" vs. Swing earlier Java versions had "heavyweight" platform dependent AWT (abstract windowing toolkit) components different on different operating systems "lightweight" Swing components are platform independent in many ways e.g., look the same on PC or Apple
7
JOptionPane Java Swing class (lightweight) "bean" that includes components including: Label, TextField Buttons Java beans: classes with extra features makes them easier to use with other classes, languages, and on different platforms
8
Additional Components the Java Swing class has additional GUI components (that are NOT platform dependent) JCheckBox: yes or no choices JRadioButton: can only select 1 in a group JComboBox: drop down list that user can type one or more characters into
9
Using GUI Components need to declare the object Label aLabel = new Label("Hello"); Button aButton = new Button("Click"); need to add objects to the Applet, usually in its init() method public void init() { add(aLabel); }
10
GUI Component Methods can use methods to set or change properties on components font on labels (type, style, size) text on a button e.g., OK input box size(s) default content inside an input box make content of input box editable or not
11
GUI Component Methods - 2 can use methods to manipulate components set focus (cursor position) onto a TextField get contents (user input) from a TextField e.g., getting String from JOptionPane
12
Event-Driven Programming event = something that happens mouse click, mouse over, mouse out, button push, key stroke, time of day, etc. event-driven program flow is (partially) controlled by external events rather than just by internal logic
13
"Listener" object that waits for an event and then triggers a method that responds to it associated with another object e.g., button or text component
14
Using a Listener need another import statement at top import java.awt.event.*; the class also needs to "implement" an ActionListener public class Greet extends Applet implements ActionListener
15
Using a Listener - 2 attach a listener method to an object public void init() { add(aButton); // declared above init aButton.addActionListener(this); }
16
Using a Listener - 3 add an actionPerformed method public void actionPerformed(ActionEvent thisEvent) { aLabel.setText("Hi"); } /*will get fatal error if have an ActionListener without actionPerformed method in class*/
17
Adding Interactive Output can add statements to actionPerformed method e.g., labels with text from user inputs may need to force Applet to redraw itself by adding following methods: invalidate(); // marks Applet as out-of-date validate(); // redraws out-of-date Applet
18
Other Interactivity can also add methods to actionPerformed method to remove GUI components remove(aLabel);
19
Controlling Position when add components without a layout manager, Java chooses where to put each item left to right until row is filled, and then starts a new row below previous as space permits, centered in row can subsequently move components to locations identified by coordinates
20
Window Coordinate System 1 st number is x (horizontal) position 2 nd number is y (vertical) position upper left corner is 0, 0 100, 0 is top of window, 100 pixels to right of upper left corner 0, 100 is left side of window, 100 pixels down from upper left corner 100, 100 is 100 pixels over, 100 down
21
Placing Components on Applet use setLocation method with a component myLabel.setLocation(50, 150); // where?
22
Disable and Enable disable makes it impossible to click a button, checkbox, etc. e.g. disable Print button until inputs are entered and calculations are completed enable makes the item functional again enable Print button when data is OK
23
Enabling Components clickButton.setEnabled(false); button previously declared by user will not respond to clicks clickButton.setEnabled(true); default condition, don't have to set to true if didn't previously set it to false
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.