JAVA Swing Gui 窗口挂起 [英] JAVA Swing Gui Window Hangs

查看:68
本文介绍了JAVA Swing Gui 窗口挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 SWING GUI 时遇到了问题,或者至少我认为是 Swing GUI.

I am having an issue with SWING GUI or at least I think it is the swing gui.

这是我的主要代码文件:

Here is my main code file:

/**
 * 
 */
package com.tda.t2.ctas.slasher;

import javax.swing.SwingUtilities;

import com.tda.t2.ctas.slasher.gui.mainFrame;
import com.tda.t2.ctas.slasher.utils.MyCloseListener;



public class SLASHer {

    public SLASHer () {
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        //EventQueue.invokeLater(new Runnable() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ConfigData myconfig = new ConfigData();
                try {
                    //TdaUrlHelper window = new TdaUrlHelper();
                    //window.tdaFrame.setVisible(true);
                    mainFrame tdaFrame = new mainFrame();
                    tdaFrame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

创建框架并打开它的简单调用.还有其他文件我没有放在这里是为了空间.但是我遇到的问题(我已经在多台机器和操作系统上尝试过)是窗口上的按钮似乎挂了.我可以选择窗口并单击按钮,它们会像被击中一样突出显示,但没有任何反应.我有一个选项卡式平面,单击其他选项卡也没有任何作用.有时持续约 15 秒,有时持续几分钟.但它最终总是会返回并响应新的输入(即它不记得我在返回之前所做的所有点击).整个应用程序很简单,因为它会等待用户执行某些操作,然后再执行某些操作,因此我对它为什么似乎挂起感到困惑.

Simple call to create the frame and open it. There are other files that I did not put here for space. But the problem that I have (and I have tried on several machines and operation systems) is that the buttons on the window seem to hang. I can select the window and click on the buttons and they highlight like they were hit but nothing happens. I have a tabbed plane and clicking on the other tabs also does nothing. Some times this last for about 15 seconds and other times it lasts several minutes. But it always eventually comes back and will respond to new input (ie it does not remember all the click around I did before it came back). The application overall is simple in that it sits waiting until a user does something before it does something so I am confused on why it seems to hang.

任何帮助将不胜感激.

谢谢

推荐答案

挂在按钮上的违规代码是什么?检查您的控制台是否有异常,并在该代码的顶部和底部放置一些 System.out.println() 语句.看看你是否看到打印出来的那些打印语句.观察顶部的打印和底部的打印需要多长时间.如果您看到两个语句,那么您就知道整个块正在执行,但是如果显示最后一个语句需要一段时间,您就知道您正在挂断 Swing 线程(也称为 EDT - 事件调度线程).在执行 ActionListener 时,Swing UI 中的第一条规则不能重新绘制.

What is the offending code attach to the button that hangs? Check your console for exceptions, and put some System.out.println() statements at the top and bottom of that code. See if you see those print statements print out. Watch how long it takes for the one at the top to print and bottom one to print. If you see both statements then you know that whole block is executing, but if it takes a while to show the last statement you know you are hanging up the Swing thread (also known as EDT - event dispatch thread). Rule number one in Swing the UI can't repaint while it's executing your ActionListener.

为了制作响应式 UI,您必须在 10-100 毫秒内(视觉上几乎是瞬时的)看到第一条和最后一条语句出现在控制台上.如果你真的想花哨,你可以在停止和底部使用 System.currentTimeMillis() .减去两个值并 println() 它.这会告诉你那个监听器运行了多长时间.如果它大于 100 毫秒,您需要通过改进算法或卸载线程上的长计算来重构代码(请参阅此 SwingWorker 教程).

In order to make a responsive UI you have to see the first and last statement appear on the console in under 10-100ms (visually almost instantaneous). If you really want to get fancy you can use System.currentTimeMillis() at the stop and bottom. Subtract the two values and println() it. That'll tell you exactly how long that listener ran for. If it's greater than 100ms you need to restructure your code by either improving your algorithm or off loading the long calculation on a thread (see this SwingWorker tutorial).

public void actionPerformed(ActionEvent event) {
    System.out.println("Starting SomeProcess");
    long start = System.currentTimeMillis();

    // all your code belongs here

    long duration = System.currentTimeMillis() - start;
    System.out.printf("SomeProcess took %,d ms%n", duration );
}

这篇关于JAVA Swing Gui 窗口挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