- /**
* This class defines the main frame for the Layout manager
* program
*/
- import java.awt.* ;
import javax.swing.* ;
- public class MainFrame extends JFrame {
-
// define north panel and components
- JPanel northPanel = new JPanel ( ) ;
- JButton resetButton =
new JButton ( "Reset" ) ;
- JButton exitButton =
new JButton ( "Exit" ) ;
-
- // define east panel
and components
- JPanel eastPanel = new
JPanel ( ) ;
- JCheckBox northCheck =
new JCheckBox ( "North" ) ;
- JCheckBox southCheck =
new JCheckBox ( "South" ) ;
- JCheckBox westCheck =
new JCheckBox ( "West" ) ;
- JCheckBox centerCheck =
new JCheckBox ( "Center"
) ;
-
- // define west panel
and components
- JPanel westPanel = new
JPanel ( ) ;
- JRadioButton red = new
JRadioButton ( "Red", false
) ;
- JRadioButton green =
new JRadioButton ( "Green",
false ) ;
- JRadioButton blue =
new JRadioButton ( "Blue",
false ) ;
- JRadioButton black =
new JRadioButton ( "Black",
true ) ;
- ButtonGroup colorGroup =
new ButtonGroup ( ) ;
-
- // define center panel
and components
- JPanel centerPanel =
new JPanel ( ) ;
- JButton northButton =
new JButton ( "North" ) ;
- JButton southButton =
new JButton ( "South" ) ;
- JButton eastButton =
new JButton ( "East" ) ;
- JButton westButton =
new JButton ( "West" ) ;
- JButton centerButton =
new JButton ( "Center" ) ;
-
- // define south panel
and components
- JPanel southPanel =
new JPanel ( ) ;
- JTextField northStatus =
new JTextField ( 10 ) ;
- JTextField westStatus =
new JTextField ( 10 ) ;
- JTextField centerStatus =
new JTextField ( 10 ) ;
-
- /**
- * The constructor
- */
- public
MainFrame ( String str ) {
- super ( str
) ;
-
- // add the
components to each panel and sets its format
- northPanel.setLayout (
new FlowLayout ( FlowLayout.CENTER ) ) ;
- northPanel.add ( resetButton ) ;
- northPanel.add ( exitButton ) ;
-
- eastPanel.setLayout (
new GridLayout ( 4,1 ) ) ;
- eastPanel.add (northCheck ) ;
- eastPanel.add (southCheck ) ;
- eastPanel.add (westCheck ) ;
- eastPanel.add (centerCheck ) ;
- westPanel.setLayout (
new GridLayout ( 4,1 ) ) ;
- westPanel.add ( red ) ;
- westPanel.add ( green ) ;
- westPanel.add ( blue ) ;
- westPanel.add ( black ) ;
-
- // Adding buttons
into a ButtonGroup object allows only
- // one button at a time
to be selected
- colorGroup.add ( red ) ;
- colorGroup.add ( green ) ;
- colorGroup.add ( blue ) ;
- colorGroup.add ( black ) ;
-
- centerPanel.setLayout (
new BorderLayout ( ) ) ;
- centerPanel.add ( northButton,
"North") ;
- centerPanel.add (southButton,
"South") ;
- centerPanel.add (eastButton,
"East") ;
- centerPanel.add (westButton,
"West") ;
- centerPanel.add (centerButton,
"Center") ;
- southPanel.setLayout (
new FlowLayout(FlowLayout.CENTER ) ) ;
- southPanel.add ( northStatus ) ;
- southPanel.add ( westStatus ) ;
- southPanel.add ( centerStatus ) ;
- // set up panels
in frame
- this.getContentPane ( ).setLayout(new
BorderLayout ( ) ) ;
- getContentPane ( ).add(northPanel,
"North" ) ;
- getContentPane ( ).add(southPanel,
"South" ) ;
- getContentPane( ).add(eastPanel,
"East" ) ;
- getContentPane( ).add(westPanel,
"West" ) ;
- getContentPane( ).add(centerPanel,
"Center" ) ;
- }
- }