|
A special Java class that doesn't run in a dos window
|
tarix | 07.11.2018 | ölçüsü | 209,5 Kb. | | #78545 |
|
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
need import statements to bring in classes, etc. that Applet will use 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 a public void init() method - has no main() method // code below
components are objects (or like objects) 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
some "heavyweight" (platform dependent) components in Java 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)
earlier Java versions had "heavyweight" platform dependent AWT (abstract windowing toolkit) components 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
Java Swing class (lightweight) "bean" that includes components including: Java Swing class (lightweight) "bean" that includes components including: Java beans: classes with extra features
the Java Swing class has additional GUI components (that are NOT platform dependent) 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
need to declare the object 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);
- }
can use methods to set or change properties on components 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
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
event = something that happens 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
object that waits for an event and then triggers a method that responds to it 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
need another import statement at top need another import statement at top the class also needs to "implement" an ActionListener - public class Greet extends Applet implements ActionListener
attach a listener method to an object - public void init()
- {
- add(aButton); // declared above init
- aButton.addActionListener(this);
- }
add an actionPerformed method 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*/
can add statements to actionPerformed method 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
-
can also add methods to actionPerformed method to remove GUI components can also add methods to actionPerformed method to remove GUI components
when add components without a layout manager, Java chooses where to put each item 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
1st number is x (horizontal) position 1st number is x (horizontal) position 2nd 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
use setLocation method with a component use setLocation method with a component - myLabel.setLocation(50, 150); // where?
disable makes it impossible to click a button, checkbox, etc. e.g. disable makes it impossible to click a button, checkbox, etc. e.g. enable makes the item functional again - enable Print button when data is OK
clickButton.setEnabled(false); 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
Dostları ilə paylaş: |
|
|