Help with creating an applet
I have no idea what I am doing wrong. All my applet is display the Welcome to java programming string I have in my paint method. Right now I just want to get a label put on a JFrame. Can anyone help please?
Code:
import java.awt.*;
import java.applet.*;
import javax.swing.JFrame;
import javax.swing.JApplet;
import javax.swing.JLabel;
import java.awt.BorderLayout;
public class SampleApplet extends JApplet {
public void init() {
}
public static void main(String[] args) {
//... Create an initialize the applet.
JApplet theApplet = new SampleApplet();
theApplet.init(); // Needed if overridden in applet
//theApplet.start(); // Needed if overridden in applet
//... Create a window (JFrame) and make applet the content pane.
JFrame window = new JFrame("Sample Applet and Application");
window.setContentPane(theApplet);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack(); // Arrange the components.
//System.out.println(theApplet.getSize());
window.setVisible(true); // Make the window visible.
JLabel label1 = new JLabel("Test Label");
label1.setBackground(Color.CYAN);
label1.setOpaque(true);
window.add(label1, BorderLayout.SOUTH);
}
public SampleApplet() {
add(new JLabel("This is both an Applet and Application!"));
}
public void paint(Graphics g) {
g.drawString("Welcome to Java!!", 50, 60 );
}
}