必须在JavaFX技术框架下做,不可采用AWT或Swing技术。
编写 GUI 程序。当用户在界面上输入任一个正整数 n 后,界面可显示 Fibonacci (斐波那契)数列第 n 项的值。
package com.wf.fx.demo01;
import javafx.application.Application;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.kordamp.bootstrapfx.scene.layout.Panel;
import java.util.Arrays;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) {
Panel panel = new Panel();
Label label = new Label();
TextField textField = new TextField();
Button button = new Button("计算");
button.addEventHandler(EventType.ROOT,event -> {
label.setText(Arrays.toString(getF(Integer.parseInt(textField.getText()))));
});
panel.setBody(textField);
panel.setFooter(button);
panel.setHeading(label);
Scene scene = new Scene(panel, 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
public static int[] getF(int value) {
int n = value;
int[] a = new int[n];
a[0] = 1;
if (n > 1) {
a[1] = 1;
for (int i = 2; i < a.length; i++) {
a[i] = a[i - 1] + a[i - 2];
}
}
return a;
}
}