import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.StringTokenizer; import java.applet.*; // public class TextTest // { public static void main(String[] args) // { // TextFrame frame = new TextFrame("TextTest"); // // frame.setVisible(true); // } // } // class TextFrame extends JFrame public 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 /* -------------------------------------------------- Constructor: 1. Construct the components 2. Place the components 3. Register Listener for (some) components -------------------------------------------------- */ // public TextFrame(String Title) public void init() { // super(Title); // setSize(500, 500); // Set frame size /* ++++++++++++++++++++++++++++++++++++++++++ 1. Construct the components ++++++++++++++++++++++++++++++++++++++++++ */ // =========================== // 1. a text area for input // =========================== inputArea = new JTextArea(); // Default is editable... // =========================== // 2. a text area for output // =========================== resultField = new JTextField(20); resultField.setEditable(false); // No editing allowed here.... // ================================ // 3. a button to start the show // ================================ calcButton = new JButton("Calculate"); /* ------------------------------------ add components to content pane ------------------------------------ */ /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2. Place the components in JFrame ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ // ====================================================== // Pick Layout manager first !!! // ====================================================== // getContentPane().setLayout( new BorderLayout() ); setLayout( new BorderLayout() ); // ====================================================== // Place JTextArea at center // ====================================================== // getContentPane().add(inputArea, "Center"); add(inputArea, "Center"); // ====================================================== // This is for extra sexy effect: // +----------+-------------------------+ // | Average: | (output text field) | // +----------+-------------------------+ // ====================================================== JPanel resultPanel = new JPanel(); // JPanel for sexy effect resultPanel.add(new JLabel("Average: ")); // Add label first resultPanel.add(resultField); // Then add output text area // ====================================================== // Another JPanel for extra sexy effect: // +-----------------+ // | Calculate | // +-----------------+ // ====================================================== JPanel buttonPanel = new JPanel(); // Make a panel buttonPanel.add(calcButton); // Add Calculate button // ====================================================== // A third JPanel to make it ultra sexy.... // We stack up these previous two panels in this JPanel // like this: // +--------------------------------------+ // |+----------+-------------------------+| // || Average: | .....(output text area) || // |+----------+-------------------------+| // | +-----------------+ | // | | Calculate | | // | +-----------------+ | // +--------------------------------------+ // ====================================================== 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 // ======================================================== // Finally, put the stacked panels at "South" of JFrame // ======================================================== // getContentPane().add(southPanel, "South"); add(southPanel, "South"); // ++++++++++++++++++++++++++++++++++++++++++++++++++++++ // OK, make-up session is over, only thing left is // to make the GUI alive.... // ++++++++++++++++++++++++++++++++++++++++++++++++++++++ /* ++++++++++++++++++++++++++++++++++++++++++ 3. Register Listener for (some) components ++++++++++++++++++++++++++++++++++++++++++ */ 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(); // <------ how to get text typed in // a JTextArea object // 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); } } }