简体   繁体   English

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

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

I have a class named Parser which gets some input and do some calculations and output the results. 我有一个名为Parser的类,该类获取一些输入并进行一些计算并输出结果。 I also have a jFrame, which has some text fields. 我也有一个jFrame,其中包含一些文本字段。 I am misunderstanding how to run the parser and use the inputs from the jFrame. 我误解了如何运行解析器并使用jFrame的输入。 I don't know if I should implement the action Listener in my Parser class? 我不知道是否应该在我的Parser类中实现动作侦听器? or should I import all my Parser class methods in the jFrame? 还是应该在jFrame中导入我所有的Parser类方法? should I have run method in my main of the Parser or should I use the void run in the jframe class?? 我应该在解析器主程序中运行run方法还是应该在jframe类中使用void run?

Here is my class Parser: 这是我的类解析器:

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

            }
        });              
} 
}

and here is the jFrame; 这是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

    }
    }

here I am trying to get keywordcount variable from the jFrame which is the int X in the getXKeywords method. 在这里,我试图从jFrame获取keywordcount变量,它是getXKeywords方法中的intX。

I solved my problem with the help of this link 我借助此链接解决了我的问题

I created a constructor in my parser class and also included a jframe in the parser class as follow: 我在解析器类中创建了一个构造函数,并在解析器类中包含了一个jframe,如下所示:

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

and in the action performed of the jframe class I call my parser constructor class: 在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