Category: FromSitesTree.com

JavaTextField.java #Programming Code Examples #Java/J2EE/J2ME #Mouse and Keyboard Events

import java.applet.Applet; import java.awt.*; /** Lets the user enter the name of any * good programming language. Or does it? * ********************* public class JavaTextField extends Applet { public void init() { setFont(new Font("Serif", Font.BOLD, 14)); setLayout(new GridLayout(2, 1)); add(new Label("Enter a Good Programming Language", Label.CENTER)); LanguageField langField = new LanguageField(); Font langFont = new …

Continue reading

An applet that permits freehand drawing #Programming Code Examples #Java/J2EE/J2ME #Mouse and Keyboard Events

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** An applet that lets you perform freehand drawing. * **************** public class SimpleWhiteboard extends Applet { protected int lastX=0, lastY=0; public void init() { setBackground(Color.white); setForeground(Color.blue); addMouseListener(new PositionRecorder()); addMouseMotionListener(new LineDrawer()); } protected void record(int x, int y) { lastX = x; lastY = y; } // Record position …

Continue reading

Five buttons arranged by FlowLayout #Programming Code Examples #Java/J2EE/J2ME #Layout Managers

FlowTest.java ************* FlowTest.java Five buttons arranged by FlowLayout By default, FlowLayout arranges components in rows, left to right, and centered. /././././././././././././ import java.applet.Applet; import java.awt.*; /** FlowLayout puts components in rows. * ************************************ public class FlowTest extends Applet { public void init() { for(int i=1; i<6; i++) { add(new Button("Button " + i)); } } …

Continue reading

A TextField that uses key events to correct the spelling of the names of computer languages entered into it #Programming Code Examples #Java/J2EE/J2ME #Mouse and Keyboard Events

import java.awt.*; import java.awt.event.*; /** A spelling-correcting TextField for entering * a language name. * ******************* public class LanguageField extends TextField { private String[] substrings = { "", "J", "Ja", "Jav", "Java" }; public LanguageField() { addKeyListener(new SpellingCorrector()); addActionListener(new WordCompleter()); addFocusListener(new SubliminalAdvertiser()); } // Put caret at end of field. private void setCaret() { setCaretPosition(5); …

Continue reading

Applet that uses processXxx methods to print detailed reports on mouse events. Illustrates low-level alternative to handling events with listeners. #Programming Code Examples #Java/J2EE/J2ME #Mouse and Keyboard Events

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** Prints non-detailed reports of mouse events. * Uses the low-level processXxxEvent methods instead * of the usual event listeners. * ***************** public class MouseReporter extends Applet { public void init() { setBackground(Color.blue); // So you can see applet in page enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK); } public void processMouseEvent(MouseEvent event) …

Continue reading

Tiny applet that uses CircleListener to handle mouse events. #Programming Code Examples #Java/J2EE/J2ME #Mouse and Keyboard Events

import java.applet.Applet; import java.awt.*; /** Draw circles centered where the user clicks. * Uses an external listener. * *********** public class CircleDrawer1 extends Applet { public void init() { setForeground(Color.blue); addMouseListener(new CircleListener()); } } Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10362 Categories:Programming Code Examples, Java/J2EE/J2ME, Mouse and Keyboard EventsTags:Java/J2EE/J2MEMouse …

Continue reading

CircleDrawer3.java Applet that uses a named nested class to handle mouse events and draw circles. Illustrates third approach to event-handling with listeners: using inner classes. #Programming Code Examples #Java/J2EE/J2ME #Mouse and Keyboard Events

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** Draw circles centered where the user clicks. * Uses named inner classes. * ****************** public class CircleDrawer3 extends Applet { public void init() { setForeground(Color.blue); addMouseListener(new CircleListener()); } private class CircleListener extends MouseAdapter { private int radius = 25; public void mousePressed(MouseEvent event) { Graphics g = getGraphics(); …

Continue reading

Applet that uses a anonymous nested class to handle mouse events and draw circles. Variation on third approach to event-handling: using inner classes. #Programming Code Examples #Java/J2EE/J2ME #Mouse and Keyboard Events

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** Draw circles centered where the user clicks. * Uses anonymous inner classes. * ********************** public class CircleDrawer4 extends Applet { public void init() { setForeground(Color.blue); addMouseListener (new MouseAdapter() { private int radius = 25; public void mousePressed(MouseEvent event) { Graphics g = getGraphics(); g.fillOval(event.getX()-radius, event.getY()-radius, 2*radius, 2*radius); } …

Continue reading

Applet handle mouse events #Programming Code Examples #Java/J2EE/J2ME #Mouse and Keyboard Events

%%%%%%%%%%%%%% ClickReporter.java A simple applet that uses the class to handle mouse events %%%%%%%%%%%%%% import java.applet.Applet; import java.awt.*; /** Prints a message saying where the user clicks. * Uses an external listener. * ******************* public class ClickReporter extends Applet { public void init() { setBackground(Color.yellow); addMouseListener(new ClickListener()); } } %%%%%%%%%%%%%% Note: Brought from our old …

Continue reading

A simple applet that uses the ClickListener class #Programming Code Examples #Java/J2EE/J2ME #Mouse and Keyboard Events

ClickReporter.java A simple applet that uses the ClickListener class to handle mouse events. *************** import java.applet.Applet; import java.awt.*; /** Prints a message saying where the user clicks. * Uses an external listener. * ****** public class ClickReporter extends Applet { public void init() { setBackground(Color.yellow); addMouseListener(new ClickListener()); } } Note: Brought from our old site: …

Continue reading