2016-06-27 2 views
1

Я пытаюсь получить класс main, чтобы получить содержимое alertText из другого класса, который сбрасывает веб-сайт, используя Jsoup. Сбой работает отлично, мне просто не удается получить переменную alertText, чтобы не ошибка. Я пытаюсь получить вызов основного класса в классе AlertSystemDaemon, чтобы заполнить содержимое JLabel jLabelAlertSystem. Я пробовал использовать jLabelAlertSystem.setText(String.valueOf(alertText));Пытается получить разновидность из другого класса

но это будет ошибка jsut на 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); 

Другое Класс:

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

Вы просто изменить метод public void alertSystemMessage() к public String alertSystemMessage() в классе AlertSystemDaemon и вместо sysoutalertText вы возвращаете его как это:

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

Смежные вопросы