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]

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