使用TextField时在JavaFX中添加文本区域

时间:2018-05-24 18:36:04

标签: java javafx

我之前提到过,由于最后一年的大学项目,我才回到JavaFX。我说每个人都创建了一个IP / Mac地址查找器。作为其中的一部分,我将运行arp命令并从arp命令返回信息。我遇到了一个问题,文本字段太小,无法返回所有arp信息。当我放入文本区域时,我的gridpane被重新配置并且未对齐。是否有人能够建议如何添加文本区域?我正在谈论的领域是**部分

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();
    }

}

2 个答案:

答案 0 :(得分:0)

尝试将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成为GridPane并将TextArea添加到其中。您可以在TextArea上设置填充或边距。

enter image description here

答案 1 :(得分:0)

我们可以指定rowpan和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();