簡體   English   中英

使用 TextField 時在 JavaFX 中添加文本區域

[英]Adding Text Area in JavaFX when using TextField

我之前提到過,由於最后一年的大學項目,我才回到 JavaFX。 我已經創建了一個 IP / Mac 地址查找器。 作為其中的一部分,我將運行 arp 命令並返回來自 arp 命令的信息。 我遇到了文本字段對於返回的所有 arp 信息來說太小的問題。 當我放入文本區域時,我的網格窗格被重新配置和未對齊。 有人能建議如何添加文本區域嗎? 我說的區域在**部分

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

    Stage window;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setTitle("Wi-FI Connection Checker");

        //GridPane with 10px padding around edge
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10, 10, 10, 10));
        grid.setVgap(10);
        grid.setHgap(10);

        //Name Label - constrains use (child, column, row)
        Label nameLabel = new Label("Find my IP Address:");
        nameLabel.setId("bold-label");
        GridPane.setConstraints(nameLabel, 0, 0);

        //Search IP Address 
        Button IPlookupButton =new Button("Search for IP");
        GridPane.setConstraints(IPlookupButton, 3, 0);

        //Name Input
        TextField nameInput = new TextField();
        nameInput.setPromptText("IP Address");
        GridPane.setConstraints(nameInput, 1, 0);

        IPlookupButton.setOnAction(event -> {
            try {
                InetAddress thisIp = InetAddress.getLocalHost();
                nameInput.setText("IP:" + thisIp.getHostAddress());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        //MAC Address Lookup Label
        Label passLabel = new Label("MAC Address Look UP:");
        GridPane.setConstraints(passLabel, 0, 1);

        //MAC Address Input
        TextField passInput = new TextField();
        passInput.setPromptText("MAC Address");
        GridPane.setConstraints(passInput, 1, 1);

        //Search MAC Address 
        Button MacAddressButton =new Button("Search for MAC Address");
        GridPane.setConstraints(MacAddressButton, 3, 1);


        //MAC Address Input
        TextField MacFound = new TextField();
        GridPane.setConstraints(MacFound, 1, 2);


        //MAC Vendor Label
        Label vendorLabel = new Label("Manufacturer Make:");
        GridPane.setConstraints(vendorLabel, 0, 2);

        //MAC Address Search 
        MacAddressButton.setOnAction(event -> {
            try {

            String MacAddress = passInput.getText();


            URL oracle = new URL("https://api.macvendors.com/"+ MacAddress);
            BufferedReader in = new BufferedReader(
            new InputStreamReader(oracle.openStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null)
                MacFound.setText(inputLine);
                MacFound.setPrefWidth(MacFound.getText().length() * 7);
            in.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
            }
        );

        //Wi-Fi Connection Button
        Button WifiButton = new Button("Search my Wi-Fi");
        GridPane.setConstraints(WifiButton, 1, 3);

        TextField WifiInfo = new TextField();
        //textArea.setPrefRowCount(4);
        GridPane.setConstraints(WifiInfo, 1, 4);

        **WifiButton.setOnAction(event -> {

            try {
                Process process = Runtime.getRuntime().exec("arp -a");
                process.waitFor();
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                int i = 0;
                while (reader.ready()) {
                    i++;
                    String ip = reader.readLine();
                    if (i >= 4) {
                        ip = ip.substring(2, 56) + "\n";
                    }
                    WifiInfo.setText(ip);
                    WifiInfo.setPrefWidth(WifiInfo.getText().length() * 7);
                }
            } catch (IOException | InterruptedException ioe) {
                ioe.printStackTrace();
            }**

        });

        //Add everything to grid
        grid.getChildren().addAll(nameLabel, IPlookupButton, nameInput, passLabel, passInput,MacAddressButton, MacFound, vendorLabel, WifiButton, WifiInfo);

        Scene scene = new Scene(grid, 600, 300);
        scene.getStylesheets().add("colour.css");
        window.setScene(scene);
        window.show();
    }

}

嘗試將您的 WinfiInfo 更改為TextArea

TextArea WifiInfo = new TextArea();

然后相應地添加/更改代碼。

grid.getChildren().addAll(nameLabel, IPlookupButton, nameInput, passLabel, passInput, MacAddressButton, MacFound, vendorLabel, WifiButton);

VBox root = new VBox(grid, WifiInfo);

Scene scene = new Scene(root, 600, 300);

此代碼使根成為VBox並向其添加GridPaneTextArea 您可以在TextArea上設置填充或邊距。

在此處輸入圖片說明

我們可以指定 rowspan 和 colspan

  TextArea dataFld = new TextArea();
   //column=0, row=5, colspan=3, rowspan=3
  gridPane.add(dataFld, 0, 5, 3, 3);

  //Creating a scene object 
  Scene scene = new Scene(gridPane);
  
  //Setting title to the Stage 
  stage.setTitle("Grid Pane Example"); 
     
  //Adding scene to the stage 
  stage.setScene(scene); 
     
  //Displaying the contents of the stage 
  stage.show(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM