通过 awt 和 setVisible() 正确使用事件和侦听器
下面,我有两个事件应该按照指定的时间间隔发生。但尽管很奇怪,但这种情况却没有发生。
class MyFrame extends JFrame implements ActionListener
{
private ActionListener listener;
private Timer t1;
private ActionListener listener2;
private Timer t2;
private String recentUpdate = "NULL";
private String recentPrinted ="NULL";
public MyFrame()
{
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();// gets the maximum size of the screen
setSize(d.width,(d.height/100)*10);//sets it to max. need to change this
// this shit find the max size of screen and puts it bottom left
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int)rect.getMinX();
int y = (int)rect.getMaxY()-getHeight();
setLocation(x,y-30);
setTitle("ALERT::OUTAGE");
MyPanel panel = new MyPanel();
add(panel);
listener = this; //I THINK THIS just says wait for the timer
t1 = new Timer(50000,listener); // time for each repaint
t1.start(); //go
listener2 = this;
t2 = new Timer(10000,listener2); //time to wait before setting visible again
t2.start();
}
public void actionPerformed(ActionEvent event)
{
repaint();
}
public void newUpdate(ActionEvent q)
{
// TODO Auto-generated method stub
this.setVisible(true);
System.out.println("Woopee!");
//checkForNewUpdate();
}
基本上,我有一个程序,它从网页中获取文本并在整个页面上显示字幕样式。它会重新绘制并每次向左移动 5 个点,使其在动画中看起来流畅。
当框架关闭时,它隐藏在背景中。关闭时隐藏 当 checkForUPdate 运行时,如果有更新,则该页面将重新出现。
我现在遇到的问题是我可以有 2 个事件和 2 个计时器。但我注意到它们是如何工作的。我如何设置它以便重绘将在事件上触发并且 checkforUpdate() 将在 ActionEvent q 上发生
Below, i have two events that are supposed to occur at their specified intervals. but as odd as it is, one is just not occuring.
class MyFrame extends JFrame implements ActionListener
{
private ActionListener listener;
private Timer t1;
private ActionListener listener2;
private Timer t2;
private String recentUpdate = "NULL";
private String recentPrinted ="NULL";
public MyFrame()
{
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();// gets the maximum size of the screen
setSize(d.width,(d.height/100)*10);//sets it to max. need to change this
// this shit find the max size of screen and puts it bottom left
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int)rect.getMinX();
int y = (int)rect.getMaxY()-getHeight();
setLocation(x,y-30);
setTitle("ALERT::OUTAGE");
MyPanel panel = new MyPanel();
add(panel);
listener = this; //I THINK THIS just says wait for the timer
t1 = new Timer(50000,listener); // time for each repaint
t1.start(); //go
listener2 = this;
t2 = new Timer(10000,listener2); //time to wait before setting visible again
t2.start();
}
public void actionPerformed(ActionEvent event)
{
repaint();
}
public void newUpdate(ActionEvent q)
{
// TODO Auto-generated method stub
this.setVisible(true);
System.out.println("Woopee!");
//checkForNewUpdate();
}
Basically, I have a program which takes text from a webpage and displays it marquee style across the page. it repaints and moves 5 points left each time, allowing it to look fluid in animation.
When the frame is closed, it is hidden in the background. hide_on_close
when checkForUPdate is run, if there is an update then the page will reappear.
The probelm i have now is i can have 2 events and 2 timers. but im note sure how they work. How can i set it so that repaint will trigger on the Event and checkforUpdate() will occur on the ActionEvent q
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个计时器最终都会触发相同的方法,actionPerformed。您的代码中没有任何内容可以调用 newUpdate。
Both timers end up triggering the same method, actionPerformed. There's nothing in your code to invoke newUpdate.
需要定义听众。我不知道如何将计时器与事件结合起来
needed to define the listeners. i didnt know how to combine timers with events