没有办法在Design中添加,只有手工在源代码中加了!
例如在一个button上加一个鼠标事件:
jButton1.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
jButton1MousePressed(evt);
}
});private void jButton1MousePressed(MouseEvent evt) {
// 添加自己的代码
}
例如在一个button上加一个鼠标事件:
jButton1.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
jButton1MousePressed(evt);
}
});private void jButton1MousePressed(MouseEvent evt) {
// 添加自己的代码
}
解决方案 »
- 求教一个窗口最大化有点灵异的问题
- name怎么传进去
- 一小段代码的疑问(多谢)
- 請問大家是用甚麼CVS的?還是沒有用到這些軟件?
- 请问一下,编写java有那些编译器呀??
- Java连接SQLServer数据库,其中某表有两个字段是datetime类型的,请问如果在java的类中插入一条记录时时,datetiem型的字段该怎么处理?
- JDialog 怎么设置标题栏的图标 还有JFrame怎么让它最小化到系统托盘
- 关于java的问题………………
- 求 arraylist 像sql group by 一样分组合计
- org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.StackOverflowE
- 有关传值的问题
- 请问在java里传递方法的可选参数是如何实现的
java.util.TimerTask;
结合使用
import java.util.TimerTask; public class HelloWorldTask extends TimerTask {
public void run() {
System.out.println("Hello World");
}
}
import java.util.Timer; public class PeriodicHello {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new HelloWorldTask(), 1000, 5000);
}
}
import java.awt.event.*;
import java.applet.*;
import com.borland.jbcl.layout.*;
import com.borland.jbcl.control.*;
import javax.swing.*;public class Applet1 extends Applet implements TimerListener
{
XYLayout xYLayout1 = new XYLayout();
boolean isStandalone = false; TimerBean timer=new TimerBean();
JTextField jTextField1 = new JTextField();
//Construct the applet
public Applet1()
{
} //Initialize the applet
public void init()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception
{
jTextField1.addActionListener(new java.awt.event.ActionListener()
{ public void actionPerformed(ActionEvent e)
{
jTextField1_actionPerformed(e);
}
});
xYLayout1.setWidth(400);
xYLayout1.setHeight(300);
this.setLayout(xYLayout1);
timer.setRunning(true);
timer.addTimerListener(this);
this.add(jTextField1, new XYConstraints(94, 151, 170, -1));
} //Get Applet information
public String getAppletInfo()
{
return "Applet Information";
} //Get parameter info
public String[][] getParameterInfo()
{
return null;
} void jTextField1_actionPerformed(ActionEvent e)
{ }
public void timeElapsed(TimerEvent e)
{
System.out.println(e.getName());
if(jTextField1.getText()!=null)
jTextField1.setText(null);
jTextField1.setText(e.getDate().toGMTString());
}
}
import java.awt.*;
import java.util.*;public class TimerBean implements Runnable, Serializable
{
private int interval = 1000;
private Vector timerListeners = new Vector();
private Thread runner; public int getInterval(){return interval;}
public void setInterval(int i){interval = i;} public boolean isRunning(){return runner !=null;}
public void setRunning(boolean b)
{
if(b&&runner == null)
{
runner = new Thread(this);
runner.start();
}
else if (!b && runner !=null)
{
runner.interrupt();
runner=null;
}
} public synchronized void addTimerListener(TimerListener l)
{
timerListeners.addElement(l);
}
public synchronized void removeTimerListener(TimerListener l)
{
timerListeners.removeElement(l);
} public void fireTimerEvent(TimerEvent evt)
{
Vector currentListeners = null;
synchronized(this)
{
currentListeners = (Vector)timerListeners.clone();
}
for(int i = 0; i<currentListeners.size();i++)
{
TimerListener listener = (TimerListener)currentListeners.elementAt(i);
listener.timeElapsed(evt);
}
} public void run()
{
//TODO: implement this java.lang.Runnable method;
if(interval <=0) return;
try
{
while(!runner.interrupted())
{
Thread.sleep(interval);
fireTimerEvent(new TimerEvent(this));
}
}
catch(InterruptedException e)
{}
}
}
import java.awt.*;
import java.awt.event.*;public class TestMyEvent extends Frame implements MyEventAble {
private Vector al = new Vector();
public void addMyListener(MyListener l) {
al.addElement(l);
}
public void removeMyListener(MyListener l) {
al.removeElement(l);
}
public synchronized void processMyEvent(MyEvent e) {
Iterator i = al.iterator();
while (i.hasNext()) {
MyListener l = (MyListener)i.next();
l.myEventRun(e);
}
}
public static void main(String[] argv) {
TestMyEvent t = new TestMyEvent();
t.addMyListener(new MyListener () {
public void myEventRun(MyEvent e) {
System.out.println("Listener 1 E = " + e.getMyCommand());
System.out.println("Source = " + e.toString());
}
});
t.addMyListener(new MyListener () {
public void myEventRun(MyEvent e) {
System.out.println("Listener 2 E = " + e.getMyCommand());
System.out.println("Source = " + e.toString());
}
});
t.setSize(300, 200);
t.show();
t.processMyEvent(new MyEvent(t, 1000, "TestMyEvent"));
}
}//MyEvent.java
import java.awt.AWTEvent;
import java.awt.Event;public class MyEvent extends AWTEvent {
private String myCommand;
MyEvent(Object source, int id, String command) {
super(source, id);
this.myCommand = command;
} public String getMyCommand() {
return myCommand;
}
}//MyListener.java
import java.util.*;public interface MyListener extends EventListener {
public void myEventRun(MyEvent e);
}//MyEventAble
public interface MyEventAble {
public void addMyListener(MyListener l);
public void removeMyListener(MyListener l);
//public synchronized void processMyEvent(MyEvent e);
}
{
public TimerEvent(Object source)
{
super(source);
now = new Date(); } public Date getDate() { return now;}
public String getName(){return "你好!";}
private Date now;
}
{
public void timeElapsed(TimerEvent evt);
}
java.util.TimerTask这两个类的方法都没看
怎么知道这不是你要什么时候发就什么时候发,这个还可以设周期的
先看看api再问,不然给再多代码也没用,始终不是自己的
你只需要在需要发出事件的地方,调用fireXXX方法。在你自己实现的监听程序中,同样切换到上面的Tab页,在Listen for these events列表框中选中你自己的事件,再切换到Source实现你的处理代码就可以了。
我只是得到当前时间,弄清事件机制,没有别的意思,根本不需要定时器。这是一个例子。
SORRY,我说的不是你,呵呵