I am getting a syntax error. I can\'t seem to find what\'s causing this progrm e
ID: 3790711 • Letter: I
Question
I am getting a syntax error. I can't seem to find what's causing this progrm even though I correct what it's asking me to do. It seems to add even more errors.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class GUI extends JFrame {
// create required GUI radios, labels, textfields and buttons
private JRadioButton iterative;
private JRadioButton recursive;
private JLabel entern;
private JLabel result;
private JLabel efficiency;
private JTextField t1;
private JTextField t2;
private JTextField t3;
private JButton compute;
private ButtonGroup radios = new ButtonGroup();
private ButtonGroup candidates = new ButtonGroup();
// create constructor for GUI
public GUI(){
super("Project 3");
setLayout (new GridLayout(6,2));
setSize(275,250);
// initializing buttons and etc and adding them
iterative = new JRadioButton("Iterative");
add(iterative);
recursive = new JRadioButton("Recursive");
add(recursive);
// add radiobuttons to group so only one selection is possible
radios.add(iterative);
radios.add(recursive);
entern = new JLabel("Enter n:");
add(entern);
result = new JLabel("Result:");
add(result);
efficiency = new JLabel("Efficiency:");
add(efficiency);
t1 = new JTextField();
add(t1);
compute = new JButton("Compute");
add(compute);
t2 = new JTextField();
add(t2);
t3 = new JTextField();
add(t3);
/*
thehandler handler = new thehandler();
iterative.addActionListener(handler);
recursive.addActionListener(handler);
compute.addActionListener(handler);
****************************/
compute.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent b){
computeactionPerformed(b);
private void computeactionPerformed(ActionEvent b){
if ((iterative.isSelected(true))){
JOptionPane.showMessageDialog(null,"you chose iterative");
}
else if (recursive.isSelected(true)){
JOptionPane.showMessageDialog(null,"you chose recursive");
}
}
}
});
// create sub-panel for radio buttons
JPanel radioPanel = new JPanel();
radioPanel.add(iterative);
radioPanel.add(recursive);
/*
class thehandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==iterative && e.getSource()==compute){
System.out.println("you chose iterative");
}
else if(e.getSource()==recursive && e.getSource()==compute){
System.out.println("you chose recursive");
}
}
}
******************************/
}
public static void main(String[]args){
GUI object = new GUI();
object.setVisible(true);
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Explanation / Answer
I have Change in code . No it's working without any compliantion error.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class GUI extends JFrame {
// create required GUI radios, labels, textfields and buttons
private JRadioButton iterative;
private JRadioButton recursive;
private JLabel entern;
private JLabel result;
private JLabel efficiency;
private JTextField t1;
private JTextField t2;
private JTextField t3;
private JButton compute;
private ButtonGroup radios = new ButtonGroup();
private ButtonGroup candidates = new ButtonGroup();
// create constructor for GUI
public GUI()
{
super("Project 3");
setLayout (new GridLayout(6,2));
setSize(275,250);
// initializing buttons and etc and adding them
iterative = new JRadioButton("Iterative");
add(iterative);
recursive = new JRadioButton("Recursive");
add(recursive);
// add radiobuttons to group so only one selection is possible
radios.add(iterative);
radios.add(recursive);
entern = new JLabel("Enter n:");
add(entern);
result = new JLabel("Result:");
add(result);
efficiency = new JLabel("Efficiency:");
add(efficiency);
t1 = new JTextField();
add(t1);
t2 = new JTextField();
add(t2);
t3 = new JTextField();
add(t3);
compute = new JButton("Compute");
add(compute);
compute.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent b)
{
if (iterative.isSelected())
{
JOptionPane.showMessageDialog(null,"you chose iterative");
}
else if (recursive.isSelected())
{
JOptionPane.showMessageDialog(null,"you chose recursive");
}
}
});
// create sub-panel for radio buttons
JPanel radioPanel = new JPanel();
radioPanel.add(iterative);
radioPanel.add(recursive);
}
public static void main(String[]args){
GUI object = new GUI();
object.setVisible(true);
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.