无法从具有contructor对象的其他类追加textArea

时间:2016-03-22 22:02:36

标签: java methods append jtextarea

我正在尝试附加文字。我的printlnappedText方法中运行正常,但我无法附加从其他类发送的内容。如果我创建方法String并返回结果,我可以这样做。

我很感激。我也尝试了构造函数“this.textArea”等所有的东西。

public class BetParser   {

    public    JMenuBar  menuBar = new JMenuBar();
    public    JMenu menu, submenu;
    public    JMenuItem menuItem;
    public    JTextField textField;
    public    JTextArea textArea = new JTextArea(5,20);
    public    JFrame frame = new JFrame("BetBrain Parser");
    public    JPanel panel = new JPanel(new BorderLayout());
    public    JLabel gridlabel = new JLabel("");
    public    JScrollPane scrollPane = new JScrollPane(textArea);
    public    JButton button;
    public    URL url;

    public BetParser(){}

    public BetParser(JTextArea textArea){
      this.textArea=textArea;
    }

    public  void createAndShowGUI() {
      //some code here for the gui

      htmlparser parseitem = new htmlparser();   

      try {
          parseitem.JsoupParser(sitelink);
    }
}

我制作了appendText方法,当我从htmlparser类调用该方法时它不起作用

public void appendText(String msg){
    textArea.append(msg+"\n");
    System.out.println("skata me fraoules");
}

这是我上面使用的htmlparser课程。

public class htmlparser {

     public htmlparser(){  
     }

     public void  JsoupParser(String url) throws IOException{
       Document doc = Jsoup.connect(url).get();
       Elements newsHeadlines = doc.select("#mp-itn b a");
       System.out.println(doc.title());

       BetParser parserItem = new BetParser();

       parserItem.appendText(doc.title());
   }   
}

2 个答案:

答案 0 :(得分:0)

我想你在这里创建BetParser的实例:

public  void createAndShowGUI() {
//some code here for the gui

如果是这样,请不要在JsoupParser(String url)中再次创建它,只需将第一个作为参数传递即可。所以你的新JsoupParser就是这样的:

public void  JsoupParser(String url,  BetParser parserItem) throws IOException{

   Document doc = Jsoup.connect(url).get();
   Elements newsHeadlines = doc.select("#mp-itn b a");
   System.out.println(doc.title());

   parserItem.appendText(doc.title(), parserItem.getText());

} 

答案 1 :(得分:-1)

我没有测试过你的代码,但我注意到你没有引用你在appendText()中传递的变量:

这是textArea1

public void appendText(String msg, JTextArea textArea1){

这里是textArea

textArea.append(msg+"\n");

这是问题的一部分吗?

相关问题