A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/javaexamples/applet_swing.htm below:

How to use swing applet in Java?

How to use swing applet in Java? Problem Description

How to use swing applet in Java?

Solution

Following example demonstrates how to go use Swing Applet in JAVA by implementing ActionListener & by creating JLabels.

import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class SApplet extends Applet implements ActionListener {
   TextField input,output;
   Label label1,label2;
   Button b1;
   JLabel lbl;
   int num, sum = 0;
   
   public void init() {
      label1 = new Label("please enter number : ");
      add(label1);
      label1.setBackground(Color.yellow);
      label1.setForeground(Color.magenta);
      input = new TextField(5);
      
      add(input);
      label2 = new Label("Sum : ");
      
      add(label2);
      label2.setBackground(Color.yellow);
      label2.setForeground(Color.magenta);
      output = new TextField(20);
      
      add(output);
      b1 = new Button("Add");
      
      add(b1);
      b1.addActionListener(this);
      lbl = new JLabel("Swing Applet Example. ");
      add(lbl);
      setBackground(Color.yellow);
   } 
   public void actionPerformed(ActionEvent ae ){
      try {
         num = Integer.parseInt(input.getText());
         sum = sum+num;
         input.setText("");
         output.setText(Integer.toString(sum));
         lbl.setForeground(Color.blue);
         lbl.setText("Output of the second Text Box : " + output.getText());
      } catch(NumberFormatException e) {
         lbl.setForeground(Color.red);
         lbl.setText("Invalid Entry!");
      }
   }  
}
Result

The above code sample will produce the following result in a java enabled web browser.

View in Browser. 

java_applets.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4