import javax.swing.*; import java.awt.*; import java.awt.event.*; /* */ public class Factorial extends JApplet implements ActionListener { JTextField input; JLabel output; @Override public void init() { input=new JTextField("0", 8); output=new JLabel(" 1"); input.addActionListener(this); setLayout(new FlowLayout()); add(input); add(new JLabel("の階乗は")); add(output); add(new JLabel("です。")); } static int factorial(int n) { // factorial -- 階乗のこと int r = 1; for (; n>0; n--) { r *= n; } return r; } public void actionPerformed(ActionEvent e) { try { int n = Integer.parseInt(input.getText()); output.setText(" "+factorial(n)); } catch (NumberFormatException ex) { input.setText("数値!"); } } }