我正在做一个家庭作业项目,其中向用户显示一个产品目录,然后用户从目录中选择一个产品进行购买。
我决定使用 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));
}
然后下一个组件仍然是一个只有两列的表格。当用户单击产品目录上的任何行时,系统会调用服务器来执行一些验证并计算含税的最终价格。然后第二个表中填充了与最终价格一起应用的各种税费。因此,在启动时,将填充产品目录表,并创建第二个表,但将其保留为空(当用户进行选择时填充)。代码片段:
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());
}
用户界面启动时:
正如您所看到的,第二个表在填充后不会调整大小。虽然表格有一个垂直滚动 Pane ,但从用户的角度来看这是一个不便。
你能帮我一下吗?我不确定这里到底出了什么问题。
最佳答案
如果您希望包含此数据的 Shell
调整大小,则需要在添加条目后调用 Shell
的 pack()
方法 table 。这将导致 shell 重新计算 shell 大小并重做组件布局。
关于java - SWT:表大小调整问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40743629/