gpt4 book ai didi

java - 自制端口扫描仪无法与 JavaFX 一起使用

转载 作者:行者123 更新时间:2023-12-02 03:24:17 25 4
gpt4 key购买 nike

所以我用 Java 创建了一个端口扫描器。

使用 Swing 时,一切顺利,错误为零。

但是,我尝试将我的应用程序转换为 JavaFX 风格的 GUI,但当我按下按钮时没有任何效果(即使我已在 GUI 构建器中设置了方法 ID)。

enter image description here

我的问题是什么?

我的代码:

package networkTools.gui;

import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class PortScannerController implements Initializable {
@FXML
TextField hostName;
@FXML
TextField fromPort;
@FXML
TextArea log;
@FXML
Button scan;
@FXML
Button reset;
@FXML
Label label;


@Override
public void initialize(URL url, ResourceBundle rb) {
hostName.requestFocus();
}

public void initFocus() {
hostName.requestFocus();
}

@FXML
private void onScan() {
int fp;
String h;
Socket s;
if (hostName.getText().equals("")) {
log.setText("Fill everything correct.");
return;
} else if (fromPort.getText().equals("")) {
log.setText("Fill everything correct..");
return;
} else if (!fromPort.getText().matches("[0-9]*")) {
log.setText("Give a number for a port ");
return;
}
// scan.disable(false);
reset.setText("Stop");
log.setText("");
log.clear();
label.setText("");
h = hostName.getText();
fp = Integer.parseInt(fromPort.getText());

label.setText("Port " + fp + " being tested (max +- 15 sec.)");

try {
s = new Socket(h, fp);
log.appendText("Poort " + fp + " is open.\n");
log.clear();
s.close();
} catch (Exception er) {
log.appendText("Poort " + fp + " is closed");
}

// scan.setEnabled(true);
reset.setText("Reset");
label.setText("Press scan to start.");

}

}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<VBox id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="400.0" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="networkTools.gui.PortScannerController">
<children>
<StackPane prefHeight="80.0" prefWidth="600.0">
<children>
<Label id="title" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="50.0" prefWidth="400.0" text="Port Scanner" StackPane.alignment="CENTER">
<font>
<Font name="Arial Black" size="18.0" />
</font>
</Label>
</children>
</StackPane>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="131.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="407.0" minWidth="10.0" prefWidth="269.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="100.0" text="Host Name">
<font>
<Font name="Arial Black" size="12.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="100.0" text="Port" GridPane.rowIndex="1">
<font>
<Font name="Arial Black" size="12.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<TextField fx:id="fromPort" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
<font>
<Font name="Arial Black" size="12.0" />
</font>
</TextField>
<ToggleButton id="commandbutton" fx:id="onScan" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="Scan" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">
<font>
<Font name="Arial Black" size="12.0" />
</font>
</ToggleButton>
<ToggleButton id="commandbutton" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="Reset" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">
<font>
<Font name="Arial Black" size="12.0" />
</font>
</ToggleButton>
<TextField fx:id="hostName" GridPane.columnIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
<font>
<Font name="Arial Black" size="12.0" />
</font>
</TextField>
</children>
</GridPane>
<TextArea id="textpane" fx:id="log" editable="false" prefHeight="148.0" prefWidth="600.0">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</TextArea>
<StackPane prefHeight="80.0" prefWidth="380.0" VBox.vgrow="NEVER">
<VBox.margin>
<Insets bottom="10.0" left="20.0" />
</VBox.margin>
<children>
<Label alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="200.0" text="Press Scan to start" textFill="#1815c9">
<font>
<Font name="Arial Black" size="13.0" />
</font>
</Label>
</children>
</StackPane>
</children>
</VBox>

最佳答案

没有任何地方可以将处理程序与按钮关联起来。您需要在 FXML 中为“扫描”按钮添加 onAction="#onScan":

<ToggleButton id="commandbutton" onAction="#onScan" fx:id="onScan" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="Scan" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">

您的 FXML 中还有其他一些看起来不正确的内容:

  1. 您有多个具有相同 CSS id 的元素(例如 "commandButton");如果您想在多个组件之间共享样式,则应该使用 styleClass 而不是 id
  2. 您为上面的按钮声明了一个 fx:id="onScan",但您的 Controller 中没有名为 onScan 的字段。
  3. 不太清楚为什么要使用 ToggleButton 而不是常规的 Button 来执行看似操作的操作。 ToggleButton 通常用于在两种不同状态之间切换,而不是发出命令/操作。

关于java - 自制端口扫描仪无法与 JavaFX 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39196576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com