// Author: Graham Freeman. July 2008 // Copyright (c) Graham Freeman. g-freeman@adfa.edu.au // The author accepts no responsibility for any problems caused by use of // this software. It is supplied free of charge, and usage is unrestricted, // provided this copyright notice remains intact, regardless of how the // software may be modified by other people. import javax.swing.*; import java.awt.*; import java.awt.event.*; /** This class demonstrates some of the features of FormLayout.java, a non-Sun layout manager which is a little easier to use in some ways than tht standard classes. It is required for this program to compile and execute.
It demonstrates a problem with non-Sun layout managers, the problem of determining the size of a JScrollPane after its contents have changed. It is necessary to occasionally call pack() twice for size of the window to be correct. */ public class TestFormLayout2 extends JFrame implements ActionListener { JButton reject, accept, quit; JTextArea addr; JScrollPane jsp; public static void main( String[] arg ) { new TestFormLayout2().setVisible(true); } public TestFormLayout2() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container cpane = getContentPane(); FormLayout form = new FormLayout(cpane); JLabel lab1 = new JLabel("Name: ") ; form.setTopAnchor( lab1, 3 ); form.setLeftAnchor( lab1, 4 ); JLabel lab2 = new JLabel("Address: "); JTextField name = new JTextField(" ",50); form.setLeftRelative( name, lab1, 3 ); form.setLeftRelative( name, lab2, 3 ); form.setTopAnchor( name, 3 ); form.setLeftAnchor( lab2, 4 ); form.setTopRelative( lab2, name, 3 ); addr = new JTextArea(" ",3,50 ); jsp = new JScrollPane( addr, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); form.setLeftAlign( jsp, name ); form.setTopRelative( jsp, name, 3 ); form.setRightAnchor( jsp, 4 ); JLabel lab4 = new JLabel("Adult "); form.setLeftAlign( lab4, name ); form.setTopRelative( lab4, jsp, 3 ); JTextField adults = new JTextField(" ",3); form.setLeftRelative( adults, lab4, 3 ); form.setTopRelative( adults, jsp, 3 ); JLabel Ch = new JLabel("Child"); form.setLeftRelative( Ch, adults, 16 ); form.setTopRelative( Ch, jsp, 3 ); JTextField children = new JTextField(" ",3); form.setLeftRelative( children, Ch, 3 ); form.setTopRelative( children, jsp, 3 ); JLabel Con = new JLabel("Concession"); form.setLeftRelative( Con, children, 16 ); form.setTopRelative( Con, jsp, 3 ); JTextField concess = new JTextField(" ",3); form.setLeftRelative( concess, Con, 3 ); form.setTopRelative( concess, jsp, 3 ); accept = new JButton("Accept"); form.setHorizontalAnchor( accept, 0.2 ); form.setTopRelative( accept, adults, 3 ); reject = new JButton("Reject"); form.setHorizontalAnchor( reject, 0.5 ); form.setTopAlign( reject, accept ); accept.addActionListener( this ); reject.addActionListener( this ); quit = new JButton("Exit"); form.setHorizontalAnchor( quit, 0.8 ); form.setTopAlign( quit, accept ); form.setBottomAnchor( quit, 5 ); quit.addActionListener( this ); pack(); } public void actionPerformed( ActionEvent e ) { Object source = e.getSource(); if (source == reject) { System.out.println("\nReject"); jsp.setVisible(false); pack(); } else if (source == accept) { System.out.println("\nAccept"); addr.setText("First a very long line of text that should force a " +"horizontal scroll bar. We need a fair bit of text; the font" +"is surprisingly narrow.\n"); pack(); } else if (source == quit) { System.out.println("\nQuit"); jsp.setVisible(true); addr.append("and\nthen\nseveral\nlines\nto force\na " +"vertical\nscrollbar\n"); pack(); pack(); // Java bug: requires two packs in many places like this } } }