Can anyone see why this won't run? I can't seem to figure out the mistake!

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Debugging7 extends JApplet

implements ItemListener

{

//Declare components and global variables here

JPanel mainPanel = new JPanel();

JPanel buttonPanel = new JPanel();

JLabel titleLabel = new JLabel("Debugging - Chapter 7", JLabel.CENTER);

JTextArea questionTextArea = new JTextArea(2, 35);

JRadioButton answerARadioButton = new JRadioButton();

JRadioButton answerBRadioButton = new JRadioButton();

JRadioButton answerCRadioButton = new JRadioButton();

JRadioButton answerDRadioButton = new JRadioButton();

ButtonGroup answerButtonGroup = new ButtonGroup();

JLabel answerLabel = new JLabel("", JLabel.CENTER);

Font titleFont = new Font("SansSerif", Font.BOLD, 18);

public void init()

{

//Set up the user interface (JPanels) here

titleLabel.setFont(titleFont);

mainPanel.setLayout(new GridLayout(0,1));

mainPanel.add(titleLabel);

questionTextArea.setText("To respond immediately to a change in the " +

"\nstate of a JRadioButton, you need to use: ");

mainPanel.add(questionTextArea);

questionTextArea.setEnabled(false);

answerButtonGroup.add(answerARadioButton);

answerButtonGroup.add(answerBRadioButton);

answerButtonGroup.add(answerCRadioButton);

answerButtonGroup.add(answerDRadioButton);

buttonPanel.setLayout(new GridLayout(0, 4));

answerARadioButton.setText("an ActionListener");

buttonPanel.add(answerARadioButton);

answerBRadioButton.setText("a compareTo method");

buttonPanel.add(answerBRadioButton);

answerCRadioButton.setText("an ItemListener");

buttonPanel.add(answerCRadioButton);

answerDRadioButton.setText("none of the above");

buttonPanel.add(answerDRadioButton);

mainPanel.add(buttonPanel);

mainPanel.add(answerLabel);

answerLabel.setForeground(Color.red);

answerLabel.setFont(titleFont);

setContentPane(mainPanel);

//Add listeners

answerARadioButton.addActionListener(this);

answerBRadioButton.addActionListener(this);

answerCRadioButton.addActionListener(this);

answerDRadioButton.addActionListener(this);

}

public void itemStatePerformed(ActionEvent event)

{

//Retrieve user input and respond with calculations, etc

Object eventSource = event.getSource();

if(event == answerARadioButton)

answerLabel.setText("an ActionListener is NOT correct");

else if(event == answerBRadioButton)

answerLabel.setText("a compareTo method is NOT correct");

else if(event == answerCRadioButton)

answerLabel.setText("an ItemListener IS CORRECT");

else if(event == answerDRadioButton)

answerLabel.setText("none of the above is NOT correct");

else

answerLabel.setText("answer the question, please!");

}

}

[2990 byte] By [tadow126a] at [2007-11-27 4:36:07]
# 1
What's the problem? What happens? What does not happen? What do you want to happen?
kajbja at 2007-7-12 9:46:16 > top of Java-index,Java Essentials,Java Programming...
# 2
It won't run. I thought I had put in all the correct coding. That is what I am trying to figure out. Do you see any bugs?
tadow126a at 2007-7-12 9:46:16 > top of Java-index,Java Essentials,Java Programming...
# 3

> It won't run. I thought I had put in all the correct

> coding. That is what I am trying to figure out. Do

> you see any bugs?

I haven't looked at the code. You should use code formatting when you post code. It makes it a lot easier to read:

http://forum.java.sun.com/help.jspa?sec=formatting

Do you get an exception or error message?

Kaj

kajbja at 2007-7-12 9:46:16 > top of Java-index,Java Essentials,Java Programming...
# 4
The code is likely not the problem. What did you do to "run" it? What happened when you did that? Why was that wrong?
DrClapa at 2007-7-12 9:46:16 > top of Java-index,Java Essentials,Java Programming...
# 5

Okay, here's some compiling code... Although I don't really want to bother to write up the html to test it so I'll leave that up to you...

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Debugging7 extends JApplet

implements ItemListener

{

//Declare components and global variables here

JPanel mainPanel = new JPanel();

JPanel buttonPanel = new JPanel();

JLabel titleLabel = new JLabel("Debugging - Chapterr 7", JLabel.CENTER);

JTextArea questionTextArea = new JTextArea(2, 35);

JRadioButton answerARadioButton = new JRadioButton();

JRadioButton answerBRadioButton = new JRadioButton();

JRadioButton answerCRadioButton = new JRadioButton();

JRadioButton answerDRadioButton = new JRadioButton();

ButtonGroup answerButtonGroup = new ButtonGroup();

JLabel answerLabel = new JLabel("", JLabel.CENTER);

Font titleFont = new Font("SansSerif", Font.BOLD, 18);

public void init()

{

//Set up the user interface (JPanels) here

titleLabel.setFont(titleFont);

mainPanel.setLayout(new GridLayout(0,1));

mainPanel.add(titleLabel);

questionTextArea.setText("To respond immediately to a change in the " +

"\nstate of a JRadioButton, you need to use: ");

mainPanel.add(questionTextArea);

questionTextArea.setEnabled(false);

answerButtonGroup.add(answerARadioButton);

answerButtonGroup.add(answerBRadioButton);

answerButtonGroup.add(answerCRadioButton);

answerButtonGroup.add(answerDRadioButton);

buttonPanel.setLayout(new GridLayout(0, 4));

answerARadioButton.setText("an ActionListener");

buttonPanel.add(answerARadioButton);

answerBRadioButton.setText("a compareTo method");

buttonPanel.add(answerBRadioButton);

answerCRadioButton.setText("an ItemListener");

buttonPanel.add(answerCRadioButton);

answerDRadioButton.setText("none of the above");

buttonPanel.add(answerDRadioButton);

mainPanel.add(buttonPanel);

mainPanel.add(answerLabel);

answerLabel.setForeground(Color.red);

answerLabel.setFont(titleFont);

setContentPane(mainPanel);

//Add listeners

answerARadioButton.addItemListener(this);

answerBRadioButton.addItemListener(this);

answerCRadioButton.addItemListener(this);

answerDRadioButton.addItemListener(this);

}

public void itemStateChanged(ItemEvent event)

{

//Retrieve user input and respond with calculations, etc

Object eventSource = event.getSource();

if(eventSource.equals(answerARadioButton))

answerLabel.setText("an ActionListener is NOT correct");

else if(eventSource.equals(answerBRadioButton))

answerLabel.setText("a compareTo method is NOT correct");

else if(eventSource.equals(answerCRadioButton))

answerLabel.setText("an ItemListener IS CORRECT");

else if(eventSource.equals(answerDRadioButton))

answerLabel.setText("none of the above is NOT correct");

else

answerLabel.setText("answer the question, please!");

}

}

Message was edited by:

Dalzhim

Dalzhima at 2007-7-12 9:46:16 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks! That did it. I see my mistake. I appreciate the help!
tadow126a at 2007-7-12 9:46:16 > top of Java-index,Java Essentials,Java Programming...