Tutorial: Building an applet
Choice
component.
init()
method as follows:
init()
method in the structure pane. The init()
method code is highlighted in the editor.
try/catch
statement and press Enter to create an extra blank line.
//initialize the applet public void init() { choice1.addItem("English"); choice1.addItem("German"); choice1.addItem("Pig Latin"); choice1.addItem("Swedish"); choice1.addItem("Australian"); try { jbinit(); } catch(Exception e) { e.printStackTrace(); } }
choice
. Use the arrow keys to select addItem(String)
from the pop-up window. Press Enter. You can configure CodeInsight in the IDE Options dialog box (Tools|IDE Options|CodeInsight).
Errors
folder appears in the structure pane as you type in the editor. Open the folder and select the error message to highlight the error in the source code.
Next, you'll hook up the event to the language choice. When you select a language from the drop-down list Choice
component, "Good Evening" appears in the cardLayout
panel in the selected language.
Choice
list events as follows:
choice1
located under upper
in the component tree.
itemStateChanged
event. JBuilder generates the method code and takes you to the source code with the cursor inserted in the method.
void choice1_itemStateChanged(ItemEvent e) { }
void choice1_itemStateChanged(ItemEvent e) { if (choice1.getSelectedItem()== "English") { cardLayout1.show(lower, "panel2"); } else if (choice1.getSelectedItem()== "German") { cardLayout1.show(lower, "panel3"); } else if (choice1.getSelectedItem()== "Pig Latin") { cardLayout1.show(lower, "panel4"); } else if (choice1.getSelectedItem()== "Swedish") { cardLayout1.show(lower, "panel5"); } (choice1.getSelectedItem()== "Australian") { cardLayout1.show(lower, "panel6"); } }
if/else if
template and press Enter. The code is generated:
if () { } else if{ }
GoodEveningApplet.html
in the project pane and selecting Run.The "Good Evening" applet runs in Sun's appletviewer:
If there are any errors, they appear in the message pane at the bottom of the AppBrowser. Select an error message and press F1 for Help. Select the error message to highlight the code in the editor. Sometimes the error may be before or after the highlighted line of code. Fix the errors, save the project, and run the applet again.
Now, let's add a button event for button1
on panel6
. When you push the button, the "Gudday, Mate" text on label6
changes to red.
button1
on panel6
. Change the button's Label
property in the Inspector from button1
to Push Me
. Press Enter. Resize the button until "Push Me" fits in the button.
button1
is pressed.
ActionPerformed
event. JBuilder switches to the editor where the following skeleton code has been added for the ActionPerformed
event just below the if/else if
statements.
void button1_actionPerformed(ActionEvent e) { }Now, you'll enter the code that defines the button event which will change "Gudday, Mate" to red.
void button1_actionPerformed(ActionEvent e) { label6.setForeground(new Color(255,0,0)); }
Your applet should look similar to this: