import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingExample implements ActionListener{ //instance variables: //(put anything here which you are going to what to access from multiple // method methods, such as things you will mess with when an action is performed) JFrame window; JTextArea commentArea; JCheckBox turkeyCheck;//we need this to be an instance variable, because we check it in ActionPerformed public SwingExample(){ this.window = new JFrame("COP3330 GUI Test"); window.setLayout(new FlowLayout());//set the layout JLabel nameLabel = new JLabel("Enter Name: "); JButton submitButton = new JButton("Submit"); //button with image: ImageIcon cancelIcon = new ImageIcon("cancel.gif"); JButton cancelButton = new JButton("Cancel", cancelIcon); //Check and radio: this.turkeyCheck = new JCheckBox("Turkey"); JCheckBox stuffingCheck = new JCheckBox("Stuffing"); stuffingCheck.setSelected(true); JLabel desertLabel = new JLabel("Do you want desert?"); JRadioButton yesRadio = new JRadioButton("yes"); JRadioButton noRadio = new JRadioButton("no"); //Text: JTextField nameText = new JTextField(20);//20 is number of collums of text, length this.commentArea = new JTextArea("Give your comments here", 4, 20);//always give a size //add everything to the window: window.add(nameLabel); window.add(nameText); window.add(submitButton); window.add(cancelButton); window.add(turkeyCheck); window.add(stuffingCheck); window.add(desertLabel); window.add(yesRadio); window.add(noRadio); window.add(commentArea); //add listeners to components turkeyCheck.addActionListener(this);//just using "this" as the listener turkeyCheck.setActionCommand("turkey");//the ActionEvent will have this as a command //make look good: window.pack(); //window.setSize(800,400); window.setVisible(true); /*UnComment to make window close after 10 seconds) for (int i = 0; i < 10; i++){ Thread.sleep(1000); } window.setVisible(false); System.exit(0); */ } public void actionPerformed(ActionEvent e){ if (e.getActionCommand().equals("turkey")){ if (turkeyCheck.isSelected()){ commentArea.setText("Turkey!"); } else { commentArea.setText("No Turkey"); } } } public static void main (String [] args) throws InterruptedException{ SwingExample swe = new SwingExample(); } }