我的Javafx按钮方法不起作用,不知道为什么

问题描述 投票:0回答:1

我没有异常/错误,只是没有试图激活按钮的结果。当我点击按钮没有任何反应时,所以,我认为它是按钮鼠标单击方法触发中的某种打嗝或执行正在进行int解析的方法的问题。我有一个负责完成任务的课程

“给定两个字符串数组a1和a2以a1的字符串的字典顺序返回一个排序数组r,这些字符串是a2的字符串的子串。请注意:r必须没有重复。”

它在控制台上运行良好,但现在我将控制台切换到JavaFX场景构建器GUI。我不知道为什么我不能使这个代码工作。这是我的Array类中的代码。

public class Array_Shenanigans {
    public int size;
    SampleController cont = new SampleController();
    public String[] a1, a2, r;

    public int first_array_size() {
        try {
            size = Integer.parseInt(cont.input.getText());
        } catch (NumberFormatException e) {
            cont.labelOutput.setText("Please, input a proper size of the first array");
            return size = 0;
        }
        return size;
    }

    public void first_array_input() {
        a1 = new String[size];
        for (int i = 0; i != size; i++) {
            cont.labelOutput.setText("Input the array element number " + (i + 1));
            a1[i] = cont.input.getText();
        }
    }

    public int second_array_size() {
        try {
            size = Integer.parseInt(cont.input.getText());
        } catch (NumberFormatException e) {
            cont.labelOutput.setText("Please, input a proper size of the first array");
            return size = 0;
        }
        return size;
    }

    public void second_array_input() {
        a2 = new String[size];
        for (int i = 0; i != size; i++) {
            cont.labelOutput.setText("Input the array element number " + (i + 1));
            a2[i] = cont.input.getText();
        }
    }

    public void uber_array_creation() {
        ArrayList r1 = new ArrayList();
        for (int i = 0; i != a1.length; i++) {
            for (int j = 0; j != a2.length; j++) {
                if (a2[j].contains(a1[i])) {
                    r1.add(a1[i]);
                }
            }
        }
        Set<String> set = new HashSet<>(r1);
        r1.clear();
        r1.addAll(set);
        r = (String[]) r1.toArray(new String[r1.size()]);
    }

    public void uber_array_sort() {
        Arrays.sort(r);
    }


    public void uber_array_output() {
        String s = "";
        for (int i = 0; i < r.length; i++) {
            s = r[i] + " ";
            cont.labelOutput.setText(s);
        }
    }
}

我的fxml文件就像这样

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

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
            prefWidth="700.0" style="-fx-background-color: #2E3348;" xmlns="http://javafx.com/javafx/8"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.SampleController">
    <children>
        <AnchorPane layoutY="87.0" prefHeight="313.0" prefWidth="700.0" style="-fx-background-color: #fafafa;">
            <children>
                <Button fx:id="countButton" layoutX="579.0" layoutY="125.0" mnemonicParsing="false"
                        onAction="#countButtonAction" prefHeight="25.0" prefWidth="74.0" text="Count"/>
                <Button layoutX="579.0" layoutY="157.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="74.0"
                        text="Save"/>
                <Button layoutX="579.0" layoutY="190.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="74.0"
                        text="Load"/>
                <TextField fx:id="input" alignment="CENTER_RIGHT" layoutX="526.0" layoutY="77.0"
                           promptText="Input goes here"/>
                <ComboBox fx:id="cmbB" layoutX="14.0" layoutY="52.0" onAction="#comboChanged" prefWidth="150.0"
                          promptText="Choose the task "/>
                <Label fx:id="taskLabel" layoutX="14.0" layoutY="85.0" prefHeight="130.0" prefWidth="236.0"/>
                <Label fx:id="labelOutput" layoutX="420.0" layoutY="36.0" prefHeight="33.0" prefWidth="255.0"/>
            </children>
        </AnchorPane>
        <Label layoutX="245.0" layoutY="14.0" prefHeight="43.0" prefWidth="334.0" text="Proverochka" textFill="WHITE">
            <font>
                <Font name="Comic Sans MS" size="39.0"/>
            </font>
        </Label>
    </children>
</AnchorPane>

Controller类就在这里,来自Array类的方法是通过将String解析为int而不是通过控制台的常规输入从TextField“input”获取信息,并将消息放入Label“labelOutput”而不是system.out.Println()

public class SampleController implements Initializable, EventHandler<ActionEvent> {

    public Label taskLabel = new Label();
    public Label labelOutput = new Label();
    public TextField input = new TextField();
    public Button countButton = new Button();

