public class TextTest
{
// Components that are SHARED by main() and
// the call-back method(s) must be define outside
public JTextArea inputArea; // Text area for input
public JTextField resultField; // Text area for output
public 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");
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2. Place the components in JFrame
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
// ======================================================
// Pick Layout manager first !!!
// ======================================================
f.getContentPane().setLayout( new BorderLayout() );
// ======================================================
// Place JTextArea at center
// ======================================================
f.getContentPane().add(inputArea, "Center");
// ======================================================
// Make this JPanel first:
// +----------+-------------------------+
// | 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
// ======================================================
// Make another JPanel with Calcultae button:
// +-----------------+
// | Calculate |
// +-----------------+
// ======================================================
JPanel buttonPanel = new JPanel(); // Make a panel
buttonPanel.add(calcButton); // Add Calculate button
// ======================================================
// Use a third JPanel to stack the 2 prior JPanels
// Like this: (Grid(2,1))
// +--------------------------------------+
// |+----------+-------------------------+|
// || 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");
f.setSize(500, 500); // Set frame size
f.setVisible(true);
}
}
|