资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java实现监听的代码,java中的监听

在Java中,实现一个按键监听器的关键代码???

JButton b = new JButton("ok");

创新互联建站主营宁国网站建设的网络公司,主营网站建设方案,成都App定制开发,宁国h5成都微信小程序搭建,宁国网站营销推广欢迎宁国等地区企业咨询

b.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

// TODO Auto-generated method stub

}

});

请教java中的按钮监听事件代码?

把下面两个类去了,直接用匿名内部类来实现...

b1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

System.out.print(jt.getText());

}

});

b.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {

jt.setText("");

DoubleColorBall dbcb = new DoubleColorBall();

try {

// 根据文本框里输入的数字,调用DoubleColorBall中的方法生成彩票

String[] num = dbcb.getNumber(Integer

.parseInt(jf.getText()));

for (int i = 0; i num.length; i++) {

jt.append(num[i] + "\n");

}

} catch (Exception e) {

e.printStackTrace();

jt.setText("请输入正整数数字");

}

}

});

Java 代码,我想从键盘上实现监听,不知道哪里错了,按上下左右箭头实现重新绘图。

”仅有一个焦点组件能够接收KeyEvent。要使一个组件成为焦点组件,需要将属性isFocusable设置为true。“

-----------------------------------------

Ps:小细节,上下弄反了。y轴向下递增。

-----------------------------------------------------

修改方法之一见注释

package baidu;

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

public class MyGraphics3 extends JFrame{    

public static void main(String[] args){

new MyGraphics3(100,100);

}

public MyGraphics3(int x,int y){

MGraphics3 mg = new MGraphics3(x, y);

mg.setFocusable(true);       //设置为焦点组件!!!

add(mg);

//add(new MGraphics3(x,y));

setVisible(true);

setLocation(500,350);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300,300);

}

}

class MGraphics3 extends JPanel{

private int x,y;

public MGraphics3(int x,int y){

this.x = x;

this.y = y;

addKeyListener(new Monitor3());

}

public void paint(Graphics g){

super.paint(g);

g.setColor(Color.BLUE);

g.fillOval(x,y,10,10);

}

public void move(int a,int b){

x += a;

y += b;

repaint();

}

class Monitor3 extends KeyAdapter{

public void keyPressed(KeyEvent e){

if(e.getKeyCode() == KeyEvent.VK_UP){

move(0,5);

}else if(e.getKeyCode() == KeyEvent.VK_DOWN){

move(0,-5);

}else if(e.getKeyCode() == KeyEvent.VK_RIGHT){

move(5,0);

}else if(e.getKeyCode() == KeyEvent.VK_LEFT){

move(-5,0);

}else{

System.out.println("ERROR!!!");

move(0,0);

}

}

}

}

如何用java实现自动监听处理jsp页面的

一、监听域对象中属性的变更的监听器

域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事件的监听器。  

这三个监听器接口分别是ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。

1.1、attributeAdded 方法

当向被监听对象中增加一个属性时,web容器就调用事件监听器的attributeAdded方法进行响应,这个方法接收一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象

各个域属性监听器中的完整语法定义为:

public void attributeAdded(ServletContextAttributeEvent scae)

public void attributeReplaced(HttpSessionBindingEvent hsbe)

public void attributeRmoved(ServletRequestAttributeEvent srae)

1.2、attributeRemoved 方法

当删除被监听对象中的一个属性时,web容器调用事件监听器的attributeRemoved方法进行响应

各个域属性监听器中的完整语法定义为:

public void attributeRemoved(ServletContextAttributeEvent scae)

public void attributeRemoved (HttpSessionBindingEvent hsbe)

public void attributeRemoved (ServletRequestAttributeEvent srae)

1.3、attributeReplaced 方法

当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的attributeReplaced方法进行响应

各个域属性监听器中的完整语法定义为:

public void attributeReplaced(ServletContextAttributeEvent scae)

public void attributeReplaced (HttpSessionBindingEvent hsbe)

public void attributeReplaced (ServletRequestAttributeEvent srae)

1.4、ServletContextAttributeListener监听器范例:

编写ServletContextAttributeListener监听器监听ServletContext域对象的属性值变化情况,代码如下:

package me.gacl.web.listener;

import java.text.MessageFormat;

import javax.servlet.ServletContextAttributeEvent;

import javax.servlet.ServletContextAttributeListener;

/**

* @ClassName: MyServletContextAttributeListener

* @Description: ServletContext域对象中属性的变更的事件监听器

* @author: 孤傲苍狼

* @date: 2014-9-11 下午10:53:04

*

*/

public class MyServletContextAttributeListener implements

