// 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 BorderLayout.java (with Box) and how it handles a JScrollPane which gains or loses scrollbars. */ public class TestBorderLayout extends JFrame implements ActionListener { JButton reject, accept, quit; JTextArea addr; JScrollPane jsp; public static void main( String[] arg ) { new TestBorderLayout().setVisible(true); } public TestBorderLayout() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container cpane = getContentPane(); BorderLayout gbag = new BorderLayout(); cpane.setLayout( gbag ); JPanel pane = new JPanel(); JLabel lab1 = new JLabel("Name: ") ; pane.add( lab1 ); JLabel lab2 = new JLabel("Address: "); JTextField name = new JTextField(" ",50); pane.add( name ); cpane.add( "North",pane ); cpane.add( "West", lab2 ); addr = new JTextArea(" ",3,50 ); jsp = new JScrollPane( addr, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); cpane.add( "Center",jsp ); pane = new JPanel(); JLabel lab4 = new JLabel("Adult "); pane.add( lab4 ); JTextField adults = new JTextField(" ",3); pane.add( adults ); pane.add(Box.createRigidArea(new Dimension(8,0))); JLabel Ch = new JLabel("Child"); pane.add( Ch ); JTextField children = new JTextField(" ",3); pane.add( children ); pane.add(Box.createRigidArea(new Dimension(8,0))); JLabel Con = new JLabel("Concession"); pane.add( Con ); JTextField concess = new JTextField(" ",3); pane.add( concess ); Box box = new Box(BoxLayout.Y_AXIS); box.add(pane); pane = new JPanel(); accept = new JButton("Accept"); pane.add( accept ); reject = new JButton("Reject"); pane.add( reject ); accept.addActionListener( this ); reject.addActionListener( this ); quit = new JButton("Exit"); pane.add( quit ); quit.addActionListener( this ); box.add(pane); cpane.add( "South",box ); 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(); } } }