从小程序关闭框架

发布于 2024-12-04 07:54:14 字数 929 浏览 2 评论 0原文

我正在尝试在小程序中为 java 游戏制作一个控制台。控制台是一个带有 TextArea 的独立框架,用于显示加载/下载进度。 当我尝试在关闭时隐藏控制台时遇到一些问题。

这是简化的代码:

//Method inherited from the Applet class
public void init() {
    console = new Frame();
    console.setSize(500, 300);
    console.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e) {
            console.setVisible(false);
        }
    });

    consoleText = new TextArea();
    consoleText.setSize(500, 300);

    console.add(consoleText);

    console.setVisible(true);

    gameThread = new Thread() {
        public void run() {
            mainLoop();
        }
    };
    gameThread.start();
}

当我关闭框架“控制台”时,线程“gameThread”只是挂起。即使我替换“console.setVisible(false);”使用“console.setExtendedState(Frame.ICONIFIED);”,线程仍然挂起,没有任何警告。 在浏览器中运行小程序时,我必须在任务管理器中终止该进程。很烦人。

使用 JFrame 可以起到完全相​​同的作用,只是该错误更难重现。

我只是希望用户能够摆脱控制台。

有人有想法吗?谢谢 !

I'm trying to make a console for a java game, in an applet. The console is an independant Frame with a TextArea, used to show loading/downloading progression.
I'm running into some problems when I try to hide the console on closing.

Here is the simplified code :

//Method inherited from the Applet class
public void init() {
    console = new Frame();
    console.setSize(500, 300);
    console.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e) {
            console.setVisible(false);
        }
    });

    consoleText = new TextArea();
    consoleText.setSize(500, 300);

    console.add(consoleText);

    console.setVisible(true);

    gameThread = new Thread() {
        public void run() {
            mainLoop();
        }
    };
    gameThread.start();
}

The thread "gameThread" simply hang when I close the Frame "console". Even when I replace "console.setVisible(false);" with "console.setExtendedState(Frame.ICONIFIED);", the thread still hang without any warning.
While running the applet in a browser, I have to kill the process in the task manager. Very annoying.

Using a JFrame instead does the exact same thing, except the bug is harder to reproduce.

I just want the user to be able to get rid of the console.

Does anybody have an idea ? Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉墨 2024-12-11 07:54:14

我认为您不应该为此使用 Frame/JFrame 而应该使用 JDialog,因为窗口的行为就像一个对话框。还要确保您使用的是 JApplet 而不是 Applet。

编辑
请注意,我无法根据您显示的代码片段重现您的问题。考虑创建并发布一个 SSCCE 来直接向我们展示问题。

编辑2
我的 SSCCE 未重现您的问题:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AppletEg extends JApplet {
   private static final int MAX_LOOP = 30;
   private static final long SLEEP_TIME = 500;
   private JFrame console;
   private JTextArea consoleText;
   private Thread gameThread;

   @Override
   public void init() {
      console = new JFrame();
      console.setSize(500, 300);
      console.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            console.setVisible(false);
         }
      });

      consoleText = new JTextArea();
      consoleText.setPreferredSize(new Dimension(500, 300));

      console.add(new JScrollPane(consoleText));

      console.setVisible(true);

      gameThread = new Thread() {
         public void run() {
            mainLoop();
         }
      };
      gameThread.start();
   }

   private void mainLoop() {
      for (int i = 0; i < MAX_LOOP; i++) {
         System.out.println("I: " + i);
         try {
            Thread.sleep(SLEEP_TIME);
         } catch (InterruptedException e) {
         }
      }
   }
}

编辑 3
我使用 JDialog 的 SSCCE:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AppletEg extends JApplet {
   private static final int MAX_LOOP = 30;
   private static final long SLEEP_TIME = 500;
   private JDialog console;
   private JTextArea consoleText;
   private Thread gameThread;

   @Override
   public void init() {
      Window win = SwingUtilities.getWindowAncestor(this);
      console = new JDialog(win);
      consoleText = new JTextArea();
      consoleText.setPreferredSize(new Dimension(500, 300));

      console.add(new JScrollPane(consoleText));
      console.pack();
      console.setLocationByPlatform(true);
      console.setVisible(true);

      gameThread = new Thread() {
         public void run() {
            mainLoop();
         }
      };
      gameThread.start();
   }

   private void mainLoop() {
      for (int i = 0; i < MAX_LOOP; i++) {
         System.out.println("i: " + i);
         try {
            Thread.sleep(SLEEP_TIME);
         } catch (InterruptedException e) {
         }
      }
   }
}

I think that you shouldn't be using a Frame/JFrame for this but rather a JDialog, since the window is behaving as a dialog. Also be sure that you're using a JApplet rather than an Applet.

Edit
Note that I cannot reproduce your problem based on your displayed code snippet. Consider creating and posting an SSCCE that would show us the problem directly.

Edit 2
My SSCCE that does not reproduce your problem:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AppletEg extends JApplet {
   private static final int MAX_LOOP = 30;
   private static final long SLEEP_TIME = 500;
   private JFrame console;
   private JTextArea consoleText;
   private Thread gameThread;

   @Override
   public void init() {
      console = new JFrame();
      console.setSize(500, 300);
      console.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            console.setVisible(false);
         }
      });

      consoleText = new JTextArea();
      consoleText.setPreferredSize(new Dimension(500, 300));

      console.add(new JScrollPane(consoleText));

      console.setVisible(true);

      gameThread = new Thread() {
         public void run() {
            mainLoop();
         }
      };
      gameThread.start();
   }

   private void mainLoop() {
      for (int i = 0; i < MAX_LOOP; i++) {
         System.out.println("I: " + i);
         try {
            Thread.sleep(SLEEP_TIME);
         } catch (InterruptedException e) {
         }
      }
   }
}

Edit 3
My SSCCE using a JDialog:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AppletEg extends JApplet {
   private static final int MAX_LOOP = 30;
   private static final long SLEEP_TIME = 500;
   private JDialog console;
   private JTextArea consoleText;
   private Thread gameThread;

   @Override
   public void init() {
      Window win = SwingUtilities.getWindowAncestor(this);
      console = new JDialog(win);
      consoleText = new JTextArea();
      consoleText.setPreferredSize(new Dimension(500, 300));

      console.add(new JScrollPane(consoleText));
      console.pack();
      console.setLocationByPlatform(true);
      console.setVisible(true);

      gameThread = new Thread() {
         public void run() {
            mainLoop();
         }
      };
      gameThread.start();
   }

   private void mainLoop() {
      for (int i = 0; i < MAX_LOOP; i++) {
         System.out.println("i: " + i);
         try {
            Thread.sleep(SLEEP_TIME);
         } catch (InterruptedException e) {
         }
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文