JApplet class in Applet

JApplet class in Applet

As we prefer Swing to AWT. Now we can use JApplet that can have all the controls of swing. The JApplet class extends the Applet class.

Example of EventHandling in JApplet:

  1. import java.applet.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. public class EventJApplet extends JApplet implements ActionListener{
  5. JButton b;
  6. JTextField tf;
  7. public void init(){
  8. tf=new JTextField();
  9. tf.setBounds(30,40,150,20);
  10. b=new JButton(“Click”);
  11. b.setBounds(80,150,70,40);
  12. add(b);add(tf);
  13. b.addActionListener(this);
  14. setLayout(null);
  15. }
  16. public void actionPerformed(ActionEvent e){
  17. tf.setText(“Welcome”);
  18. }
  19. }
In the above example, we have created all the controls in init() method because it is invoked only once.

myapplet.html

  1. <html>
  2. <body>
  3. <applet code=“EventJApplet.class” width=“300” height=“300”>
  4. </applet>
  5. </body>
  6. </html>
download this example.
Next TopicPainting-in-applet

 

java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

JApplet class in Applet As we prefer Swing to AWT. Now we can use JApplet that can have all the controls of swing. The JApplet class extends the Applet class. Example of EventHandling in JApplet: import java.applet.*; import javax.swing.*; import java.awt.event.*; public class EventJApplet extends JApplet implements ActionListener{ JButton b; JTextField tf; public void init(){ tf=new JTextField(); tf.setBounds(30,40,150,20); b=new JButton(“Click”); b.setBounds(80,150,70,40); add(b);add(tf); b.addActionListener(this); setLayout(null); } public void actionPerformed(ActionEvent e){ tf.setText(“Welcome”); } } In […]

Tags:

....
...