Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Java Program Key Mouse Adapter

java

Uploaded by

shivanka2516
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Java Program Key Mouse Adapter

java

Uploaded by

shivanka2516
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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