|
|
|
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.StringTokenizer; public class TextTest { public static void main(String[] args) { TextFrame frame = new TextFrame("TextTest"); frame.setVisible(true); } } class TextFrame extends JFrame { private JTextArea inputArea; // Text area for input private JTextField resultField; // Text area for output private JButton calcButton; // Button to trigger calculation public TextFrame(String Title) { super(Title); setSize(500, 500); // Set frame size inputArea = new JTextArea(); // Default is editable... resultField = new JTextField(20); resultField.setEditable(false); // No editing allowed here.... calcButton = new JButton("Calculate"); getContentPane().setLayout( new BorderLayout() ); getContentPane().add(inputArea, "Center"); JPanel resultPanel = new JPanel(); // JPanel for sexy effect resultPanel.add(new JLabel("Average: ")); // Add label first resultPanel.add(resultField); // Then add output text area JPanel buttonPanel = new JPanel(); // Make a panel buttonPanel.add(calcButton); // Add Calculate button JPanel southPanel = new JPanel(); // Make a panel southPanel.setLayout(new GridLayout(2, 1)); // We need a Grid layout... southPanel.add(resultPanel); // Add first panel southPanel.add(buttonPanel); // Stack second panel getContentPane().add(southPanel, "South"); calcButton.addActionListener(new ButtonListener()); // Press to calculate! } /* ============================================================= Listener class for the Calculate button ============================================================= */ private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { String input; double sum; int i, N; // get user input from text area input = inputArea.getText(); // Get input // Tokenize the string StringTokenizer tokenizer = new StringTokenizer(input); N = tokenizer.countTokens(); sum = 0; for ( i = 0; i < N; i++ ) sum += Double.parseDouble(tokenizer.nextToken()); // display in result field resultField.setText("" + sum/N); } } } |
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.StringTokenizer; import java.applet.Applet; <-------- !!! // public class TextTest // { public static void main(String[] args) // { // TextFrame frame = new TextFrame("TextTest"); // frame.setVisible(true); // } // } // class TextFrame extends JFrame class TextFrame extends Applet <-------- !!! { private JTextArea inputArea; // Text area for input private JTextField resultField; // Text area for output private JButton calcButton; // Button to trigger calculation public TextFrame(String Title) { // super(Title); // setSize(500, 500); // Set frame size inputArea = new JTextArea(); // Default is editable... resultField = new JTextField(20); resultField.setEditable(false); // No editing allowed here.... calcButton = new JButton("Calculate"); |
How to run an applet:
1. Compile: javac TextFrame.java 2. Make a webpage that loads the applet (see below) 3a. Testing: use appletviewer webpage 3b. For real: point browser to the webpage |
|
A copy of the Java plugin classes12.zip can be found here: RIGHT click and save
Example:
<EMBED type="application/x-java-applet;version=1.2" archive="classes12.zip" code="TextFrame.class" WIDTH=400 HEIGHT=400> </EMBED> |
Click on the link to load applet in browser
Load the webpage with appletviewer:
1. Save webpage 2. Save applet code 3. Compile applet code 4. Run: appletviewer Load-TextFrame.html |