【问题标题】:Calculate Fibonacci Method using GUI使用 GUI 计算斐波那契方法
【发布时间】:2016-09-01 05:54:20
【问题描述】:

我正在尝试使用斐波那契方法绑定到按钮,该方法将使用滑块作为输入值,然后在文本字段中打印斐波那契数。我在将值从我的斐波那契方法传递到文本字段时遇到了麻烦。任何帮助,将不胜感激。这是我到目前为止所写的:

public class Main extends Application {

    private static Slider fibSlider = new Slider(0, 10, 0);
    private static Label indexLabel = new Label();
    private static Label fibNumLabel = new Label();
    private static int colIndex = 0;
    private static int rowIndex = 0;
    private static int topIndex = 0;
    private static int rightIndex = 0;
    private static int leftIndex = 0;
    private static int bottomIndex = 0;
    private static TextField tfIndex;
    private static TextField tfFibNum;
    private static Button butCalcFib = new Button();

    @Override
    public void start(Stage primaryStage) {

        fibSlider.setMajorTickUnit(1);
        fibSlider.setMinorTickCount(0);
        fibSlider.setShowTickLabels(true);
        fibSlider.setShowTickMarks(true);
        fibSlider.setSnapToTicks(true);

        fibSlider.valueProperty().addListener(sl -> {
            tfIndex.setText(Double.toString(fibSlider.getValue()));
        });

        indexLabel.setText("Index: ");
        fibNumLabel.setText("Fibonacci #: ");
        butCalcFib.setText("Calculate Fibonacci");
        //tfFibNum.setText(long.toString(ComputeFibonacci()));

        fibSlider.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                if (newValue == null) {
                    tfIndex.setText("");
                    return;
                }
                tfIndex.setText(Math.round(newValue.intValue()) + "");
            }
        });

        GridPane mainGPane = buildGPane();
        Scene mainScene = new Scene(mainGPane, 500, 200);

        primaryStage.setTitle(" - Fibonacci Calculator");
        primaryStage.setScene(mainScene);
        primaryStage.show();

    }

    private GridPane buildGPane() {
        GridPane gPane = new GridPane();
        gPane.setAlignment(Pos.CENTER);
        gPane.setPadding(new Insets(topIndex = 10, rightIndex = 10,
            bottomIndex = 10, leftIndex = 10));
        gPane.setHgap(2);
        gPane.setVgap(2);
        gPane.add(fibSlider, colIndex = 1, rowIndex = 3);
        gPane.add(indexLabel, colIndex = 1, rowIndex = 5);
        gPane.add(tfIndex, colIndex = 2, rowIndex = 5);
        gPane.add(butCalcFib, colIndex = 1, rowIndex = 6);
        gPane.add(fibNumLabel, colIndex = 1, rowIndex = 7);
        gPane.add(tfFibNum, colIndex = 2, rowIndex = 7);

        return gPane;
    }

    public Main() {
        tfIndex = new TextField();
        tfFibNum = new TextField();
    }

    public static void ComputeFibonacci() {
        double index = fibSlider.getValue();
        // Find and display the Fibonacci number
        fib((long) index);
    }

    /**
     * The method for finding the Fibonacci number
     */
    public static long fib(long index) {
        if (index == 0) // Base case
        {
            return 0;
        } else if (index == 1) // Base case 
        {
            return 1;
        } else // Reduction and recursive calls 
        {
            return fib(index - 1) + fib(index - 2);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

【问题讨论】:

    标签: java recursion javafx fibonacci


    【解决方案1】:

    您可以通过让滑块的侦听器更新两个索引结果字段来大大简化您的代码。那么你只需要一个滑块监听器,不需要按钮。

        fibSlider.valueProperty().addListener(sl -> {
            long value = (long) fibSlider.getValue();
            tfIndex.setText(Long.toString(value));
            tfFibNum.setText(Long.toString(fib(value)));
        });
    

    另见Q&A 关于观察者模式。在下面的变体中,

    • GridPane 允许滑块跨越两列。

    • 调用滑块的setBlockIncrement() 方法可让您使用箭头键更改滑块。

    • 在滑块的InvalidationListener 中,您可以引用滑块的DoubleProperty 值,而不是引用封闭类的fibSlider 字段。

    经测试:

    import javafx.application.Application;
    import javafx.beans.property.DoubleProperty;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.Slider;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.GridPane;
    import javafx.stage.Stage;
    
    /** @see https://stackoverflow.com/a/37068554/230513 */
    public class Main extends Application {
    
        private final Slider fibSlider = new Slider(0, 10, 0);
        private final Label indexLabel = new Label("Index: ");
        private final Label fibNumLabel = new Label("Fibonacci #: ");
        private final TextField tfIndex = new TextField("0");
        private final TextField tfFibNum = new TextField("0");
    
        @Override
        public void start(Stage primaryStage) {
            fibSlider.setMajorTickUnit(1);
            fibSlider.setMinorTickCount(0);
            fibSlider.setShowTickLabels(true);
            fibSlider.setShowTickMarks(true);
            fibSlider.setSnapToTicks(true);
            fibSlider.setBlockIncrement(1.0);
            fibSlider.valueProperty().addListener(sl -> {
                long value = ((DoubleProperty) sl).longValue();
                tfIndex.setText(Long.toString(value));
                tfFibNum.setText(Long.toString(fib(value)));
            });
    
            GridPane mainGPane = buildGPane();
            Scene mainScene = new Scene(mainGPane);
            primaryStage.setTitle("Fibonacci Calculator");
            primaryStage.setScene(mainScene);
            primaryStage.show();
        }
    
        private GridPane buildGPane() {
            GridPane gPane = new GridPane();
            gPane.setAlignment(Pos.CENTER);
            gPane.setPadding(new Insets(10, 10, 10, 10));
            gPane.setHgap(10);
            gPane.setVgap(10);
            gPane.add(fibSlider, 0, 0, 2, 1);
            gPane.add(indexLabel, 0, 1);
            gPane.add(tfIndex, 1, 1);
            gPane.add(fibNumLabel, 0, 2);
            gPane.add(tfFibNum, 1, 2);
            return gPane;
        }
    
        /**
         * The method for finding the Fibonacci number
         */
        public long fib(long index) {
            if (index == 0) {
                return 0;
            } else if (index == 1) {
                return 1;
            } else {
                return fib(index - 1) + fib(index - 2);
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

    • 感谢您的彻底回复。这正是我遇到麻烦的事情。至于简化我的代码,我知道我可以像您显示的那样更新两个字段,但是我想要一个按钮,一旦单击它就会更新斐波那契数;这就是我用按钮编写它的原因。
    • @Jake:您可以添加自己的EventHandlerbutCalcFib.setOnAction(…),用于example;如果您仍有问题,请更新您的问题。
    • 如何设置按钮的动作来调用fib方法并显示在tfFibNum文本域中?
    • 我不会,因为它没有明显的效果;相反,向tfIndex 添加一个侦听器,以便在用户键入时更新tfFibNum;见textProperty().addListener(),对于example
    猜你喜欢
    • 2013-03-31
    • 2020-01-18
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 2012-12-03
    • 2016-08-04
    • 1970-01-01
    • 2016-09-11
    相关资源
    最近更新 更多