    public void countButtonAction(ActionEvent event) {
        Array_Shenanigans array = new Array_Shenanigans();
        if ((labelOutput.getText() == "Input the size of the first array") || (labelOutput.getText() == "Please, input a proper size of the first array")) {
            array.first_array_size();
            if (array.size == 0)
                return;
            array.first_array_input();
            labelOutput.setText("Input the size of the second array");
        }

        if (labelOutput.getText() == "Input the size of the second array") {
            array.second_array_size();
            if (array.size == 0)
                return;
            array.second_array_input();
            array.uber_array_creation();
            array.uber_array_sort();
            array.uber_array_output();
        }
    }

    public ComboBox<String> cmbB;
    ObservableList<String> list = FXCollections.observableArrayList("Task 1 Arrays", "Task 2 Exp. numbers");

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        cmbB.setItems(list);
    }

    public void comboChanged(ActionEvent event) {
        if (cmbB.getValue() == "Task 1 Arrays") {
            taskLabel.setText("Given two arrays of strings a1 and a2" + "\n" + "return a sorted array r in lexicographical" + "\n" + "order of the strings of a1 which are" + "\n" + "substrings of strings of a2." + "\n" + "\n" + "Beware: r must be without duplicates.");
            labelOutput.setText("Input the size of the first array");
        }
        if (cmbB.getValue() == "Task 2 Exp. numbers") {
            taskLabel.setText("Write Number in Expanded Form. You will" + "\n" + "be given a number and you will need to" + "\n" + "return it as a string inExpanded Form. " + "\n" + "\n" + "NOTE: All numbers will be whole numbers" + "\n" + "greater than 0.");
            labelOutput.setText("Input the number to expand");
        }
    }

    @Override
    public void handle(ActionEvent event) {

    }   
}

所有进口都应该没问题,不包括它们以节省空间。主类在这里。

public class Main extends Application {
    @Override


    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/sample.fxml"));
        primaryStage.setTitle("Test task");
        primaryStage.setScene(new Scene(root, 700, 400));
        primaryStage.show();
        primaryStage.setMaxHeight(400);
        primaryStage.setMaxWidth(700);
        primaryStage.setMinHeight(400);
        primaryStage.setMinWidth(700);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
java javafx scenebuilder
1个回答
0
投票

你的控制器skelton应该是

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class SampleController {

    @FXML
    private Button countButton;

    @FXML
    private TextField input;

    @FXML
    private ComboBox<String> cmbB;

    @FXML
    private Label taskLabel;

    @FXML
    private Label labelOutput;

    ObservableList<String> list = FXCollections.observableArrayList("Task 1 Arrays", "Task 2 Exp. numbers");



@Override
public void initialize(URL location, ResourceBundle resources) {
    cmbB.setItems(list);
}

@FXML
public void comboChanged(ActionEvent event) {
    if (cmbB.getValue() == "Task 1 Arrays") {
        taskLabel.setText("Given two arrays of strings a1 and a2" + "\n" + "return a sorted array r in lexicographical" + "\n" + "order of the strings of a1 which are" + "\n" + "substrings of strings of a2." + "\n" + "\n" + "Beware: r must be without duplicates.");
        labelOutput.setText("Input the size of the first array");
    }
    if (cmbB.getValue() == "Task 2 Exp. numbers") {
        taskLabel.setText("Write Number in Expanded Form. You will" + "\n" + "be given a number and you will need to" + "\n" + "return it as a string inExpanded Form. " + "\n" + "\n" + "NOTE: All numbers will be whole numbers" + "\n" + "greater than 0.");
        labelOutput.setText("Input the number to expand");
    }
}    
    @FXML
public void countButtonAction(ActionEvent event) {
    Array_Shenanigans array = new Array_Shenanigans();
    if ((labelOutput.getText() == "Input the size of the first array") || (labelOutput.getText() == "Please, input a proper size of the first array")) {
        array.first_array_size();
        if (array.size == 0)
            return;
        array.first_array_input();
        labelOutput.setText("Input the size of the second array");
    }

    if (labelOutput.getText() == "Input the size of the second array") {
        array.second_array_size();
        if (array.size == 0)
            return;
        array.second_array_input();
        array.uber_array_creation();
        array.uber_array_sort();
        array.uber_array_output();
    }

}

}

你的fxml界面看起来像enter image description here

了解什么是@FXML注释read this

© www.soinside.com 2019 - 2024. All rights reserved.