Re: Layout help
geoph521@gmail.com wrote:
I'm just learning swing with awt for making a ui, and i can't seem to
get it to look how I want.
layout:
__________________________
|Label : [Text Area ] |
|Label : [Text Area ] |
|Label : [Text Area ] |
|Label : [Text Area ] |
| [Button][Button] |
|_________________________|
|Text Area |
| |
| |
|_________________________|
Does this do what you want?
<sscce>
import java.awt.*;
import javax.swing.*;
class FieldAndTextForm {
public static void main(String args[]) {
JFrame f = new JFrame("Field & Text Form");
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = f.getContentPane();
c.setLayout( new BorderLayout(5,5) );
JPanel fieldGrid = new JPanel(new BorderLayout(2,3));
JPanel labelGrid = new JPanel(new GridLayout(0,1));
JPanel textFieldGrid = new JPanel(new GridLayout(0,1));
fieldGrid.add( labelGrid, BorderLayout.WEST );
fieldGrid.add( textFieldGrid, BorderLayout.CENTER );
for (int ii=0; ii<5; ii++) {
labelGrid.add( new JLabel("Label " + (ii+1)) );
textFieldGrid.add( new JTextField("Text " + (ii+1)) );
}
JButton restore = new JButton("Restore");
JButton cancel = new JButton("Cancel");
JPanel buttonPanel = new
cJPanel(new FlowLayout(FlowLayout.CENTER, 15, 0) );
buttonPanel.add(restore);
buttonPanel.add(cancel);
fieldGrid.add( buttonPanel, BorderLayout.SOUTH );
JTextArea ta = new JTextArea(3,35);
c.add( new JScrollPane(ta,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER ),
BorderLayout.CENTER );
c.add( fieldGrid, BorderLayout.NORTH );
f.pack();
f.setVisible(true);
}
}
</sscce>
Andrew T.