繁体   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