java - JFrame 在处理另一个事件时不响应事件

标签 java events jframe java-3d canvas3d

我有以下场景: 1. 我在实现 ActionListener 的“FrontEnd.java”类中创建一个 JFrame jFrame。然后,我将一个 Canvas3D 对象添加到其 ContentPane 中,然后,我添加一个 JMenuBar 和一些 JMenus每个都有几个JMenuItems。 2. 然后我有一个 RendererClass.java 类,用于将球体渲染到 Canvas3D 对象中。因此,从 FrontEnd 中,单击其中一个 JMenuItems,我处理该事件,并从 actionPerformed(ActionEvent ae) 方法调用 RendererClass(jFrame) 并获取 jFrame 对象从渲染器一侧,Canvas3D 在其中绘制球体。所以,我把它们画在最初的位置。 3. 然后,我在 FrontEnd 中的循环中更新球体的坐标,该循环调用 RendererClass 中的“updateCooperatives()”。这是一个沉重的循环,可能会持续长达一分钟。在更新球体的坐标时,我展示了如何在 Canvas3D 中更新它们(在每次迭代中,坐标仅略有变化) - 这是通过 RendererClass 中的 updateCooperatives() 完成的。

问题是,在从 actionPerformed(...) 方法调用的循环中,我无法与 jFrame 交互,也无法事件关闭它。它实际上正在监听,因为当循环结束时,如果我在循环中单击“X”(关闭窗口),则窗口将关闭。此外,如果我尝试在 Canvas3D 上旋转相机,它在循环完成之前不会更新旋转。请注意,在循环中,我看到我的球体在移动。此外,按钮停止响应并且将不再响应 - 下拉 JMenuItems 似乎位于 Canvas3D 下方并且无法访问。

代码如下:

public class FrontEnd implements ActionListener {

    /**
     * The main Window and menus
     */
    private static JFrame jFrame = null;
    private JMenuBar jMenuBar;
    private JMenu fileMenu;
    private JMenu editMenu;
    private JMenu aboutMenu;

    private JMenuItem openAction;
    private JMenuItem exitAction;
    private JMenuItem renderAction;
    private JMenuItem aboutAction;

    /**
     * The renderer
     */
    private RendererClass renderer = null;

    /**
     * Constructor
     *
     */
    public FrontEnd() {

        jFrame = new JFrame("The Main Window");
        jFrame.getContentPane().add(new     Canvas3D(SimpleUniverse.getPreferredConfiguration()));

        jFrame.setPreferredSize(new Dimension(800, 600));
        jFrame.setResizable(false);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /**
         * Menus
         */
        jMenuBar = new JMenuBar();
        jFrame.setJMenuBar(jMenuBar);

        //Dropdown menus
        fileMenu = new JMenu("File");
        editMenu = new JMenu("Edit");
        aboutMenu = new JMenu("About");
        jMenuBar.add(fileMenu);
        jMenuBar.add(editMenu);
        jMenuBar.add(aboutMenu);

        //Create and add simple menu item to one of the drop down menu
        openAction = new JMenuItem("Open");
        openAction.setMnemonic('O');
        exitAction = new JMenuItem("Exit");
        exitAction.setMnemonic('x');
        renderAction = new JMenuItem("Render All");
        renderAction.setMnemonic('R');
        aboutAction = new JMenuItem("About");
        aboutAction.setMnemonic('A');

        fileMenu.add(openAction);
        fileMenu.add(exitAction);
        editMenu.add(renderAction);
        aboutMenu.add(aboutAction);

        //Event Listeners
        openAction.addActionListener(this);
        exitAction.addActionListener(this);
        renderAction.addActionListener(this);
        aboutAction.addActionListener(this);

        // Configure the JFrame
        jFrame.setResizable(false);

        jFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent winEvent) {
                System.exit(0);
            }
        });

        jFrame.setSize(820,620);
        jFrame.setVisible(true);
        jFrame.pack();

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == renderAction) {
            doHeavyLoop();
        }
    }

    public void doHeavyLoop() {
        renderer = new RendererClass(jFrame);
        for (int i=0; i<100000; i++) {
            try {
                    Thread.sleep(1);
                } catch (InterruptedException ie) {
                    System.out.println("Baaad MF.");
                }
                    renderer.updateCoordinates();
            }
        }
    }
}


/**
 * The RenedererClass class
 */
public static JFrame jFrame;
public SimpleUniverse universe;
public BrachGroup branchGroup;
public static PickCanvas pickCanvas;    

public RendererClass(JFrame frame) {

    jFrame = frame;
    jFrame.update(jFrame.getGraphics());

    theCanvas = (Canvas3D) jFrame.getContentPane().getComponent(0);
    theCanvas.addMouseListener(this);
    //STUFF HERE... CREATE AND ADD SPHERES TO THE BRANCHGROUP
    // Add the brachgroup to the Universe.
    universe.addBranchGraph(branchGroup);

    //The following three lines enable navigation through the scene using the mouse.
    OrbitBehavior ob = new OrbitBehavior(theCanvas);
    ob.setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0), Double.MAX_VALUE));
    universe.getViewingPlatform().setViewPlatformBehavior(ob);

    //Now make it pickable for picking spheres
    pickCanvas = new PickCanvas(theCanvas, branchGroup);
    pickCanvas.setMode(PickCanvas.GEOMETRY);
    pickCanvas.setTolerance(0.0f);
}

public void updateCoordinates() {
    // Irrelevant... just set Translations transforms to the spheres
}

所以,问题很简单...为什么 JFrame 窗口卡住并停止响应事件?而且,为什么循环结束后,过去的事件都突然被处理了?最后但并非最不重要的一点是,您将如何实现这样的功能(在一个类中创建一个 JFrame,并将其传递给其他类,以便它们可以将内容放入其中的 Canvas3D 中......这样在循环时,我可以与Canvas3D?

提前致谢。

最佳答案

当您在 UI 或 Event Dispatch Thread 中运行繁重的处理任务时,JFrame 窗口停止响应事件

解决方案是在其自己的线程中运行此资源密集型任务。在 Swing 中有一个类称为 SwingWorker 它将处理此类可能耗时的工作。

对于此处的应用程序,您可以在类级别创建一个非常简单的 SwingWorker 实现,如下所示:

SwingWorker worker = new SwingWorker<Void, Void>() {

   @Override
   protected Void doInBackground() throws Exception {
      doHeavyLoop();
      return null;
   }
};

并调用:

worker.execute();

在您的 ActionListener 中执行。

关于java - JFrame 在处理另一个事件时不响应事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12519706/

相关文章:

javascript - 阻止某些事件监听器为事件触发,但允许父监听器触发

java - 如何在java游戏中分离 View 和模型?

java - JFrame随机渲染图形

java - 为什么在 Eclipse 中初始化 Java Tooling 时出现 NullPointerException?

Java 在根路径上设置 cookie

java - 如果线程不会自行停止,如何停止它?

java - 实现自定义 NFC 管理器的最简单方法是什么?

Jquery:如何根据事件从其他列表设置文本框的值?

php - 将数据从 MySQL 提取到 Elgg 应用程序中

java - 如何围绕 JPanel 缩放 JFrame?