使用按钮(GUI)运行JAVA程序

问题描述 投票:0回答:1

尽管进行了数小时的研究,但我整天都被困在这一天。以下是我的计划的基本概念。它从链接中提取数据并将其放入电子表格中,并且可以一次运行数小时。我的目的是将其连接到带有进度条的GUI。我正在尝试“上传”按钮,然后是“运行”按钮。

对于“运行”按钮,我无法弄清楚如何将按钮连接到运行以下程序的实例。

我试过推杆

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

在行动中执行没有运气。我收到以下错误:

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();
    }
}
}
java swing user-interface jsoup jxl
1个回答
1
投票

粗略猜测我会说编译器会提示你改变:

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

对于几种可能性中的一种,这种可能性基于'必须被捕获'(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

© www.soinside.com 2019 - 2024. All rights reserved.