【问题标题】:JButton run main methodJButton运行主方法
【发布时间】:2014-01-18 04:22:21
【问题描述】:

我正在做一些文本挖掘应用程序。它由TextRazor API Java Swing 组成。如何使用JButton 运行main() 类?单击按钮后,必须触发main() 类中的代码。下面是代码,请帮帮我。

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {   
    //I want the main class to be called here** 
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {         
    JOptionPane.showMessageDialog(null, "Completed Analysis!","Alert", 1);
    jButton5.setEnabled(false);
    jTextArea2.setEditable(false);
    jTextArea3.setEditable(false);
}   
    /**
     * @param args
     * @throws NetworkException 
     */
public static void main(String[] args) throws NetworkException, AnalysisException {

    // Sample request, showcasing a couple of TextRazor features
    String API_KEY = "7d5066bec76cb47f4eb4e557c60e9b979f9a748aacbdc5a44ef9375a";

    TextRazor client = new TextRazor(API_KEY);

    client.addExtractor("words");
    client.addExtractor("entities");
    client.addExtractor("entailments");
    client.addExtractor("senses");
    client.addExtractor("entity_companies");

    String rules = "entity_companies(CompanyEntity) :- entity_type(CompanyEntity, 'Company').";

    client.setRules(rules);

    AnalyzedText response = client.analyze("Barclays misled shareholders and the public RBS about one of the biggest investments in the bank's history, a BBC Panorama investigation has found.");

    for (Sentence sentence : response.getResponse().getSentences()) {
        for (Word word : sentence.getWords()) {
            System.out.println("----------------");
            System.out.println("Word: " + word.getLemma());

            for (Entity entity : word.getEntities()) {
                System.out.println("Matched Entity: " + entity.getEntityId());
            }
            for (Sense sense: word.getSenses()) {
                System.out.println("Word sense: " + sense.getSynset() + " has score: " + sense.getScore());
            }                
        }
    }

    // Use a custom rule to match 'Company' type entities

    for (Custom custom : response.getResponse().getCustomAnnotations()) {
        for (Custom.BoundVariable variable : custom.getContents()) {
            if (null != variable.getEntityValue()) {
                for (Entity entity : variable.getEntityValue()) {
                    System.out.println("Variable: " + variable.getKey() + " Value:" + entity.getEntityId());
                }
            }
        }
    }           
}

【问题讨论】:

标签: java swing text-mining


【解决方案1】:

Class中的main方法也是一个普通的方法,它是为JVM启动java应用而设计的。但是,你也可以在你的方法中调用它

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {         
    JOptionPane.showMessageDialog(null, "Completed Analysis!","Alert", 1);
    jButton5.setEnabled(false);
    jTextArea2.setEditable(false);
    jTextArea3.setEditable(false);
    ClassName.main(new String[]{"arg1","arg2"}); 
}  

添加了虚拟参数来调用主方法

【讨论】:

  • 嗨 Octopus,它可以工作,但如果我尝试在 jTextArea 中显示 '("Variable:" + variable.getKey() +" Value:" + entity.getEntityId())' 会出现问题。其显示的非静态变量 jTextArea3 无法从静态内容中引用
  • 是的,这就是静态方法在 Java 中的工作方式。您不能从静态上下文中引用非静态变量。您必须使用静态修饰符声明 jTextArea3 变量
猜你喜欢
  • 2019-02-25
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多