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);
その他のクラス:あなただけのクラスAlertSystemDaemon
にpublic String alertSystemMessage()
にあなたの方法public void alertSystemMessage()
を変更し、代わりにsysout
のalertText
あなたのようにそれを返す
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);
}
}
はどうもありがとうございました! – javajoejuan