1 Attachment(s)
Applet not appearing as I believe it should
The age textfield and done button dont appear, my HTML height and width are both 800.
Can anybody see any errors I have put in the code to make it appear like this?
The code:
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class agegreeting extends Applet implements ActionListener {
String greeting;
TextField nameField;
TextField ageField;
public void init () {
setLayout (new BorderLayout());
Panel bottomPanel = new Panel();
Label nameLabel = new Label ("Name: ");
nameField = new TextField (15);
Label ageLabel = new Label ("Age: ");
ageField = new TextField (2);
Button doneButton = new Button ("Press when done");
bottomPanel.add (nameLabel);
bottomPanel.add (nameField);
bottomPanel.add (ageLabel);
bottomPanel.add (ageField);
bottomPanel.add (doneButton);
doneButton.addActionListener (this);
add (bottomPanel, BorderLayout.SOUTH);
greeting = "";
}
public void actionPerformed (ActionEvent click) {
String name = nameField.getText ();
int age = new Integer (ageField.getText().trim()).intValue();
if (age < 2) {
greeting = "Hello little baby!";
}
else if (age < 10) {
greeting = "Hey " + name +", whats up?";
}
else if (age < 16) {
greeting = "Hello " + name + " hows school going?";
}
else {
greeting = "Nice to see you again " + name;
}
}
public void paint (Graphics g) {
g.drawOval (40,40, 120,150); // Head
g.drawOval (57,75, 30,20); // Left eye
g.drawOval (110,75, 30,20); // Right eye
g.fillOval (68,81, 10,10); // pupil (left)
g.fillOval (121,81, 10,10); // pupil (right)
g.drawOval (85,100, 30,30); // Nose
g.fillArc (60,125,80,40, 180,180); // Mouth
g.drawOval (25,92, 15,30); // Left ear
g.drawOval (160,92, 15,30); // Right ear
g.drawString (greeting, 190,150);
}
}
How I see it:
Attachment 4028
Re: Applet not appearing as I believe it should
Moved from New to Java
db
Re: Applet not appearing as I believe it should
1. Why, in this day and age, are you using java.awt.Applet which has been superseded by javax.swing.JApplet more than 10 years ago?
2. Every painting method override should invoke the super implementation. Go through this Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
3. After changing the value of greeting you need to repaint()
db
Re: Applet not appearing as I believe it should
I'm completely new to java and I just followed what the guide that I found told me to do.
I'll go through that lesson tonight.
Thanks, may have been 10 years behind if it weren't for the help!