You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX替换TextField为TextArea时GridPane布局错乱求助

解决JavaFX GridPane布局错位与TextArea正确使用问题

我来帮你搞定这个布局错位和arp结果显示的问题~咱们一步步调整代码:

核心问题分析

  1. 原来的TextField只能单行显示,换成TextArea后如果不调整GridPane的布局约束,会因为控件尺寸和列宽不匹配导致错位;
  2. 原代码中用setText()每次覆盖内容,只会显示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.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
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
        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
        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为TextArea,设置合适的行/列数
        TextArea WifiInfo = new TextArea();
        WifiInfo.setPrefRowCount(8); // 设置显示行数
        WifiInfo.setPrefColumnCount(50); // 设置列数,控制宽度
        WifiInfo.setWrapText(true); // 自动换行,避免横向滚动
        // 添加:让TextArea跨3列显示,避免布局拥挤错位
        GridPane.setConstraints(WifiInfo, 1, 4);
        GridPane.setColumnSpan(WifiInfo, 3);

        WifiButton.setOnAction(event -> {
            try {
                // 添加:每次查询前清空TextArea内容
                WifiInfo.clear();
                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";
                    }
                    // 修改:用appendText累积内容,而不是setText覆盖
                    WifiInfo.appendText(ip);
                }
            } 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, 450); // 适当加大窗口高度,容纳TextArea
        scene.getStylesheets().add("colour.css");
        window.setScene(scene);
        window.show();
    }
}

关键修改点说明

  • 替换为TextArea:设置prefRowCount和prefColumnCount来定义控件的初始尺寸,开启wrapText让内容自动换行;
  • GridPane列跨度:通过GridPane.setColumnSpan(WifiInfo, 3)让TextArea横跨3列,避免在窄列中挤压导致布局错位;
  • 结果累积显示:用appendText()替代setText(),确保arp命令的所有结果都能显示,同时每次查询前clear()内容,避免重复输出;
  • 窗口尺寸调整:适当加大窗口高度,给TextArea足够的显示空间。

内容的提问来源于stack exchange,提问作者aacm14

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:18:53