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

Explain the lifecycle of Servlet with block diagram.

 The lifecycle of a Servlet can be divided into several stages. Here's a block diagram that illustrates the different stages: Servlet API: The Servlet API provides a standard set of interfaces and classes for creating and interacting with Servlets. It is typically included in the web application's classpath as a JAR file. Servlet Container: The Servlet Container is a web server or application server that implements the Servlet API. It provides a runtime environment for executing Servlets and manages their lifecycle. Servlet Class: The Servlet Class is the Java class that implements the javax.servlet.Servlet interface. It contains the logic for processing HTTP requests and generating HTTP responses. init(): The init() method is called once when the Servlet is first loaded by the Servlet Container. It is used for initialization tasks, such as setting up database connections, loading configuration settings, or initializing other resources that will be used by the Servlet. service(...

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 a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space.

 To solve this problem, we can follow these steps: Open the input file for reading and the output file for writing. Read the input file line by line. For each line, replace every sequence of consecutive blank spaces with a single space using the std::regex_replace function from the regex library. Write the modified line to the output file. Here is the code: #include <iostream> #include <fstream> #include <regex> using namespace std; int main() {     // Open the input file for reading and the output file for writing     ifstream infile("input.txt");     ofstream outfile("output.txt");     if (!infile) {         cerr << "Error: Could not open input file.\n";         return 1;     }     if (!outfile) {         cerr << "Error: Could not open output file.\n";         return 1;     }     string lin...