Java Reference
In-Depth Information
The text in the labels is right justified so that each label clearly refers to the values
in the corresponding text field. The text field references are instance variables
so that in a more ambitious program that uses InputsPanel other methods
could access them and display text on the text fields or grab user input text from
them.
The following program - MultiPanelApplet - shows how to create a
more complex user interface by combining multiple panels and components.
We set the layout manager for the content pane to a GridLayout of one
row by three columns. We create instances of our ActionButtonsPanel and
InputsPanel classes discussed above and also an instance of a JTextArea .
The JTextArea component can display multiple lines of text input or output.
Here we set the text area so that it only shows output and accepts no input, and we
set its background color to light gray. These components are added in the order
that we wish them to appear left to right.
import java.awt.*;
import javax.swing.*;
/** Demonstrate the use of multiple JPanel subclasses. **/
public class MultiPanelApplet extends JApplet
{
InputsPanel fInputsPanel;
JTextArea fTextOutput;
/** Build the interface with InputsPanel
* and ActionButtonsPanel.
**/
public void init () {
Container content - pane = getContentPane ();
// Set the layout as before with 1 row of 3 columns
// but now in one step.
content - pane.setLayout (new GridLayout (1, 3));
// First create a panel of buttons
ActionButtonsPanel buttons - panel =
new ActionButtonsPanel ();
// Next create a panel of input fields and labels
fInputsPanel =
new InputsPanel ( " Input x " , " 1.5 " ,
" Input y " , " 3.14 " );
// Use a JTextArea for the output of the calculations.
fTextOutput = new JTextArea ();
 
Search WWH ::




Custom Search