Java中Applet小程序的编写

2024-11-21 22:05:38
推荐回答(1个)
回答1:

import javax.swing.*;import java.awt.*;import java.awt.event.*;/** * @author Hardneedl */public class NumberApplet extends JApplet { private JTextField n1Field,n2Field; private JTextArea outArea; public NumberApplet() throws HeadlessException { Container container = getContentPane(); container.setLayout(new GridLayout(0,1,2,2)); container.add(n1Field = new JTextField(10)); container.add(n2Field = new JTextField(10)); container.add(new JButton(new ComputeAction())); container.add(new JScrollPane(outArea = new JTextArea(){ public boolean isEditable() {return false;} })); } private class ComputeAction extends AbstractAction { public ComputeAction() { super("Compute"); putValue(Action.MNEMONIC_KEY, Integer.valueOf('C')); } public void actionPerformed(ActionEvent e) { int d1,d2; try { d1 = Integer.parseInt( n1Field.getText() ); d2 = Integer.parseInt( n2Field.getText() ); outArea.setText(null); outArea.append("number square cube\n"); for (int i=Math.min(d1,d2),j=Math.max(d1,d2); i i++) { int t; outArea.append(i + " " + (t=i*i) + " " + (t*i)+'\n'); } } catch(NumberFormatException ne){} } }}