JAVA Swing Gui Window Hangs

2019-08-29 23:58发布

问题:

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();
                }
            }
        });
    }

}

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.

Any help would be appreciated.

Thanks

回答1:

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.

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 );
}