java - 我可以使用Java中每个元素的代码创建动态网格视图吗?

我是 Java 新手,我正在研究 VB.NET,但它不适合我的新项目。
我正在尝试制作一个 POS 系统,我想问一个具体的问题。
如果我有一个 10 行的 DB 表,我需要将它们像网格视图一样放在 10 个面板中,
在 vb.net 中,我需要制作 10 个面板并为每个面板重复代码并使用隐藏和显示属性,这很累人如果表有更多行,则专业。
在PHP中,我只需要使用foreach语句来显示整个表格,无需重复任何代码。那么我可以在java中创建动态网格视图吗?
我到底应该在 Java 中学习什么?
谢谢你,抱歉我的语言不好。

http://img2.mukewang.com/6179ffae0001324a07140708.jpg

LEATH
浏览 121回答 1
1回答

米琪卡哇伊

这是我认为您正在尝试做的一个示例:import java.util.List;import java.util.stream.Collectors;import java.util.stream.IntStream;import javafx.application.Application;import javafx.geometry.Insets;import javafx.scene.Scene;import javafx.scene.control.Alert;import javafx.scene.control.Alert.AlertType;import javafx.scene.control.Button;import javafx.scene.control.Label;import javafx.scene.layout.Border;import javafx.scene.layout.BorderStroke;import javafx.scene.layout.BorderStrokeStyle;import javafx.scene.layout.GridPane;import javafx.scene.layout.Region;import javafx.scene.layout.VBox;import javafx.scene.paint.Color;import javafx.stage.Stage;import javafx.stage.Window;public class Main extends Application {&nbsp; @Override&nbsp; public void start(Stage primaryStage) {&nbsp; &nbsp; GridPane grid = new GridPane();&nbsp; &nbsp; grid.setPadding(new Insets(20));&nbsp; &nbsp; grid.setVgap(15);&nbsp; &nbsp; grid.setHgap(15);&nbsp; &nbsp; buildGrid(grid, getPanelNames());&nbsp; &nbsp; primaryStage.setScene(new Scene(grid));&nbsp; &nbsp; primaryStage.setTitle("Example App");&nbsp; &nbsp; primaryStage.show();&nbsp; }&nbsp; private void buildGrid(GridPane grid, List<String> panelNames) {&nbsp; &nbsp; int row = 0;&nbsp; &nbsp; int col = 0;&nbsp; &nbsp; for (String name : panelNames) {&nbsp; &nbsp; &nbsp; Label label = new Label(name);&nbsp; &nbsp; &nbsp; Button btn = new Button("Click Me!");&nbsp; &nbsp; &nbsp; btn.setOnAction(event -> {&nbsp; &nbsp; &nbsp; &nbsp; event.consume();&nbsp; &nbsp; &nbsp; &nbsp; showAlert(grid.getScene().getWindow(), name);&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; VBox box = new VBox(10, label, btn);&nbsp; &nbsp; &nbsp; box.setPadding(new Insets(10));&nbsp; &nbsp; &nbsp; box.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, null)));&nbsp; &nbsp; &nbsp; box.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);&nbsp; &nbsp; &nbsp; grid.add(box, col, row);&nbsp; &nbsp; &nbsp; if (++col > 3) {&nbsp; &nbsp; &nbsp; &nbsp; col = 0;&nbsp; &nbsp; &nbsp; &nbsp; row++;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; private List<String> getPanelNames() {&nbsp; &nbsp; return IntStream.rangeClosed(0, 13)&nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(i -> "Panel #" + i)&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp; }&nbsp; private void showAlert(Window owner, String panelName) {&nbsp; &nbsp; Alert alert = new Alert(AlertType.INFORMATION);&nbsp; &nbsp; alert.initOwner(owner);&nbsp; &nbsp; alert.setTitle(panelName);&nbsp; &nbsp; alert.setHeaderText(null);&nbsp; &nbsp; alert.setContentText("Hello from \"" + panelName + "\"!");&nbsp; &nbsp; alert.show();&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java