【问题标题】:Applet isShowing() vs Frame isShowing()小程序 isShowing() 与框架 isShowing()
【发布时间】:2012-08-23 08:47:17
【问题描述】:

我正在尝试复制最初扩展 Applet 类的代码。 但下面的代码对于Frame 始终是正确的。我知道如果框架 isVisible() 也为真,isShowing() 将始终返回真。除非 setVisible() 明确设置为 false,否则 isShowing() 将返回 true。

我的目标是在应用程序框架最小化时暂停守护线程循环。

public class Screen extends Applet{

@Override
public void init() {

    addComponentListener(new ComponentAdapter() {

        @Override
        public void componentShown(ComponentEvent e) {
            //do stuff

        }

        @Override
        public void componentHidden(ComponentEvent e) {
            //Stop doing stuff
        }       
    });
}

实施建议(鲍里斯·帕夫洛维奇)

public class Screen extends Frame implements Runnable{

private boolean runL;
private Thread thread;

public Screen() {
    setSize(256,256);
    setVisible(true);

    addWindowFocusListener(new WindowAdapter() {

        @Override
        public void windowGainedFocus(WindowEvent e) {
            runL = true;
            starThread();
        }

        @Override
        public void windowLostFocus(WindowEvent e) {
            runL = false;
        }

    });

}


@Override
public void run() {
    while(runL){System.out.println("showing");}
}

private void starThread(){
    if(thread == null){
        thread = new Thread(this);
        thread.start();
    } else if(!thread.isAlive()){
        thread = new Thread(this);
        thread.start();
    }

}

【问题讨论】:

    标签: java applet awt frame


    【解决方案1】:

    查看有关“How to Use Focus Subsystem”的教程。 WindowsAdapter 允许覆盖可用于启动/停止计算的不同状态转换。

    【讨论】:

      【解决方案2】:

      我的目标是在应用程序框架最小化时暂停守护线程循环。

      添加WindowListener 并在windowIconified(WindowEvent) 上停止计算。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-02
        • 2015-12-17
        • 1970-01-01
        相关资源
        最近更新 更多