//These are some of the standard "imports" we'll be using, explained in class import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; public class PowersMethod extends Applet implements ActionListener { JTextArea output; JTextField base, exponent; JButton button; public void init() { setLayout(new BorderLayout()); // Create the text-area and button we'll use base = new JTextField(8); exponent = new JTextField(8); output = new JTextArea(); button = new JButton("Compute Power!"); // Next we create a "panel" that we add button to JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.add(new JLabel("base: ")); p.add(base); p.add(new JLabel("exponent: ")); p.add(exponent); p.add(button); // Finally, to finish building the UI, we simply add the text and the panel // (which contains the button) to the applet add("Center", new JScrollPane(output)); add("South", p); // Lastly, we instruct the applet to "listen" for the button to be pressed button.addActionListener(this); } int power(int base, int exponent) { int answer = 1; for (int i = 0; i < exponent; i++) { answer = answer * base; } return answer; } public void actionPerformed(ActionEvent evt) { // Get the base and exponent from input, convert to numbers int iBase = Integer.parseInt(base.getText()); int iExponent = Integer.parseInt(exponent.getText()); // Display the result... output.append(iBase + "^" + iExponent + " = " + power(iBase, iExponent) + "\n"); } }