运行 JAVA 带有按钮的程序 (GUI)

Running a JAVA Program with a Button (GUI)

尽管进行了数小时的研究,我一整天都被这个问题难住了。以下是我的程序的基本概念。它从链接中提取数据并将其放入电子表格中,一次可以 运行 数小时。我的目的是将其连接到带有进度条的 GUI。我正在尝试使用 "upload" 按钮,然后是 "Run" 按钮。

对于 "Run" 按钮,我不知道如何将按钮连接到 运行 以下程序的实例。

我试过

App obj= new App();
obj.main(null);

under actionPerformed 没有运气。我收到以下错误:

Error:(31, 25) java: unreported exception java.lang.Exception; 
    must be caught or declared to be thrown

我现在明白我们不能准确地调用主函数。但在那种情况下,我怎样才能使我的程序与 GUI 一起工作呢?这背后的主要原因是为了将来能够为它创建一个webapp,以便我可以在任何地方访问它。

public class App {

private static final int[] URL_COLUMNS = { 0, 4, 9 }; // Columns A, E, J
public static void main(final String[] args) throws Exception {

    Workbook originalWorkbook = Workbook.getWorkbook(new File("C:/Users/Shadow/Desktop/original.xls"));
    WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/Users/Shadow/Desktop/updated.xls"), originalWorkbook);
    originalWorkbook.close();
    WritableSheet sheet = workbook.getSheet(0);
    Cell cell;

    for (int i = 0; i < URL_COLUMNS.length; i++) {
        int currentRow = 1;
        while (!(cell = sheet.getCell(URL_COLUMNS[i], currentRow)).getType().equals(CellType.EMPTY)) {

            String url = cell.getContents();
            System.out.println("Checking URL: " + url);
            if (url.contains("scrapingsite1.com")) {
                String Price = ScrapingSite1(url);
                System.out.println("Scraping Site1's Price: " + Price);
                // save price into the next column
                Label cellWithPrice = new Label(URL_COLUMNS[i] + 1, currentRow, Price);
                sheet.addCell(cellWithPrice);
            }
            currentRow++;
        }
    }

    workbook.write();
    workbook.close();
}

private static String ScrapingSite1 (String url) throws IOException {
    Document doc = null;
    for (int i=1; i <= 6; i++) {
        try {
            doc = Jsoup.connect(url).userAgent("Mozilla/5.0").timeout(6000).validateTLSCertificates(false).get();
            break;
        } catch (IOException e) {
            System.out.println("Jsoup issue occurred " + i + " time(s).");
        }
    }
    if (doc == null){
        return null;
    }
    else{
        return doc.select("p.price").text();
    }
}
}

粗略地猜测,我会说编译器提示您更改:

App obj= new App();
obj.main(null);

几种可能性之一,这个基于'must be caught' (try / catch):

try {
    App obj= new App();
    obj.main(null);
} catch(Exception e) {
    e.printStackTrace(); // good fall back if logging not implemented
}

编辑:有关详细信息,请参阅 Java 教程的 Exceptions lesson