当前位置: 技术问答>java相关
这个程序那里错了?
来源: 互联网 发布时间:2015-02-19
本文导语: 编译通过了,但运行时,只有窗口,没有动作. import java.awt.*; import java.util.*; import java.awt.event.*; import javax.swing.*; class CustomEventPanel extends JPanel implements TimerListener { public CustomEventPanel() { Timer t = new Timer(1000); t.ad...
编译通过了,但运行时,只有窗口,没有动作.
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
class CustomEventPanel extends JPanel
implements TimerListener
{
public CustomEventPanel()
{
Timer t = new Timer(1000);
t.addTimerListener(this);
}
public void timeElapsed(TimerEvent evt)
{
Graphics g = getGraphics();
g.fillRect(0,0,ticks,10);
ticks++;
g.dispose();
}
private int ticks = 0;
}
interface TimerListener extends EventListener
{ public void timeElapsed(TimerEvent evt);
}
class Timer extends Component implements Runnable
{
public Timer(int i)
{
interval = i;
Thread t = new Thread(this);
t.start();
evtq = Toolkit.getDefaultToolkit().getSystemEventQueue();
enableEvents(0);
}
public void addTimerListener(TimerListener l)
{
listener = l;
}
public void run()
{ while (true)
{ try { Thread.sleep(interval);}
catch(InterruptedException e) {}
TimerEvent te=new TimerEvent(this);
evtq.postEvent(te);
}
}
public void processevent(AWTEvent evt)
{
if (evt instanceof TimerEvent)
{
if(listener!=null)
listener.timeElapsed((TimerEvent)evt);
}
else super.processEvent(evt);
}
private int interval;
private TimerListener listener;
private static EventQueue evtq;
}
class TimerEvent extends AWTEvent
{ public TimerEvent(Timer t) { super(t,TIMER_EVENT);}
public static final int TIMER_EVENT=AWTEvent.RESERVED_ID_MAX+5555;
}
class CustomEventFrame extends JFrame
{ public CustomEventFrame()
{setTitle("CustomEventTest");
setSize(300,50);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(new CustomEventPanel());
}
}
public class CustomEventTest
{ public static void main(String[] args)
{JFrame frame =new CustomEventFrame();
frame.show();
}
}
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
class CustomEventPanel extends JPanel
implements TimerListener
{
public CustomEventPanel()
{
Timer t = new Timer(1000);
t.addTimerListener(this);
}
public void timeElapsed(TimerEvent evt)
{
Graphics g = getGraphics();
g.fillRect(0,0,ticks,10);
ticks++;
g.dispose();
}
private int ticks = 0;
}
interface TimerListener extends EventListener
{ public void timeElapsed(TimerEvent evt);
}
class Timer extends Component implements Runnable
{
public Timer(int i)
{
interval = i;
Thread t = new Thread(this);
t.start();
evtq = Toolkit.getDefaultToolkit().getSystemEventQueue();
enableEvents(0);
}
public void addTimerListener(TimerListener l)
{
listener = l;
}
public void run()
{ while (true)
{ try { Thread.sleep(interval);}
catch(InterruptedException e) {}
TimerEvent te=new TimerEvent(this);
evtq.postEvent(te);
}
}
public void processevent(AWTEvent evt)
{
if (evt instanceof TimerEvent)
{
if(listener!=null)
listener.timeElapsed((TimerEvent)evt);
}
else super.processEvent(evt);
}
private int interval;
private TimerListener listener;
private static EventQueue evtq;
}
class TimerEvent extends AWTEvent
{ public TimerEvent(Timer t) { super(t,TIMER_EVENT);}
public static final int TIMER_EVENT=AWTEvent.RESERVED_ID_MAX+5555;
}
class CustomEventFrame extends JFrame
{ public CustomEventFrame()
{setTitle("CustomEventTest");
setSize(300,50);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(new CustomEventPanel());
}
}
public class CustomEventTest
{ public static void main(String[] args)
{JFrame frame =new CustomEventFrame();
frame.show();
}
}
|
public void processevent(AWTEvent evt)->
public void processEvent(AWTEvent evt)
大小写错了。
public void processEvent(AWTEvent evt)
大小写错了。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。