import javax.swing.*; import java.awt.*; import java.awt.event.*; /* */ public class Hanoi extends JApplet implements ActionListener { JTextField input; JTextArea output; @Override public void init() { input = new JTextField("0", 8); input.addActionListener(this); output = new JTextArea(18, 22); setLayout(new FlowLayout()); add(input); add(output); } void hanoi(int n, String a, String b, String c) { if (n == 1) { output.append("円盤 1を、" + a + "から" + b + "へ。\n"); } else { hanoi(n - 1, a, c, b); output.append("円盤 " + n + "を、" + a + "から" + b + "へ。\n"); hanoi(n - 1, c, b, a); } } public void actionPerformed(ActionEvent e) { int n = Integer.parseInt(input.getText()); hanoi(n, "棒A", "棒B", "棒C"); output.append("以上\n\n"); } public static void main(String[] args) { JFrame frame = new JFrame(""); JApplet applet = new Hanoi(); applet.setPreferredSize(new Dimension(400, 350)); frame.add(applet); frame.pack(); frame.setVisible(true); applet.init(); applet.start(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }