// GridLayoutTest.java JPanel test too import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class GridLayoutTest extends JApplet implements ActionListener { //, ItemListener, ListSelectionListener { private JButton myButton1, myButton2, myButton3, myButton4, myButton5, myButton6, myButton7, myButton8, myButton9; private JPanel myPanelNorth, myPanelSouth; private JLabel myLabel; private JTextField myTextField; private JPasswordField myPasswordField; private JTextArea myTextArea; private JCheckBox myCheckBox1, myCheckBox2; private JRadioButton myRadioButton1, myRadioButton2; private ButtonGroup myButtonGroup; private JComboBox myComboBox; private JList myList; String [] names = {"fred", "jane", "steve", "susan", "bob"}; public void init() { Container container = getContentPane(); //container.setLayout( new BorderLayout() ); container.setLayout( new GridLayout(3,3) ); container.setBackground(Color.MAGENTA); /* //combine with JPanel test myPanelNorth = new JPanel(); //myPanelNorth.setLayout( new FlowLayout() ); myPanelNorth.setLayout( new GridLayout(3,3) ); container.add( myPanelNorth, BorderLayout.NORTH ); myPanelSouth = new JPanel(); myPanelSouth.setLayout( new FlowLayout() ); container.add( myPanelSouth, BorderLayout.SOUTH ); myLabel = new JLabel( "My JLabel" ); myPanelSouth.add( myLabel ); myTextField = new JTextField( "My JTextField", 20 ); myPanelSouth.add( myTextField ); myTextField.addActionListener( this ); */ /* myPasswordField = new JPasswordField( "Type your password here",20 ); container.add( myPasswordField ); myPasswordField.addActionListener( this ); */ myButton1 = new JButton( "My JButton 1\n\nkjhkjhkj" ); myButton2 = new JButton( "My JButton2" ); myButton3 = new JButton( "My JButton3lkjlkjlkjlkjlkjlkjlkj" ); myButton4 = new JButton( "My JButton4" ); myButton5 = new JButton( "My JButton5" ); myButton6 = new JButton( "My JButton6" ); myButton7 = new JButton ("My JButton7" ); myButton8 = new JButton( "My JButton8" ); myButton9 = new JButton( "My JButton9" ); //comment out if doing JPanel test container.add( myButton1 ); container.add( myButton2 ); container.add( myButton3 ); container.add( myButton4 ); container.add( myButton5 ); container.add( myButton6 ); container.add( myButton7 ); container.add( myButton8 ); container.add( myButton9 ); /* myPanelNorth.add( myButton1 ); myPanelNorth.add( myButton2 ); myPanelNorth.add( myButton3 ); myPanelNorth.add( myButton4 ); myPanelNorth.add( myButton5 ); myPanelNorth.add( myButton6 ); myPanelNorth.add( myButton7 ); myPanelNorth.add( myButton8 ); myPanelNorth.add( myButton9 ); */ myButton1.addActionListener( this ); myButton2.addActionListener( this ); myButton3.addActionListener( this ); myButton4.addActionListener( this ); myButton5.addActionListener( this ); myButton6.addActionListener( this ); myButton7.addActionListener( this ); myButton8.addActionListener( this ); myButton9.addActionListener( this ); /* myCheckBox1 = new JCheckBox( "My JCheckBox 1" ); container.add( myCheckBox1 ); myCheckBox1.addItemListener( this ); myCheckBox2 = new JCheckBox( "My JCheckBox 2", true ); container.add( myCheckBox2 ); myCheckBox2.addItemListener( this ); myRadioButton1 = new JRadioButton( "My JRadioButton 1" ); container.add( myRadioButton1 ); myRadioButton1.addItemListener( this ); myRadioButton2 = new JRadioButton( "My JRadioButton 2" ); container.add( myRadioButton2 ); myRadioButton2.addItemListener( this ); //ButtonGroup makes radio buttons act as a group of radio buttons myButtonGroup = new ButtonGroup(); myButtonGroup.add(myRadioButton1); myButtonGroup.add(myRadioButton2); //ButtonGroup NOT added to container myComboBox = new JComboBox(names); myComboBox.setMaximumRowCount(3); container.add( myComboBox ); myComboBox.addItemListener( this ); myList = new JList(names); myList.setVisibleRowCount(3); myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); container.add(new JScrollPane(myList)); //container.add(myList); myList.addListSelectionListener( this ); myButton2 = new JButton( "My JButton2" ); container.add( myButton2, 6 ); //insert into position 6 myButton2.addActionListener( this ); */ } public void paint (Graphics g) { super.paint(g); //draw random line to see if alive g.drawLine((int)(Math.random()*getWidth()), (int)(Math.random()*getHeight()), (int)(Math.random()*getWidth()), (int)(Math.random()*getHeight())); } // event handler for JButton, JTextField and JPasswordField events public void actionPerformed( ActionEvent event ) { // if ( event.getSource() == myButton ) JOptionPane.showMessageDialog(null,"got it"); /* else if ( event.getSource() == myTextField ) { JOptionPane.showMessageDialog(null,"myTextField got it: " + event.getActionCommand() ); JOptionPane.showMessageDialog(null,"myTextField got it: " + myTextField.getText() ); myTextField.setText( "" ); } else if ( event.getSource() == myPasswordField ) { JOptionPane.showMessageDialog(null,"myPasswordField got it: " + event.getActionCommand()+ " oops" ); myPasswordField.setText( "" ); } */ } /* // event handler for JCheckBox, JRadioButton and JComboBox events public void itemStateChanged( ItemEvent event ) { if ( event.getSource() == myCheckBox1 ) { if (event.getStateChange() == ItemEvent.SELECTED) JOptionPane.showMessageDialog(null,"myCheckBox 1 selected"); else if (event.getStateChange() == ItemEvent.DESELECTED) JOptionPane.showMessageDialog(null,"myCheckBox 1 deselected"); } else if ( event.getSource() == myCheckBox2 ) { if ( myCheckBox2.isSelected() ) //alternate way JOptionPane.showMessageDialog(null,"myCheckBox 2 selected"); else if ( ! myCheckBox2.isSelected() ) //alternate way JOptionPane.showMessageDialog(null,"myCheckBox 2 deselected"); } else if ( event.getSource() == myRadioButton1 ) { if ( myRadioButton1.isSelected() ) //alternate way JOptionPane.showMessageDialog(null,"myRadioButton 1 selected"); } else if ( event.getSource() == myRadioButton2 ) { if ( myRadioButton2.isSelected() ) //alternate way JOptionPane.showMessageDialog(null,"myRadioButton 2 selected"); } else if ( event.getSource() == myComboBox ) { if ( event.getStateChange() == ItemEvent.SELECTED ) { int i = myComboBox.getSelectedIndex(); JOptionPane.showMessageDialog(null,"myComboBox selected index:" + i + "\nitem value: " + names[i]); } } } //event handler for JList public void valueChanged( ListSelectionEvent event ) { if ( event.getSource() == myList ) { int i = myList.getSelectedIndex(); JOptionPane.showMessageDialog(null,"myList index: " + i + "\nitem value: " + names[i]); } } */ }