通过 awt 和 setVisible() 正确使用事件和侦听器

发布于 2024-10-15 10:27:46 字数 1788 浏览 2 评论 0原文

下面,我有两个事件应该按照指定的时间间隔发生。但尽管很奇怪,但这种情况却没有发生。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-10-22 10:27:46

两个计时器最终都会触发相同的方法,actionPerformed。您的代码中没有任何内容可以调用 newUpdate。

Both timers end up triggering the same method, actionPerformed. There's nothing in your code to invoke newUpdate.

能怎样 2024-10-22 10:27:46

需要定义听众。我不知道如何将计时器与事件结合起来

    listener = new ActionListener()
    {
        public void actionPerformed(ActionEvent event) 
        {
            checkForNewUpdate();
        }           
    };
    //I THINK THIS just says wait for the timer
    t1 = new Timer(3000,listener); // time for each repaint
    t1.start();                 //go



    listener2 = new ActionListener()
    {
          public void actionPerformed(ActionEvent q) 
          {repaint();}
    };
    t2 = new Timer(50,listener2); //time to wait before setting visible again
    t2.start();
}
public void actionPerformed(ActionEvent event)
{
    checkForNewUpdate();
    System.out.println("Woopee!");
    /**System.out.println(t2.getActionListeners().toString());*/ // just for testing
}

needed to define the listeners. i didnt know how to combine timers with events

    listener = new ActionListener()
    {
        public void actionPerformed(ActionEvent event) 
        {
            checkForNewUpdate();
        }           
    };
    //I THINK THIS just says wait for the timer
    t1 = new Timer(3000,listener); // time for each repaint
    t1.start();                 //go



    listener2 = new ActionListener()
    {
          public void actionPerformed(ActionEvent q) 
          {repaint();}
    };
    t2 = new Timer(50,listener2); //time to wait before setting visible again
    t2.start();
}
public void actionPerformed(ActionEvent event)
{
    checkForNewUpdate();
    System.out.println("Woopee!");
    /**System.out.println(t2.getActionListeners().toString());*/ // just for testing
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文