Results 1 to 3 of 3
  1. #1
    dekon is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default radio button error

    hi i'm a currently creating a program for my coursework and i am new to java could someone give me some help please thanks the code is

    import static javax.swing.JOptionPane.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.swing.*;
    import java.text.DecimalFormat;

    public class ticket extends JFrame implements ActionListener {

    static final double SINGLE = 1.40;
    static final double RETURN = 2.40;
    static final double ZONE_A = 3.80;
    static final double ZONE_A_B = 5.50;

    DecimalFormat pounds = new DecimalFormat("£0.00");
    private String id;
    private JTextField ticket = new JTextField(5);
    private JTextField stp = new JTextField(5);
    int amount = 0; //pay in pence
    private JTextField change = new JTextField(5);
    private JTextField paid = new JTextField(5);
    private JButton ten = new JButton("10p");
    private JButton twenty = new JButton("20p");
    private JButton fifty = new JButton("50p");
    private JButton pound = new JButton("£1");
    private JButton two = new JButton("£2");
    private JButton five = new JButton("£5");
    private JButton tenp = new JButton("£10");
    private JButton twentyp = new JButton("£20");
    private JButton cancel = new JButton("Cancel");
    private JRadioButton single = new JRadioButton("single (£1.40)",false);
    private JRadioButton Return = new JRadioButton("return (£2.40)",false);
    private JRadioButton zoneA = new JRadioButton("Zone A (£3.80)",false);
    private JRadioButton zoneAB = new JRadioButton("Zone A B (£5.50)",false);
    ButtonGroup type =new ButtonGroup();
    // The spinner give double values in the range 1 to 10.
    // From the spinner, the user can only purchase 10 tickets at a time!
    private JSpinner numtick = new JSpinner(new SpinnerNumberModel(1, 0, 10, 1));
    private int stpno = 0;
    private int size = 0;
    // Set as private so that it can only be used in this class and no others. Set as public to be used in other classes.

    public static void main(String[] args)
    {
    new ticket();
    }
    public ticket()
    {
    setLayout(new BorderLayout());
    setSize(700, 150);
    setTitle("Redwich Trams - Ticket Machine");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel top = new JPanel();
    top.setLayout(new FlowLayout());
    top.add(new JLabel("Ticket Type: "));

    type.add(single);
    type.add(Return);
    type.add(zoneA);
    type.add(zoneAB);
    top.add(single);
    top.add(Return);
    top.add(zoneA);
    top.add(zoneAB);
    top.add(new JLabel(" Tickets"));
    top.add(numtick);
    single.addActionListener(this);
    Return.addActionListener(this);
    zoneA.addActionListener(this);
    zoneAB.addActionListener(this);

    add("North", top);
    setVisible(true);

    JPanel middle = new JPanel();
    middle.setLayout(new FlowLayout());
    middle.add(new JLabel("Ticket Type: "));
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOS E);
    middle.add(ten);
    middle.add(twenty);
    middle.add(fifty);
    middle.add(pound);
    middle.add(two);
    middle.add(five);
    middle.add(tenp);
    middle.add(twentyp);
    middle.add(cancel);
    ten.addActionListener(this);
    twenty.addActionListener(this);
    fifty.addActionListener(this);
    pound.addActionListener(this);
    two.addActionListener(this);
    five.addActionListener(this);
    tenp.addActionListener(this);
    twentyp.addActionListener(this);
    add("center", middle);
    setVisible(true);

    JPanel bottom = new JPanel();
    bottom.setLayout(new FlowLayout());
    bottom.add(new JLabel(" Ticket Cost:"));
    bottom.add(ticket);
    bottom.add(new JLabel(" Amount Paid:"));
    bottom.add(paid);
    bottom.add(new JLabel(" Amount Still To Pay:"));
    bottom.add(stp);
    stp.setEditable(false);
    bottom.add(new JLabel(" Change:"));
    bottom.add(change);
    ticket.setEditable(false);
    paid.setEditable(false);

    change.setEditable(false);
    ticket.setText("£0.00");
    stp.setText("£0.00");
    change.setText("£0.00");
    paid.setText("£0.00");
    add("South", bottom);
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource() == ten) amount -= 10;
    if (e.getSource() == twenty) amount -= 20;
    if (e.getSource() == fifty) amount -= 50;
    if (e.getSource() == pound) amount -= 100;
    if (e.getSource() == two) amount -= 200;
    if (e.getSource() == five) amount -= 500;
    if (e.getsource() == tenp) amount -= 1000;
    if (e.getsource() == twentyp) amount -= 2000;
    }

    double tp = 0;
    int noticket = Integer.parseInt(""+numticket.getValue());
    if (single.isSelected()) tp = 1.40;
    {

    ticket.setText(pounds.format(tp));
    stp.setText(pounds.format(tp));
    }

    if (Return.isSelected()) tp = 2.40;
    {

    ticket.setText(pounds.format(tp));
    stp.setText(pounds.format(tp));
    }

    if (zoneA.isSelected()) tp = 3.80;
    {
    JOptionPane.showMessageDialog(this, "Zone A (Between Kingstone Town and Parkon City (£3.80) has been selected.");
    ticket.setText(pounds.format(tp));
    stp.setText(pounds.format(tp));
    }

    if (zoneAB.isSelected()) tp = 5.50;
    {
    ticket.setText(pounds.format(tp));
    stp.setText(pounds.format(tp));
    }

    if (e.getSource() == cancel);
    {
    ticket.setText("£0.00");
    stp.setText("£0.00");
    change.setText("£0.00");
    paid.setText("£0.00");
    }

    if (e.getSource() == bExit) {
    System.exit(0);
    }

    }

    the error is

    ---------- javac ----------
    ticket.java:135: illegal start of type
    if (single.isSelected()) tp = 1.40;
    ^
    ticket.java:135: <identifier> expected
    if (single.isSelected()) tp = 1.40;
    ^
    ticket.java:135: ';' expected
    if (single.isSelected()) tp = 1.40;
    ^
    ticket.java:135: illegal start of type
    if (single.isSelected()) tp = 1.40;
    ^
    ticket.java:142: illegal start of type
    if (Return.isSelected()) tp = 2.40;
    ^
    ticket.java:142: <identifier> expected
    if (Return.isSelected()) tp = 2.40;
    ^
    ticket.java:142: ';' expected
    if (Return.isSelected()) tp = 2.40;
    ^
    ticket.java:142: illegal start of type
    if (Return.isSelected()) tp = 2.40;
    ^
    ticket.java:149: illegal start of type
    if (zoneA.isSelected()) tp = 3.80;
    ^
    ticket.java:149: <identifier> expected
    if (zoneA.isSelected()) tp = 3.80;
    ^
    ticket.java:149: ';' expected
    if (zoneA.isSelected()) tp = 3.80;
    ^
    ticket.java:149: illegal start of type
    if (zoneA.isSelected()) tp = 3.80;
    ^
    ticket.java:156: illegal start of type
    if (zoneAB.isSelected()) tp = 5.50;
    ^
    ticket.java:156: <identifier> expected
    if (zoneAB.isSelected()) tp = 5.50;
    ^
    ticket.java:156: ';' expected
    if (zoneAB.isSelected()) tp = 5.50;
    ^
    ticket.java:156: illegal start of type
    if (zoneAB.isSelected()) tp = 5.50;
    ^
    ticket.java:162: illegal start of type
    if (e.getSource() == cancel);
    ^
    ticket.java:162: <identifier> expected
    if (e.getSource() == cancel);
    ^
    ticket.java:162: ';' expected
    if (e.getSource() == cancel);
    ^
    ticket.java:162: illegal start of type
    if (e.getSource() == cancel);
    ^
    ticket.java:162: ';' expected
    if (e.getSource() == cancel);
    ^
    ticket.java:170: illegal start of type
    if (e.getSource() == bExit) {
    ^
    ticket.java:170: <identifier> expected
    if (e.getSource() == bExit) {
    ^
    ticket.java:170: ';' expected
    if (e.getSource() == bExit) {
    ^
    ticket.java:170: illegal start of type
    if (e.getSource() == bExit) {
    ^
    ticket.java:170: ';' expected
    if (e.getSource() == bExit) {
    ^
    26 errors

    Output completed (0 sec consumed) - Normal Termination

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Check your curly brackets; you have statements outside of the body of a method; that makes your compiler whine ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    dekon is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Radio Button
    By S-NESH in forum New To Java
    Replies: 6
    Last Post: 02-04-2011, 05:03 PM
  2. Customized Radio Button
    By Javified in forum AWT / Swing
    Replies: 6
    Last Post: 12-13-2010, 07:06 AM
  3. Radio Button help!
    By javanator in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 08:01 PM
  4. Radio button dot color
    By Psyclone in forum AWT / Swing
    Replies: 3
    Last Post: 02-21-2010, 04:29 AM
  5. How to use SWT Radio Button
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •