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...

write a program in C++ to overload '-' operator to find difference of two complex object.

write a program to overload '-' operator to find difference of two complex object /* program in C++ to overload '-' operator to find difference of two complex object */ #include<iostream> using namespace std; class Complex{     public:     float a, b;     complex(): a(0), b(0) {}     complex(float x, float y): a(x), b(y){}     void display(){          cout<<this->a<<"+"<<this->b<<"i"<<endl;     }     friend Complex operator-(const Complex&, const Complex&); }; complex operator-(const Complex& com, const Complex& comp){     float x= com.a - comp.a;     foat y= com.b - comp.b;     return Complex(x,y); } int main(){     Complex a(1,7), b(6,9);     cout<<"A = ";a.display();      cout<<"B = ";b.display();      cout<<"A - B = ";(a-b).display(); ...

what is static data member and static member function? how can we overload a function?

what is static data member and static member function? how can we overload a function? A static data member in C++ is a class variable that is shared among all objects of a class and is defined using the 'static' keyword. It has a single instance for the entire class, and its value is shared by all objects of that class. Unlike non-static class members, a static data member can be accessed without creating an instance of the class. A static member function in C++ is a member function of a class that can be called without creating an instance of the class. Like static data members, a static member function operates on the class as a whole rather than on individual objects. A static member function can only access static data members on other static member functions of the same class. To overload a function in C++, you create multiple functions with the same name but different parameter lists. The correct function to call is determined at compile-time based on the number and type...