import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.StringTokenizer; public class TextTest { public static JTextArea inputArea; // Text area for input public static JTextField resultField; // Text area for output public static JButton calcButton; // Button to trigger calculation public static void main(String[] args) { JFrame f = new JFrame("TextTest"); /* ++++++++++++++++++++++++++++++++++++++++++ 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 !!! // ====================================================== f.getContentPane().setLayout( new BorderLayout() ); // ====================================================== // Place JTextArea at center // ====================================================== f.getContentPane().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 // ======================================================== f.getContentPane().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! f.setSize(500, 500); // Set frame size f.setVisible(true); } /* ============================================================= Listener class for the Calculate button ============================================================= */ public static 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); } } }