0

当程序最小化时,您可以停止程序重新绘制面板吗?* 使用秋千

4

2 回答 2

2

我猜真正的问题是在最小化时暂停动画循环等。如果您使用的是JFrame,则可以通过以下方式检测窗口最小化事件:

myFrame.addWindowStateListener(
    new WindowStateListener() {
        @Override
        public void windowStateChanged(WindowEvent evt) {
            if (myFrame.getState() == Frame.ICONIFIED) {
                // suspend painting/animation loops
            } else {
                // resume or continue painting/animation loops
            }
        }
    }
);
于 2012-03-07T17:50:35.787 回答
1

使用 WindowListener 的示例可以像对我之前的示例进行小修改一样简单,可以在此处找到:

private static void createAndShowGui() {
  ShowGraph showGraphPanel = new ShowGraph(MAX_POINTS);
  TimerListener timerListener = new TimerListener(MAX_POINTS, showGraphPanel);

  JFrame frame = new JFrame("TestShowGraph");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(showGraphPanel);
  frame.pack();
  frame.setLocationByPlatform(true);
  frame.setVisible(true);

  // Timer now made a final variable so it can be referred to
  final Timer timer = new Timer(TIMER_DELAY, timerListener);
  timer.start();

  frame.addWindowListener(new WindowAdapter() {

     @Override
     public void windowOpened(WindowEvent arg0) {
        timer.start();
     }

     @Override
     public void windowIconified(WindowEvent arg0) {
        timer.stop();
     }

     @Override
     public void windowDeiconified(WindowEvent arg0) {
        timer.start();
     }

     @Override
     public void windowActivated(WindowEvent arg0) {
        timer.start();
     }
  });
}
于 2012-03-07T22:26:23.073 回答