Skip to main content

Differentiate swing and AWT. Write a program using components to add two numbers. Use text fields for inputs and output. Your program should display the result when the user presses a button.

 Swing and AWT are both user interface (UI) toolkits for building graphical user interfaces (GUIs) in Java, but there are some key differences between them.

  1. Look and feel: AWT uses the native UI widgets of the underlying platform, whereas Swing uses its own set of UI widgets that are implemented entirely in Java. This means that Swing applications have a consistent look and feel across different platforms, while AWT applications may look different on different platforms.
  2. Features: Swing provides a richer set of UI components and features than AWT, including support for more advanced UI widgets, such as JTree, JTable, JTabbedPane, and JToolBar. Swing also provides more advanced layout managers and event handling mechanisms than AWT.
  3. Performance: AWT is generally faster than Swing, especially for simple UIs, because it uses the native UI widgets of the underlying platform. However, Swing provides better performance for more complex UIs that require custom graphics or animation.

Here's an example program that uses Swing components to add two numbers entered by the user:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AddNumbers extends JFrame implements ActionListener {
   JTextField num1, num2, result;
   JButton addBtn;
   
   public AddNumbers() {
      // Set up the frame
      setTitle("Add Numbers");
      setSize(300, 150);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      // Set up the UI components
      num1 = new JTextField(10);
      num2 = new JTextField(10);
      result = new JTextField(10);
      result.setEditable(false);
      addBtn = new JButton("Add");
      addBtn.addActionListener(this);
      
      // Set up the layout
      JPanel inputPanel = new JPanel(new GridLayout(2, 2));
      inputPanel.add(new JLabel("Number 1:"));
      inputPanel.add(num1);
      inputPanel.add(new JLabel("Number 2:"));
      inputPanel.add(num2);
      JPanel outputPanel = new JPanel(new FlowLayout());
      outputPanel.add(new JLabel("Result:"));
      outputPanel.add(result);
      JPanel btnPanel = new JPanel(new FlowLayout());
      btnPanel.add(addBtn);
      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(inputPanel, BorderLayout.NORTH);
      mainPanel.add(outputPanel, BorderLayout.CENTER);
      mainPanel.add(btnPanel, BorderLayout.SOUTH);
      setContentPane(mainPanel);
   }
   
   public void actionPerformed(ActionEvent e) {
      // Get the numbers entered by the user
      double n1 = Double.parseDouble(num1.getText());
      double n2 = Double.parseDouble(num2.getText());
      
      // Add the numbers and display the result
      double sum = n1 + n2;
      result.setText(Double.toString(sum));
   }
   
   public static void main(String[] args) {
      AddNumbers frame = new AddNumbers();
      frame.setVisible(true);
   }
}

In this program, we create a JFrame and add three text fields and a button to it using Swing components. When the user clicks the "Add" button, the actionPerformed() method is called, which gets the numbers entered by the user, adds them together, and displays the result in the third text field. The program demonstrates the use of text fields, buttons, and event handling in Swing.

Comments

Popular posts from this blog

Write a program using the algorithm count() to count how many elements in a container have a specified value.

 Here's an example program using the count() algorithm to count the number of occurrences of a specific value in a vector container: #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() {     vector<int> numbers = { 2, 5, 3, 7, 8, 5, 1, 5, 4 };          // count the number of occurrences of the value 5 in the vector     int count = count(numbers.begin(), numbers.end(), 5);          cout << "The number of occurrences of 5 in the vector is: " << count << endl;          return 0; } Output: The number of occurrences of 5 in the vector is: 3 Explanation: The program starts by creating a vector named numbers that contains several integer values. The count() algorithm is used to count the number of occurrences of the value 5 in the numbers vector. The function takes three arguments: the beginning and end iterators of...

What are Stub and Skeleton in Distributed Application? Explain its function with block diagram.

 Stub and Skeleton are two important components of distributed applications. A distributed application is a software system that runs on multiple computers connected through a network. It allows users to access resources and services on different computers as if they were on a local computer. In a distributed application, a client program on one computer sends a request to a server program on another computer. The server program processes the request and sends a response back to the client program. Stub and Skeleton help to facilitate this communication between the client and server programs. A Stub is a client-side proxy that represents the remote object on the client machine. It acts as a gateway for the client to communicate with the server. When a client invokes a method on the Stub, it marshals the arguments and sends them to the server over the network. The Stub then waits for the server to send a response. When the response is received, the Stub unmarshals the data and retur...

Write short notes on 1. polymorphism 2. applet vs Application 3. Marshalling and unmarshalling

 1. Polymorphism: Polymorphism is a concept in object-oriented programming (OOP) that refers to the ability of an object to take on multiple forms. In Java, polymorphism can be achieved through method overloading and method overriding. Method overloading means that multiple methods can have the same name but different parameters. Method overriding means that a subclass can provide a different implementation of a method that is already defined in its superclass. Polymorphism allows for more flexible and modular code design, as objects can be treated as their superclass type, allowing for easier code reuse and maintenance. 2. Applet vs Application: An applet is a small program that runs within a web browser. It is written in Java and can be embedded into a web page using HTML. Applets are designed to be run on any computer with a Java Virtual Machine (JVM) installed, making them platform-independent. In contrast, an application is a standalone program that runs on a computer's operat...