I am trying to run a very simple Java applet program. When I used AWT.Label
component, I can see the component through appletviewer. The code I am using is:
import java.applet.Applet;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.*;
public class AppletTest extends Applet
{
Label user = new Label("Username", Label.LEFT);
public void paint(Graphics g)
{
g.drawString("Registration Form", 195, 10);
}
public void init()
{
add(user);
}
}
When I use swing.JLabel
component, I cannot see any component on the screen except for Registration form printed on top:
import java.applet.Applet;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.*;
public class AppletTest extends JApplet
{
JLabel user = new JLabel("Username", JLabel.LEFT);
public void paint(Graphics g)
{
g.drawString("Registration Form", 195, 10);
}
public void init()
{
add(user);
}
}
What is the issue?
Because paint
is a method in JApplet
, when you override it, you need to call super.paint(g)
so that the parent component can still do it's painting. Your paint
method should look something more like this:
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawString("Registration Form", 195, 10);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.