简体   繁体   English

每分钟关闭并重新打开Java窗口

[英]Close and Reopen Java Window Every Minute

I'm working on an app that uses Java and JFreeChart. 我正在开发一个使用Java和JFreeChart的应用程序。 In my main I want to use a timer to close a window/object of the class and create a new object and window of the class. 在我的主要文章中,我想使用计时器关闭类的窗口/对象并创建类的新对象和窗口。 JFreeChart's dynamic capabilities don't seem to work with the APIs that I'm using and this is about the only way I can give the graph a dynamic feel. JFreeChart的动态功能似乎无法与我正在使用的API一起使用,这是使图具有动态感觉的唯一方法。

The below code is what I currently have for the main, but all that it does is open one window and then close it and open a new one a minute later. 下面的代码是我目前主要使用的代码,但是它所做的只是打开一个窗口,然后关闭它,并在一分钟后打开一个新窗口。 Obviously, it's pretty clear why that's all that happens, but I can't seem to think of a good way to make it so that it creates and deletes the objects all within the timer. 显然,很清楚为什么会这样,但是我似乎想不出一种好方法来使其在计时器内创建和删除所有对象。

  public static void main(final String[] args) throws Exception {
    Scanner in = new Scanner(System.in);
    final String host = "";
    final String username = "";
    final String password = "";
    final String id = "testStream"; 

    final GraphStream demo = new GraphStream("Dia Example graph", host, username, password, id);
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
    new java.util.Timer().schedule( 
            new java.util.TimerTask() {
                @Override
                public void run() {
                    demo.setVisible(false);
                    demo.dispose();
                    try {
                        final GraphStream demo2 =new GraphStream("Dia Example graph", host, username, password, id);
                        demo2.pack();
                        RefineryUtilities.centerFrameOnScreen(demo2);
                        demo2.setVisible(true);
                    } catch (DataServiceException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }, 
            60000); 
}

}

I suggest to split the constructor into a minimalist constructor and a refresh method: 我建议将构造函数拆分为极简构造函数和刷新方法:

public static void main(final String[] args) throws Exception {
   final String host = "";
   final String username = "";
   final String password = "";
   final String id = "testStream"; 
   final GraphStream demo = new GraphStream( "Dia Example graph" );
   new java.util.Timer().schedule( 
      new java.util.TimerTask() {
         @Override
         public void run() {
            demo.refresh( host, username, password, id );
            demo.pack();
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible( true );
         }
      }, 
      0, 60000); 
}

As you can see timer.schedule() may has 3 parameters to start immediately. 如您所见,timer.schedule()可能具有3个参数以立即启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM