Monday, January 9, 2023

Write a GUI program using components to find sum and difference of two numbers.Use two text fields for giving input and a label for output. The program should display sum if user presses mouse and difference if user release mouse.

  


import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JFrame;

import javax.swing.JLabel;


/**

 *

 * @author sital

 */

import java.awt.*;  

import java.awt.event.*;  

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JTextField;

public class MouseListenerExample extends JFrame implements MouseListener{  

    

    JTextField t1,t2;

    JLabel l;  

    MouseListenerExample(){  

        addMouseListener(this);  

        t1 = new JTextField("");

        t1.setBounds(20,20,100,20);  

        t2 = new JTextField("");                

        t2.setBounds(20,40,100,20);  

       

        add(t1);

        add(t2);

        l=new JLabel();  

        l.setBounds(20,60,100,20);  

        add(l);  

        setSize(300,300);  

        setLayout(null);  

        setVisible(true);  

    }  

    public void mouseClicked(MouseEvent e) {  

        l.setText("Mouse Clicked");  

    }  

    public void mouseEntered(MouseEvent e) {  

        l.setText("Mouse Entered");  

    }  

    public void mouseExited(MouseEvent e) {  

        l.setText("Mouse Exited");  

    }  

    public void mousePressed(MouseEvent e) {  

        int n1 = Integer.parseInt(t1.getText());

        int n2 = Integer.parseInt(t2.getText());

        int sum = n1 + n2;

        l.setText("MPressed" + Integer.toString(sum));  

    }  

    public void mouseReleased(MouseEvent e) {  

        try {

            Thread.sleep(1000);

        } catch (InterruptedException ex) {

            Logger.getLogger(MouseListenerExample.class.getName()).log(Level.SEVERE, null, ex);

        }

        int n1 = Integer.parseInt(t1.getText());

        int n2 = Integer.parseInt(t2.getText());

        int m = n1 - n2;       

        l.setText("MReleased" + Integer.toString(m));  

    }  

public static void main(String[] args) {  

    new MouseListenerExample();  

}  

}  

No comments:

Post a Comment

CORBA Java Tutorial using Netbeans and Java 8.

CORBA-Example A simple CORBA implementation using Java Echo.idl module EchoApp{ interface Echo{ string echoString(); }; }; ...