// BorderLayoutTest.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class BorderLayoutTest extends JApplet implements ActionListener { //, ItemListener, ListSelectionListener { private JLabel myLabel; private JTextField myTextField; private JPasswordField myPasswordField; private JButton myButton1, myButton2, myButton3, myButton4, myButton5; 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() { // get applet's content pane and change its layout to FlowLayout Container container = getContentPane(); container.setLayout( new BorderLayout(12,12) ); container.setBackground(Color.MAGENTA); /* myLabel = new JLabel( "My JLabel" ); container.add( myLabel ); myTextField = new JTextField( "My JTextField", 20 ); container.add( myTextField ); //myTextField.setEditable( false ); 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" ); container.add( myButton1, BorderLayout.NORTH ); myButton2 = new JButton( "My JButton2" ); container.add( myButton2, BorderLayout.EAST ); */ myButton3 = new JButton( "My JButton3lkjlkjlkjlkjlkjlkjlkj" ); container.add( myButton3, BorderLayout.WEST ); myButton4 = new JButton( "My JButton4" ); container.add( myButton4, BorderLayout.CENTER ); myButton5 = new JButton( "My JButton5" ); container.add( myButton5, BorderLayout.SOUTH ); // myButton1.addActionListener( this ); // myButton2.addActionListener( this ); myButton3.addActionListener( this ); myButton4.addActionListener( this ); myButton5.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,"myButton 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]); } } */ }