亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java - 我可以使用Java中每個元素的代碼創建動態網格視圖嗎?

java - 我可以使用Java中每個元素的代碼創建動態網格視圖嗎?

LEATH 2021-10-28 09:40:28
我是 Java 新手,我正在研究 VB.NET,但它不適合我的新項目。我正在嘗試制作一個 POS 系統,我想問一個具體的問題。如果我有一個 10 行的 DB 表,我需要將它們像網格視圖一樣放在 10 個面板中,在 vb.net 中,我需要制作 10 個面板并為每個面板重復代碼并使用隱藏和顯示屬性,這很累人如果表有更多行,則專業。在PHP中,我只需要使用foreach語句來顯示整個表格,無需重復任何代碼。那么我可以在java中創建動態網格視圖嗎?我到底應該在 Java 中學習什么?謝謝你,抱歉我的語言不好。
查看完整描述

1 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

這是我認為您正在嘗試做的一個示例:


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 {


  @Override

  public void start(Stage primaryStage) {

    GridPane grid = new GridPane();

    grid.setPadding(new Insets(20));

    grid.setVgap(15);

    grid.setHgap(15);


    buildGrid(grid, getPanelNames());


    primaryStage.setScene(new Scene(grid));

    primaryStage.setTitle("Example App");

    primaryStage.show();


  }


  private void buildGrid(GridPane grid, List<String> panelNames) {

    int row = 0;

    int col = 0;


    for (String name : panelNames) {

      Label label = new Label(name);

      Button btn = new Button("Click Me!");

      btn.setOnAction(event -> {

        event.consume();

        showAlert(grid.getScene().getWindow(), name);

      });


      VBox box = new VBox(10, label, btn);

      box.setPadding(new Insets(10));

      box.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, null, null)));

      box.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);

      grid.add(box, col, row);


      if (++col > 3) {

        col = 0;

        row++;

      }

    }


  }


  private List<String> getPanelNames() {

    return IntStream.rangeClosed(0, 13)

        .mapToObj(i -> "Panel #" + i)

        .collect(Collectors.toList());

  }


  private void showAlert(Window owner, String panelName) {

    Alert alert = new Alert(AlertType.INFORMATION);

    alert.initOwner(owner);

    alert.setTitle(panelName);

    alert.setHeaderText(null);

    alert.setContentText("Hello from \"" + panelName + "\"!");

    alert.show();

  }


}



查看完整回答
反對 回復 2021-10-28
  • 1 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號