GUI threading in java

后端 未结 3 1389
天命终不由人
天命终不由人 2020-12-22 08:34

I\'m trying to code a simple game in Java. The basic structure is a single JFrame with different JPanels that I add/remove at different times. At s

3条回答
  •  一生所求
    2020-12-22 09:17

    You can start some code with a time delay using TimerTask:

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        public void run() {
            invokeLater(); // This starts after [delay] ms 
            // and - if given - will run every [period] ms.
        }
    }, delay, period);
    

    You could solve your problem with this, though it won't be a pretty solution.

    // edit: (see comments) you should synchronize accesses to the gui properly, else it will give you errors.

提交回复
热议问题