当程序最小化时,你能否停止重新绘制面板的程序? *使用Swing
答案 0 :(得分: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
}
}
}
);
答案 1 :(得分:1)
使用WindowListener的一个例子可以简单到对前一个示例进行小修改here:
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();
}
});
}