1.
Mouse Handling Events Demo
import [Link].*;
import [Link].*;
public class MouseEventsDemo extends Frame implements MouseListener {
String msg = "";
public MouseEventsDemo() {
setSize(400, 300);
setTitle("Mouse Events Demo");
setVisible(true);
addMouseListener(this);
}
public void paint(Graphics g) {
[Link](msg, 100, 150);
}
public void mouseClicked(MouseEvent e) {
msg = "Mouse Clicked";
repaint();
}
public void mouseEntered(MouseEvent e) {
msg = "Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent e) {
msg = "Mouse Exited";
repaint();
}
public void mousePressed(MouseEvent e) {
msg = "Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent e) {
msg = "Mouse Released";
repaint();
}
public static void main(String[] args) {
new MouseEventsDemo();
}
}
2. Move Shapes with Arrow Keys
import [Link].*;
import [Link].*;
public class ShapeMover extends Frame implements KeyListener {
int x = 100, y = 100;
public ShapeMover() {
setSize(400, 400);
setTitle("Shape Mover");
setVisible(true);
addKeyListener(this);
}
public void paint(Graphics g) {
[Link](0, 0, 400, 400);
[Link]([Link]);
[Link](x, y, 50, 50);
}
public void keyPressed(KeyEvent e) {
int key = [Link]();
if (key == KeyEvent.VK_LEFT) x -= 10;
else if (key == KeyEvent.VK_RIGHT) x += 10;
else if (key == KeyEvent.VK_UP) y -= 10;
else if (key == KeyEvent.VK_DOWN) y += 10;
repaint();
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new ShapeMover();
}
}
3. Personal Info Applet
import [Link].*;
import [Link].*;
public class PersonalInfoApplet extends Applet {
public void paint(Graphics g) {
[Link]("Name: John Doe", 20, 20);
[Link]("Age: 20", 20, 40);
[Link]("College: Benedict College", 20, 60);
}
}
// <applet code="[Link]" width="300" height="100"></applet>
4. Frame Showing Info on Button Check
import [Link].*;
import [Link].*;
public class PersonalInfoFrame extends Frame implements ActionListener {
Button showBtn;
Label infoLabel;
public PersonalInfoFrame() {
setLayout(new FlowLayout());
showBtn = new Button("Check Info");
infoLabel = new Label("");
add(showBtn);
add(infoLabel);
[Link](this);
setSize(300, 150);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
[Link]("Name: John | Age: 20 | Course: BCA");
}
public static void main(String[] args) {
new PersonalInfoFrame();
}
}
5. Frame with Father and Mother Buttons
import [Link].*;
import [Link].*;
public class ParentInfoFrame extends Frame implements ActionListener {
Button fatherBtn, motherBtn;
Label infoLabel;
public ParentInfoFrame() {
setLayout(new FlowLayout());
fatherBtn = new Button("Father");
motherBtn = new Button("Mother");
infoLabel = new Label("");
add(fatherBtn);
add(motherBtn);
add(infoLabel);
[Link](this);
[Link](this);
setSize(400, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if ([Link]() == fatherBtn) {
[Link]("Father: Mr. Robert | Age: 45 | Engineer");
} else if ([Link]() == motherBtn) {
[Link]("Mother: Mrs. Anna | Age: 42 | Teacher");
}
}
public static void main(String[] args) {
new ParentInfoFrame();
}
}
6. Font Message Display Window
import [Link].*;
public class FontMessageWindow extends Frame {
public FontMessageWindow() {
setTitle("Welcome Window");
setSize(400, 200);
setVisible(true);
}
public void paint(Graphics g) {
Font customFont = new Font("Serif", [Link] | [Link], 24);
[Link](customFont);
[Link]([Link]);
[Link]("Welcome to Benedict College", 50, 100);
}
public static void main(String[] args) {
new FontMessageWindow();
}
}