Reputation: 11
The assignment is a basic voting system of picking between 3 people or 4th button is a "decline to answer"
I keep getting errors such as "Cannot find symbol"
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Vote
{
JLabel label;
public static void main (String[] args)
{
Vote sr = new Vote();
}
public Vote ()
{
JFrame frame = new JFrame("Please Choose your next President:");
JRadioButton one = new JRadioButton("Name 1");
JRadioButton two = new JRadioButton("Name 2");
JRadioButton three = new JRadioButton("Name 3");
JRadioButton four = new JRadioButton("I decline to answer");
JPanel panel = new JPanel();
panel.add(one);
panel.add(two);
panel.add(three);
panel.add(four);
ButtonGroup but = new ButtonGroup();
but.add(one);
but.add(two);
but.add(three);
but.add(four);
one.addActionListener(new MyAction());
two.addActionListener(new MyAction());
three.addActionListener(new MyAction());
four.addActionListener(new MyAction());
label = new JLabel("Please Select a Canidate for President ");
label.setHorizontalAlignment(JLabel.CENTER);
frame.add(panel, BorderLayout.NORTH);
frame.add(label, BorderLayout.CENTER);
frame.setSize(400, 100);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if (jRadioButton.isSelected())
{
label.setText(e.getActionCommand());
JOptionPane.showMessageDialog(null, "You have selected " +
e.getActionCommand()
+ " as the new President. Thank you for voting.");
}
else
{
label.setText(e.getActionCommand());
JOptionPane.showMessageDialog(null, "You have selected " +
e.getActionCommand() + " Thank you for voting.");
}
}
}
}
So basic functions is to pick a name from 4 available radio buttons and upon selection of any buttons, a new windows comes up saying thank you for voting.
I would like to have different responses depending on the radio button pressed, so if a button pressed saying "I declined to answer" a proper response would be towards it.
Please check my If statement because i think where most of the errors are coming from and from a part of .isSelected
.
Upvotes: 1
Views: 122
Reputation: 2047
You have no object named jRadioButton
.
try changing
jRadioButton.isSelected()
to
((JRadioButton)e.getSource()).isSelected()
Also if you want to check if 4th JRadioButton
was selected, then do the following:
four.setActionCommand("fourth");
and then in if
statement
if ("fourth".equals(e.getActionCommand()) && ((JRadioButton)e.getSource()).isSelected())
it will check if action command maches fourth radio button and if it is selected
Upvotes: 0
Reputation: 395
if(jRadioButton.isSelected())
jRadioButton here is not defined.
You would want to get the source that triggered the event (i.e. one, two, three or four):
if(((JRadioButton)e.getSource()).isSelected())
This gets the object that triggered the event, casts it as a JRadioButton, and checks if that radio button is selected.
That should at least get rid of your "Cannot find symbol" error.
Upvotes: 2