有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Swing GUI控件在后台执行和操作时停止

enter image description here

现在,当我点击导入按钮时,一个操作在后端执行。因此,当GUI停止控制,我不能按下任何按钮,但我希望可以按下取消按钮,以便用户可以取消正在进行的操作。 我需要一些关于如何做的想法

JButton importButton = new JButton("Import");
importButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {


    for(ResourceListObject currentImage : imagesToBeImported){

        String ImOid = String.valueOf(currentImage.getOID());


            String image_content = restEngine.getimageContent(ImOid);

            String str[]=new String[3];

            str[0] = "aix";
            str[1] = image_content;
            str[2] = selectedRepository.concat("_").concat(currentImage.getName());

            PowerVC_Image_Deployer imgDeployer= new PowerVC_Image_Deployer(str);



             }

        }

共 (1) 个答案

  1. # 1 楼答案

    您的问题是GUI正在调用操作并阻塞,也就是说,它等待操作完成,因为它正在等待,所以不能进行用户交互。要解决这个问题,必须在应用程序中使用多个线程

    Careful use of concurrency is particularly important to the Swing programmer. A well-written Swing program uses concurrency to create a user interface that never "freezes" — the program is always responsive to user interaction, no matter what it's doing. To create a responsive program, the programmer must learn how the Swing framework employs threads.

    你可以在这里找到更多信息:http://docs.oracle.com/javase/tutorial/uiswing/concurrency/