Java Oop Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Develop a java program that creates a user interface to perform integer divisions with

possible validations (Divide by Zero, NumberFormatException).

Division.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Division {

public static void main(String[] args) {

Frame frame = new Frame("ActionListener Example");

TextField textField1 = new TextField();

TextField textField2 = new TextField();

TextField textField3 = new TextField();

Button button = new Button("Click Me");

Label label1 = new Label("Textfield 1:");

Label label2 = new Label("Textfield 2:");

Label label3 = new Label("Textfield 3:");

frame.add(label1);

frame.add(textField1);

frame.add(label2);

frame.add(textField2);

frame.add(label3);

frame.add(textField3);

frame.add(button);

frame.setLayout(new FlowLayout());
frame.setSize(300, 300);

frame.setVisible(true);

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

try{

int num1 = Integer.parseInt(textField1.getText());

int num2= Integer.parseInt(textField2.getText());

String s;

s=""+(num1/num2);

textField3.setText(s);

catch(ArithmeticException a)

JOptionPane.showMessageDialog(null,"Divide by zero");

catch(NumberFormatException b)

JOptionPane.showMessageDialog(null,"NumberFormatException");

});

}
Develop a java program to implement mouse events like mouse pressed, mouse released,
and mouse moved by means of adapter classes.

MouseListenerExample.java

import java.awt.*;

import java.awt.event.*;

public class MouseListenerExample extends Frame implements MouseListener{

Label l;

MouseListenerExample(){

addMouseListener(this);

l=new Label();

l.setBounds(20,50,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) {

l.setText("Mouse Pressed");

public void mouseReleased(MouseEvent e) {

l.setText("Mouse Released");

public static void main(String[] args) {

new MouseListenerExample();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy