Java Program Key Mouse Adapter

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

// Demonstrate the key event handlers. import java.awt.

*;
import java.awt.event.*;
import java.applet.*;
import java.awt.Graphics;
/*
<applet code="SimpleKey" width=500 height=500>
</applet>
*/

public class SimpleKey extends Applet implements KeyListener {

String msg = " ";


int X = 10, Y = 20; // output coordinates

public void init() { addKeyListener(this);


}

public void keyPressed(KeyEvent ke) { showStatus("Key Down");


}

public void keyReleased(KeyEvent ke) { showStatus("Key Up");


}

public void keyTyped(KeyEvent ke) { msg += ke.getKeyChar();

repaint();
}

// Display keystrokes.
public void paint(Graphics g) {
g.drawString(msg, X, Y);
}
}

//Sample output is shown here:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MouseEvents" width=1000 height=1000>
</applet>
*/
public class MouseEvents extends Applet
implements MouseListener, MouseMotionListener {
String msg = "";
int mouseX = 0, mouseY = 0; // coordinates of mouse
public void init() {
addMouseListener(this);
addMouseMotionListener(this);
}
// Handle mouse clicked.
public void mouseClicked(MouseEvent me) {
// save coordinates
mouseX = 0;
mouseY = 10;
msg = "Mouse clicked.";
repaint();
}
// Handle mouse entered.
public void mouseEntered(MouseEvent me) {
// save coordinates
mouseX = 0;
mouseY = 10;
msg = "Mouse entered.";
repaint();
}
// Handle mouse exited.
public void mouseExited(MouseEvent me) {
// save coordinates
mouseX = 0;
mouseY = 10;
msg = "Mouse exited.";
repaint();
}
// Handle button pressed.
public void mousePressed(MouseEvent me) {
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "Down";
repaint();
}

// Handle button released.


public void mouseReleased(MouseEvent me) {
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "Up";
repaint();
}
// Handle mouse draggeS.
public void mouseDragged(MouseEvent me) {
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "*";
showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
repaint();
}
// Handle mouse moved.
public void mouseMoved(MouseEvent me) {
// show status
showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
}
// Display msg in applfet window at current X,Y location.
public void paint(Graphics g) {
g.drawString(msg, mouseX, mouseY);
}}

import java.awt.*;
import java.applet.*;
import java.util.*;
/*
<applet code="BorderLayoutDemo" width=400 height=200></applet> */

public class BorderLayoutDemo extends Applet {


public void init() {
setLayout(new BorderLayout());

add(new Button("This is across the top."), BorderLayout.NORTH);


add(new Label("The footer message might go here."), BorderLayout.SOUTH);
add(new Button("Right"), BorderLayout.EAST);
add(new Button("Left"), BorderLayout.WEST);

String msg = "The reasonable man adapts " + "himself to the world;\n" +
"the unreasonable one persists in " + "trying to adapt the world to himself.\n" + "Therefore all
progress depends " +
"on the unreasonable man.\n\n" +
" - George Bernard Shaw\n\n";

add(new TextArea(msg), BorderLayout.CENTER);


}}

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