//import javax.swing.JButton; //import javax.swing.JTextField; //import javax.swing.JLabel; //import javax.swing.JFrame; import java.awt.event.*; import javax.swing.*; import java.awt.FlowLayout; class BobPanel extends JPanel { private int numstudents; private double classAverage; private JButton myButton; private JTextField firstNameField; private JTextField LastNameField; private JLabel label1; private JLabel label2; private JCheckBox cbox; private String[] buttonLabels = {"Button1", "Button2", "Button3", "Button4"}; private JRadioButton[] rbuttons; ButtonGroup bgroup; public BobPanel() // one possible constructor { setLayout(new FlowLayout()); firstNameField = new JTextField(15); this.add(firstNameField); // this is the same as saying: add(firstNameField); myButton = new JButton("Query the text field"); add(myButton); bgroup = new ButtonGroup(); rbuttons = new JRadioButton[buttonLabels.length]; for (int i = 0; i < buttonLabels.length; i++) { rbuttons[i] = new JRadioButton(buttonLabels[i]); add(rbuttons[i]); bgroup.add(rbuttons[i]); } cbox = new JCheckBox("Would you like our spam newsletter"); add(cbox); label1 = new JLabel("Default Text"); add(label1); // we will set up an event on the BUTTON that reads the textfield // and sets the LABEL's text to the same thing Fred evan = new Fred(); // this is an object of our inner class type below myButton.addActionListener(evan); // what to pass in here? firstNameField.addActionListener(evan); // we will continue from here on Thursday } public int GetNumStudents() { return numstudents; } public static void main(String[] args) { // from here, can I use the variables I declared above? // NO!!!!!!!!!!!!!!!!!!! JFrame myFrame = new JFrame("My Program In Class"); // This line builds a BobPanel object (which IS a JPanel, because // class BobPanel extends JPanel -- with customization) // AND it runs the BobPanel constructor. BobPanel p = new BobPanel(); myFrame.add(p); // adds the panel (p) INTO the frame (myFrame) myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); myFrame.setSize( 400, 300 ); // set frame size myFrame.setVisible( true ); // display frame } // we will write an inner class that IMPLEMENTS the ActionListener interface, // so that objects of this type can be passed IN to the "addActionListener" method private class Fred implements ActionListener // implementing the interface { // this is the function that RUNS when an object of type Fred is // associated with a specific component that calls addActionListener public void actionPerformed(ActionEvent e) { if (e.getSource() == myButton) { String s = firstNameField.getText(); // retrieve text from the textfield label1.setText(s); // set that as the label's text } else if (e.getSource() == firstNameField) { JOptionPane.showMessageDialog(null, "Surprise! Your text is: " + firstNameField.getText()); } } } // end of class Fred (which is an INNER class inside BobPanel) } // end of class BobPanel