ServletContextAttributeListener {

@Override

public void attributeAdded(ServletContextAttributeEvent scab) {

String str =MessageFormat.format(

"ServletContext域对象中添加了属性:{0},属性值是:{1}"

,scab.getName()

,scab.getValue());

System.out.println(str);

}

@Override

public void attributeRemoved(ServletContextAttributeEvent scab) {

String str =MessageFormat.format(

"ServletContext域对象中删除属性:{0},属性值是:{1}"

,scab.getName()

,scab.getValue());

System.out.println(str);

}

@Override

public void attributeReplaced(ServletContextAttributeEvent scab) {

String str =MessageFormat.format(

"ServletContext域对象中替换了属性:{0}的值"

,scab.getName());

System.out.println(str);

}

}

在web.xml文件中注册监听器

listener

descriptionMyServletContextAttributeListener监听器/description

listener-classme.gacl.web.listener.MyServletContextAttributeListener/listener-class

/listener

编写ServletContextAttributeListenerTest.jsp测试页面

%@ page language="java" pageEncoding="UTF-8"%

!DOCTYPE HTML

html

head

titleServletContextAttributeListener监听器测试/title

/head

body

%

//往application域对象中添加属性

application.setAttribute("name", "孤傲苍狼");

//替换application域对象中name属性的值

application.setAttribute("name", "gacl");

//移除application域对象中name属性

application.removeAttribute("name");

%

/body

/html

运行结果如下:

从运行结果中可以看到,ServletContextListener监听器成功监听到了ServletContext域对象(application)中的属性值的变化情况。

java 自定义事件的触发及监听

JAVA事件响应机制

1,先自定义一个事件

public class MyEvent extends java.util.EventObject{

public MyEvent(Object source)

{

super(source);

}

}

2,再自定义一个监听器

public class MyListener implements java.util.EventListener{

//这里是当事件发生后的响应过程

public void EventActivated(MyEvent me)

{

System.out.println("事件已经被触发");

}

}

3,以下这个类为触发事件的事件源

public class MyObject {

private Vector vectorListeners=new Vector();

public synchronized void addMyListener(MyListener ml)

{

vectorListeners.addElement(ml);

}

public synchronized void removeMyListener(MyListener ml)

{

vectorListeners.removeElement(ml);

}

protected void activateMyEvent()

{

Vector tempVector=null;

MyEvent e=new MyEvent(this);

synchronized(this)

{

tempVector=(Vector)vectorListeners.clone();

for(int i=0;itempVector.size();i++)

{

MyListener ml=(MyListener)tempVector.elementAt(i);

ml.EventActivated(e);

}

}

}

//定义一个公用方法用于触发事件

public void test()

{

activateMyEvent();

}

}

4,测试类

public class Test {

public static void main(String[] args)

{

MyObject mo=new MyObject();

//注册该事件

mo.addMyListener(new MyListener());

//触发该事件

mo.test();

}

}

Java按钮监听

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.math.BigDecimal;

import java.util.Scanner;

import javax.swing.JFrame;

import java.awt.Button;

import java.awt.Label;

import java.awt.TextField;

import java.awt.Frame;

import java.awt.Panel;

import java.awt.Color;

import java.awt.*;

public class PanelTest{

public static void main(String args[]) {

/*Scanner sc = new Scanner(System.in); double pi=3.14,s;  double r;  r=sc.nextDouble();    s=pi*r*r; System.out.println("s等于"+s);*/

EventQueue.invokeLater(new Runnable() {

public void run() {

CricleFrame frame = new CricleFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

});

}

}

class CricleFrame extends JFrame{

Panel p = new Panel();

TextField t = new TextField();

Button b = new Button("确定");

Label a = new Label("请在此输入半径");

TextField result = new TextField();

public CricleFrame(){

add(a);

add(t);

add(b);

add(result);

add(p);

setVisible(true);

p.setBackground(Color.black);

a.setBackground(Color.yellow);

t.setBackground(Color.white);

result.setBackground(Color.white);

b.setBackground(Color.cyan);

setSize(300, 300);

setTitle("圆的面积");

a.setBounds(105, 45, 90, 25);

t.setBounds(100, 80, 100, 25);

result.setBounds(100, 180, 100, 25);

b.setBounds(111, 120, 80, 40);

b.addActionListener(new ActionListener() {  //按钮点击事件监听

public void actionPerformed(ActionEvent event) {

Double r=0.0;

try{

r = Double.parseDouble(t.getText());

}catch (Exception e){

System.out.println(e.getMessage());

}

BigDecimal tmp = new BigDecimal(r * r * Math.PI);

Double area = tmp.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); //保留2位小数

result.setText(""+area);

}

});

}

}

在你的基础上改了一下,界面什么的没有改


网页题目:java实现监听的代码,java中的监听
文章地址:http://cdkjz.cn/article/hsjjoh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220