繁体   English   中英

在jFrame中单击按钮时执行其他类的方法

[英]perform methods from other class when button is clicked in jFrame

我有一个名为Parser的类,该类获取一些输入并进行一些计算并输出结果。 我也有一个jFrame,其中包含一些文本字段。 我误解了如何运行解析器并使用jFrame的输入。 我不知道是否应该在我的Parser类中实现动作侦听器? 还是应该在jFrame中导入我所有的Parser类方法? 我应该在解析器主程序中运行run方法还是应该在jframe类中使用void run?

这是我的类解析器:

public class Parser{
    public static List getXKeywords(String Url, int X, String html) throws Exception {
//somemethod with someoutput
    }
    public static void main(String[] args) throws Exception {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SpyBiteDemo Sp = new SpyBiteDemo();
                Sp.setVisible(true);
             int X=Sp.getKeywordcount();
              //this top line is not correct because it can only be done when the jframe jButton1 was clicked

            }
        });              
} 
}

这是jFrame;

public class SpyBiteDemo extends javax.swing.JFrame {
    /**
     * Creates new form SpyBiteDemo
     */
    public SpyBiteDemo() {
        initComponents();

    }
    public String getKeywordcount()
    {
    return jTextField4.getText();
    }
//some methods
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        //get the input from the jframe
        //feed it to the parser?? how???
       String SeedUrl=jTextField1.getText();
            Parser P=new Parser();
            //I don't have access to methods 
           because they are static

    }
    }

在这里,我试图从jFrame获取keywordcount变量,它是getXKeywords方法中的intX。

我借助此链接解决了我的问题

我在解析器类中创建了一个构造函数,并在解析器类中包含了一个jframe,如下所示:

public class Parser {
        SpyBiteDemo Sp=new SpyBiteDemo();
    public Parser(SpyBiteDemo Sp)
            {
               this.Sp=Sp;
             int X = Sp.getXKeywords();
         //do whatever
      }

在jframe类执行的操作中,我将解析器构造函数类称为:

public class SpyBiteDemo extends javax.swing.JFrame {
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

       Parser P=new Parser(this);

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM