examples
Class Example12
java.lang.Object
examples.Example12
public class Example12
- extends java.lang.Object
This example illustrates the
BASELINE
alignment
to align comoponents which contain text.
This example is taken from
an article on
Relative Layout. Without the use of BASELINE alignment in the top level
cell, the text on the label Sample Text and the check box
Strikethrough would not be properly aligned.
public static void createGUI()
{
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container container=frame.getContentPane();
// Create components
JCheckBox bold=new JCheckBox("Bold");
JCheckBox italic=new JCheckBox("Italic");
JCheckBox underline=new JCheckBox("Underline");
JCheckBox strikeThrough=new JCheckBox("Strikethrough");
JCheckBox teletype=new JCheckBox("Teletype");
JCheckBox emphasis=new JCheckBox("Emphasis");
JCheckBox strong=new JCheckBox("Strong");
JLabel sampleText=new JLabel("Sample Text");
JTextArea textArea=new JTextArea("This is sample text");
textArea.setRows(5);
textArea.setColumns(15);
textArea.setBorder(BorderFactory.createLoweredBevelBorder());
JButton apply=new JButton("Apply");
// Top Level cell. The components are laid out in a row
// of three columns. Not the vertical alignment specification.
Row topLevel= new Row(Cell.NO_ALIGNMENT,Cell.BASELINE);
// left column
topLevel.newColumn(Cell.LEFT,Cell.NO_ALIGNMENT,
bold,italic,underline);
// middle column
topLevel.newColumn(Cell.LEFT,Cell.NO_ALIGNMENT,
strikeThrough,teletype,emphasis,strong);
// Column of the Text Area and the Button to center the button
Column textAndButton
=new Column(Cell.CENTER,Cell.NO_ALIGNMENT,textArea,apply);
// Right column
// Since sampleText is a component, and textAndButton
// is a cell, we have to make a separate call to 'add',
// rather than use both in the 'newColumn' call.
topLevel.newColumn(Cell.LEFT,Cell.NO_ALIGNMENT,sampleText).
add(textAndButton);
topLevel.createLayout(container);
`
frame.pack();
frame.setSize(frame.getPreferredSize());
frame.show();
}
public static void main(String[] args)
{
createGUI();
}
Method Summary |
static void |
createGUI()
|
static void |
main(java.lang.String[] args)
|
static void |
run()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Example12
public Example12()
createGUI
public static void createGUI()
main
public static void main(java.lang.String[] args)
run
public static void run()