Java P110
Java P110
Java P110
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.*;
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 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));
}
}
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.*;
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 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);
}
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 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);
}
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 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);
}
Output: