2016-06-27 14 views
1

mainクラスは、Jsoupを使用してウェブサイトをスクレービングする別のクラスのalertTextのコンテンツを取得しようとしています。スクラップがうまくいきました。エラーが発生しないようにするには、alertText変数に問題があります。私はAlertSystemDaemonクラスのメインクラスコールを持って、JLabel jLabelAlertSystemの内容を記入しようとしています。 私は試してみましたjLabelAlertSystem.setText(String.valueOf(alertText));別のクラスからの変数を取得しようとしています

しかし、これは以前のようにalertTextのエラーになります。

メインクラス:

public static void main(String[] args) { 


    MainPanel mainPanel = new MainPanel(); 
    mainPanel.initialize(); 
    mainPanel.frame.setVisible(true); 
    systemTray(); 
} 


private void initialize() { 

    SplashScreen splash = new SplashScreen(3000); 
    splash.showSplash(); 

    AlertSystemDaemon alertsystemdaemonobject = new AlertSystemDaemon(); 
     try { 
      alertsystemdaemonobject.alertSystemMessage(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 


    frame = new JFrame(); 
    frame.setTitle(Label.MAIN_PANEL); 
    frame.setBounds(100, 100, 1140, 768);// 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 


    JLabel jLabelAlertSystem = new JLabel(alertText, SwingConstants.RIGHT); 
    jLabelAlertSystem.setBounds(750, 0, 360, 20); 
    jLabelAlertSystem.setFont(new Font("Calibri", Font.BOLD, 15)); 
    jLabelAlertSystem.setForeground (Color.red); 
    jLabelAlertSystem.setText(String.valueOf(alertText)); 
    frame.getContentPane().add(jLabelAlertSystem); 

その他のクラス:あなただけのクラスAlertSystemDaemonpublic String alertSystemMessage()にあなたの方法public void alertSystemMessage()を変更し、代わりにsysoutalertTextあなたのようにそれを返す

import java.io.IOException; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 


public class AlertSystemDaemon { 

public void alertSystemMessage() throws IOException { 
//MainPanel mainpanel = new MainPanel(); 
String url = "http://example.com/alertpage"; 
    Document document = Jsoup.connect(url).get(); 

    String alertText = document.select("p").first().text();   
// jLabelAlertSystem.setText(String.valueOf(alertText)); 
     System.out.println(alertText); 

} 
} 

答えて

0

this:

public String alertSystemMessage() throws IOException { 
    String url = "http://example.com/alertpage"; 
    Document document = Jsoup.connect(url).get(); 
    String alertText = document.select("p").first().text();   
    return alertText; 
} 
は、その後、あなたはそれを呼び出す方法では、このように、その値を保持する文字列変数を作成します。

AlertSystemDaemon alertsystemdaemonobject = new AlertSystemDaemon(); 
String someNameYouWant = null; 
try { 
    someNameYouWant = alertsystemdaemonobject.alertSystemMessage(); 
} catch (IOException e1) { 
    e1.printStackTrace(); 
} 
//use someNameYouWant at your will. 
+0

はどうもありがとうございました! – javajoejuan

関連する問題