package method;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;
//菲波拉契数列:
//0,1,1,2,3,5,8,13,21..........
public class FibonacciTest extends JApplet implements ActionListener ...{
JLabel numberLabel,resultLabel;
JTextField numberField,resultField;
public void init()...{
Container container = getContentPane();
container.setLayout(new FlowLayout());
numberLabel = new JLabel("Enter an integer and press Enter");
container.add(numberLabel);
numberField = new JTextField(10);
container.add(numberField);
numberField.addActionListener(this);
resultLabel = new JLabel("Fibonacci value is");
container.add(resultLabel);
resultField = new JTextField(15);
resultField.setEditable(false);
container.add(resultField);
}

public void actionPerformed(ActionEvent event) ...{
long number,fibonacciValue;
number = Long.parseLong(numberField.getText());
showStatus("Calcuating......");
fibonacciValue = fibonacci(number);
showStatus("Done");
resultField.setText(Long.toString(fibonacciValue));
}

private long fibonacci(long n) ...{
if(n == 0 || n == 1)
return n;
else
return fibonacci(n-1)+fibonacci(n-2);
}
}
这是一个使用Java Swing编写的JApplet应用程序,实现了用户输入整数后计算并显示菲波拉契数列对应数值的功能。通过JLabel、JTextField和ActionListener等组件,用户可以在界面中输入一个整数,程序会调用递归方法计算菲波拉契数列的值,并在结果字段中展示。
346

被折叠的 条评论
为什么被折叠?



