Java P110

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

Name : Telange Pranav

Deepak Roll no : 57
Batch : CO5I ‘A ’

Practical No. 10
Aim : Write a program to generate KeyEvent when a key is pressed and display
“Key Pressed” message.
Source Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KEvent extends JFrame implements KeyListener {


Label i;
TextArea t1;
KEvent() {
i = new Label();
t1 = new TextArea();
i.setBounds(20, 50, 100, 20);
t1.setBounds(20, 80, 300, 300);
t1.addKeyListener(this);
add(i);
add(t1);
setSize(300, 300);
setVisible(true);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void keyPressed(KeyEvent ke) {
i.setText("Key Pressed");
}
public static void main(String[] args) {
new KEvent();
}
}

Output:
Aim : Develop a program which will implement special keys such as function
keys and arrow keys
Source Code:
import java.awt.*;
import java.awt.event.*;

public class KEvent implements KeyListener{


private Frame frame;
private Label l1;

public KEvent()
{
frame = new Frame("Key Press Listener");
l1 = new Label("Press a Key");
l1.setFont(new Font("Arial",Font.PLAIN,20));
frame.add(l1);
frame.setSize(300,200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);;
frame.addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
String keyName =
KeyEvent.getKeyText(keycode); l1.setText("Key
Pressed : "+keyName);
}
public void keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
String keyName = KeyEvent.getKeyText(keycode);
l1.setText("Key Pressed : "+keyName);
}
public static void main(String[] args) {
new KEvent();
}
}

Output:
Aim : Develop a program to accept two numbers and display product of two
numbers when user pressed “Multiply” button.
Source Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Product extends JFrame implements ActionListener
{ private JTextField num1field, num2field, resultfield;
JButton mulbtn;
public Product() {
super("Product Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 2));
JLabel num1L = new JLabel("Number
1:"); num1field = new JTextField();
JLabel num2L = new JLabel("Number
2:"); num2field = new JTextField();
JLabel resultl = new JLabel("Result :");
resultfield = new JTextField();
resultfield.setEnabled(false);
mulbtn = new JButton("Multiply");
mulbtn.addActionListener(this);
add(num1L);
add(num1field);
add(num2L);
add(num2field);
add(resultl);
add(resultfield);
add(new JLabel());
add(mulbtn);
setSize(300, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == mulbtn) {
double num1 =
Double.parseDouble(num1field.getText()); double num2
= Double.parseDouble(num2field.getText()); double
result = num1 * num2; resultfield.setEnabled(true);
resultfield.setText(Double.toString(result));
}
}

public static void main(String[] args) {


new Product();
}
}
Output:
Name : Telange Pranav
Deepak Roll no : 57
Batch : CO5I ‘A ’

Practical No. 11
Aim : Debug the following Program code and write the output.
Source Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

/* <APPLET CODE="MouseDemo" WIDTH=300 HEIGHT=200> </APPLET> */

public class MouseDemo extends Applet implements MouseListener {


Label l;

public void init() {


setLayout(null);
l = new Label("Hello Mouse");
l.setBounds(50, 70, 200, 100);
add(l);
addMouseListener(this);
}

public void mousePressed(MouseEvent e) {


l.setText("Mouse Pressed no. of clicks: " + e.getClickCount() + " at position " + e.getX()
+ "," + e.getY());
}

public void mouseReleased(MouseEvent e) {


l.setText("Mouse Released; # of clicks: " + e.getClickCount());
}

public void mouseEntered(MouseEvent e)


{ l.setText("Mouse Entered");
}

public void mouseExited(MouseEvent e) {


l.setText("Mouse exited");
}

public void mouseClicked(MouseEvent e) {


l.setText("Mouse clicked (# of clicks: " + e.getClickCount() + ")");
}
}

Output:
Aim : Write a program to change the background color of Applet when user
performs events using Mouse.
Source Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MouseEventA extends JFrame implements MouseListener {

public MouseEventA() {
setTitle("Mouse Event Background Color");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
getContentPane().setBackground(Color.YELLOW);
}

public void mousePressed(MouseEvent e) {


getContent
public void mouseReleased(MouseEvent e) {
getContentPane().setBackground(Color.BLUE);
System.out.println("Blue");
}
public void mouseEntered(MouseEvent e) {
getContentPane().setBackground(Color.CYAN);
}
public void mouseExited(MouseEvent e) {
getContentPane().setBackground(Color.LIGHT_GRAY);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(MouseEventA::new);
}
}

Output:
Aim : Write a program to count the number of clicks performed by the user in a
Frame window.
Source Code:
import javax.swing.*;
import java.awt.event.*;

public class ClickCounter extends JFrame implements MouseListener {


private int clickCount = 0;
private JLabel label;

public ClickCounter() {
setTitle("Click Counter");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("Number of clicks: 0", SwingConstants.CENTER);
label.setFont(label.getFont().deriveFont(18.0f));
add(label);
addMouseListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
clickCount++;
label.setText("Number of clicks: " + clickCount);
}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}


public void mouseExited(MouseEvent e) {}
public static void main(String[] args) {
SwingUtilities.invokeLater(ClickCounter::new);
}
}

Output:
Aim : Write a program to demonstrate the use of mouseDragged and
mouseMoved method of MouseMotionListener.
Source Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MouseMotionDemo1 extends JFrame implements MouseMotionListener {


private int mouseX = 0;
private int mouseY = 0;
private String eventType =
"";

public MouseMotionDemo1() {
setTitle("Mouse Motion Demo");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
eventType = "Mouse Moved By
Pranav"; repaint();
}
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
eventType = "Mouse
Dragged"; repaint();
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.drawString(eventType + " at (" + mouseX + ", " + mouseY + ")", 50, 50);
}

public static void main(String[] args) {


new MouseMotionDemo1();
}
}

Output:

You might also like