诡异的。查询可以在 Fuseki HTML 表单中运行,但不能在 JAVA 中运行?

发布于 2024-11-28 02:27:20 字数 1320 浏览 0 评论 0原文

我想请教一些关于线程使用的问题。我查看了很多帖子和这些帖子建议的链接,但仍然一片空白。 我有一个包含几个类的 NetBeans 项目。其中之一是 Gui 类,我只需单击一个按钮即可执行一些处理。我从 Gui 调用另一个类的实例,该类又调用其他类。其中一个类向 TDB 后端数据库提交 Sparql 查询。目前所有输出都保存到文件中。

我想要做的是以某种方式使从 Gui 调用的类在另一个线程上运行,并且还能够从一个或多个被调用的类更新 Gui 上的 EditorPane 和 TextArea。到目前为止,我已经尝试调用 Gui 类的实例并在其中使用公共方法,但这不起作用。我正在调用实例 Gui

Gui gui = new Gui();
gui.setEditorPaneText("File name is: " + fn);

,并且 Gui 类中的方法是

public void setEditorPaneText(final String string) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            setString(string);
            EditorPane.setText(getString());
            EditorPane.repaint();
        }
    });
}

我尝试运行调试器,但处理从方法的第一行跳到最后一个大括号,而不处理其中的代码。我的 Gui 类具有以下主要方法。评论部分是我在阅读有关该问题的大量帖子时更改的事件队列的先前版本。

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            new Gui().setVisible(true);
            throw new UnsupportedOperationException("Not supported yet.");
        }
    });
}

以下是我在阅读有关此问题的一些帖子后替换的主要方法的先前代码。

java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Gui().setVisible(true);
        }
    });

任何有用的信息将不胜感激。谢谢。

I would like to ask some questions regarding the use of threads. I have looked at a lot of posts and links suggested from those posts but still came up blank.
I have a NetBeans project that has a few classes. One of them is the Gui class that I use to just click a button and some processing gets performed. From the Gui I call an instance of another class that in turn calls other classes. One of these classes submits a Sparql query to a TDB backend database. All output is saved to files for now.

What I would like to do is to somehow make the class called from the Gui to run on another thread and also to be able to update an EditorPane and a TextArea on the Gui from one or more of the called classes. Up to now I have tried calling an instance of the Gui class and use a public method within but this does not work. I am calling the instance Gui with

Gui gui = new Gui();
gui.setEditorPaneText("File name is: " + fn);

and the method in the Gui class is

public void setEditorPaneText(final String string) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            setString(string);
            EditorPane.setText(getString());
            EditorPane.repaint();
        }
    });
}

I tried running the debugger but the processing skips from the first line of the method to the last curly bracket without processing the code within. My Gui class has the following as a main method. The commented part was a previous version of the event queue that I changed while I was reading through the numerous posts on the issue.

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            new Gui().setVisible(true);
            throw new UnsupportedOperationException("Not supported yet.");
        }
    });
}

The following is the previous code of the main method that I replaced after reading some of the posts on this issue.

java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Gui().setVisible(true);
        }
    });

Any helpful information will be much appreciated. Thank you.

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

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

发布评论

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

评论(1

無處可尋 2024-12-05 02:27:20

我认为您的主要错误是您创建了 Gui 类的两个实例。您有两次以下代码片段:new Gui()。请看下面我的示例代码,了解如何将 Gui 传递到工作线程的示例。

// This is handwritte-untested-uncompiled code to show you what I mean
public class Main {
  public static void main(String[]args) {
     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           Gui g = new Gui();
           g.show(); // show() is equal to setVisible(true)
           g.doBackendAction(); // Normally this would be invoked by a button or sthg. I was to lazy
        }
     });
  }
}


public class Gui extends JFrame {
  private JTextArea area;
  public Gui() {
    // Just some code to create the UI. Not sure if this actually does sthg right :P
    area = new JTextArea();
    setContentPane(area);
    pack();
  }


  public void setTextAreaContent(final String string) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            area.setText(string);
            this.repaint(); // Not 100% sure if we need this

        }
    });
  }

  public void doBackgroundWork() {
    BackgroundWorker w = new BackgroundWorker(this);
    new Thread(w).start(); // Start a worker thread
  }
}

public class BackgroundWorker implements Runnable {
   private Gui gui;
   public BackgroundWorker(Gui gui) {
     this.gui = gui; // we take the initial instance of Gui here as a parameter and store it for later
   }

   public void run() {
     try { Thread.sleep(10 * 1000); } catch (InterruptedException e) {; }
     this.gui.setTextAreaContent("Hello World!"); // calls back to the Gui to set the content 

   }
}

I think your main error is that you create two instances of your Gui class. You have the following snippet twice: new Gui(). Take a look at my example code below to see an example how to pass the Gui to your worker thread.

// This is handwritte-untested-uncompiled code to show you what I mean
public class Main {
  public static void main(String[]args) {
     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           Gui g = new Gui();
           g.show(); // show() is equal to setVisible(true)
           g.doBackendAction(); // Normally this would be invoked by a button or sthg. I was to lazy
        }
     });
  }
}


public class Gui extends JFrame {
  private JTextArea area;
  public Gui() {
    // Just some code to create the UI. Not sure if this actually does sthg right :P
    area = new JTextArea();
    setContentPane(area);
    pack();
  }


  public void setTextAreaContent(final String string) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            area.setText(string);
            this.repaint(); // Not 100% sure if we need this

        }
    });
  }

  public void doBackgroundWork() {
    BackgroundWorker w = new BackgroundWorker(this);
    new Thread(w).start(); // Start a worker thread
  }
}

public class BackgroundWorker implements Runnable {
   private Gui gui;
   public BackgroundWorker(Gui gui) {
     this.gui = gui; // we take the initial instance of Gui here as a parameter and store it for later
   }

   public void run() {
     try { Thread.sleep(10 * 1000); } catch (InterruptedException e) {; }
     this.gui.setTextAreaContent("Hello World!"); // calls back to the Gui to set the content 

   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文