有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSWT:表大小调整问题

我正在做一个家庭作业项目,其中向用户显示一个产品目录,然后用户从目录中选择一个产品进行购买

我决定使用SWT构建一个基本的UI。更不用说我刚刚开始学习SWT

这就是我到目前为止所做的。UI的第一个组件是Table,它显示产品目录。以下代码片段:

private void displayProductCatalog(List<Product> productList) {
        Group group = new Group(shell, SWT.NULL);
        group.setLayout(new GridLayout());
        Label label = new Label(group, SWT.NULL);
        label.setAlignment(SWT.CENTER);
        label.setText("Plese select a product by clicking on the desired row.");

        Table table = new Table(group, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
        table.setLayoutData(data);
        String[] titles = { "Product ID", "Product Description", "Cost" };
        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(table, SWT.BOLD | SWT.CENTER);
            column.setText(titles[i]);
            column.setWidth(300);
        }

        String currency = " " + CurrencyHelper.fetchCurrency();

        for (Product product : productList) {
            ProductDescription productDescription = product.getProductDescription();
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(0, productDescription.getProductId());
            item.setText(1, productDescription.getDescription());
            item.setText(2, productDescription.getPrice().toString() + currency);
        }

        table.addSelectionListener(new TableRowSelectionListener(vendingMachine));
    }

然后,下一个组件又是一个只有两列的表。当用户单击产品目录上的任何一行时,会调用服务器执行一些验证,并计算含税的最终价格。然后,第二个表中填入了各种税收,以及最终的价格,这是。所以在启动时,会填充product catalog表,创建第二个表,但保留为空(当用户进行选择时填充)。代码片段:

private void displaySaleLineItem(List<Product> productList) {
        Group group = new Group(shell, SWT.NULL);
        group.setLayout(new GridLayout());
        Label label = new Label(group, SWT.NULL);
        label.setAlignment(SWT.CENTER);
        label.setText("Product Details.");

        saleLineItemTable = new Table(group, SWT.BORDER);
        saleLineItemTable.setLinesVisible(true);
        saleLineItemTable.setHeaderVisible(true);
        // GridData data = new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1);
        // saleLineItemTable.setLayoutData(data);
        for (int i = 0; i < 2; i++) {
            TableColumn column = new TableColumn(saleLineItemTable, SWT.BOLD | SWT.CENTER);
            column.setWidth(450);
        }
    }

填充第二个表的代码段:

@Override
    public void onPropertyEventBeforeSale(Sale sale) {
        TableItem item = new TableItem(saleLineItemTable, SWT.NONE);
        item.setText(0, sale.getProduct().getProductDescription().getDescription());
        item.setText(1, sale.getProduct().getProductDescription().getPrice().toString());

        for (TaxTypeModel taxTypeModel : sale.getTaxModel().getTaxTypeModelList()) {
            item = new TableItem(saleLineItemTable, SWT.NONE);
            item.setText(0, taxTypeModel.getTaxName());
            item.setText(1, taxTypeModel.getTaxValue().toString() + "%");
        }

        item = new TableItem(saleLineItemTable, SWT.NONE);
        item.setText(0, "TOTAL");
        item.setText(1, sale.getTaxModel().getProductPriceIncludingTax().toString());
    }

在UI启动时:

enter image description here

当用户选择产品时: enter image description here

如您所见,填充表时,第二个表不会调整大小。虽然表格有一个垂直滚动窗格,但从用户的角度来看,这是一个不便

你能帮帮我吗。我不确定这里到底出了什么问题


共 (0) 个答案