java - 小程序 isShowing() 与框架 isShowing()

标签 java applet awt frame

我正在尝试复制最初扩展 Applet 类的代码。 但下面的代码对于 Frame 始终适用。据我所知,如果框架 isVisible() 也为 true,则 isShowing() 将始终返回 true。除非 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();
    }

}

最佳答案

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

关于java - 小程序 isShowing() 与框架 isShowing(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12087969/

相关文章:

java - Maven jersey Artifact 创建一个没有目标运行时选项的项目

java - Spring找不到类路径中存在的类

java - 为什么 NumberFormat 返回空白字段?

Java找不到符号

java - JLabel - 在鼠标输入时更改为 JTextField

java - 多个 graphs2d 对象

java - Sonar 自定义规则检查java集合类型是unknownSymbol

java - Java Applet代码可以看到吗?

java - 使用 OO 编程避免多个嵌套 if

java - JRadioButton选择颜色