Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: i'm not sure whats wrong with the code for my button could someone help me with this

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i'm not sure whats wrong with the code for my button could someone help me with this

    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");
    	String id;
    	 JTextField ticket = new JTextField(5);
    	JTextField stp = new JTextField(5);
    	int amount = 0; 
    	JTextField change = new JTextField(5);
    	JTextField paid = new JTextField(5);
    	JButton ten = new JButton("10p");
    	JButton twenty = new JButton("20p");
    	JButton fifty = new JButton("50p");
    	JButton pound = new JButton("£1");
    	JButton two = new JButton("£2");
    	JButton five = new JButton("£5");
    	JButton tenp = new JButton("£10");
    	JButton twentyp = new JButton("£20");
    	JButton cancel = new JButton("Cancel");
    	JRadioButton single = new JRadioButton("single (£1.40)",true);
    	JRadioButton Return = new JRadioButton("return (£2.40)",true);
    	JRadioButton zoneA = new JRadioButton("Zone A (£3.80)",true);
    	JRadioButton zoneAB = new JRadioButton("Zone A B (£5.50)",true);
    	ButtonGroup type =new ButtonGroup();
    	// The spinner gives values from 1 to 10.
    	// Spinners allow safe entry of a number in a range.
    	 JSpinner numtick = new JSpinner(new SpinnerNumberModel(1, 0, 10, 1));
    	 int stpno = 0;
    	 private int size = 0;
     
     
    	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_CLOSE);
    		 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);
    		 cancel.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)
    			  paid= paid + 10;
    			  amount =amount - 10;
    			  if (ticket<paid)
    			  {
    				  change=ticket-paid;
    				  }
     
    		  if (e.getSource() == twenty)
    			  paid=paid+20;
    			  amount =amount - 20;
    			  if (ticket<paid)
    			  {
    				  change=ticket-paid;
    				  }
     
    	  if (e.getSource() == fifty)
    			  paid=paid+50;
    			  amount =amount - 50;
    			  if (ticket<paid)
    			  {
    				  change=ticket-paid;
    				  }
     
    				    if (e.getSource() == pound)
    			  paid=paid+100;
    			  amount =amount - 100;
    			  if (ticket<paid)
    			  {
    				  change=ticket-paid;
    				  }
     
    				    if (e.getSource() == two)
    			  paid=paid+200;
    			  amount =amount - 200;
    			  if (ticket<paid)
    			  {
    				  change=ticket-paid;
    				  }
     
    				    if (e.getSource() == five)
    			  paid=paid+500;
    			  amount =amount - 500;
    			  if (ticket<paid)
    			  {
    				  change=ticket-paid;
    				  }
     
    		  if (e.getSource() == tenp)
    			  paid=paid+1000;
    			  amount =amount - 1000;
    			  if (ticket<paid)
    			  {
    				  change=ticket-paid;
    				  }
     
    		  if (e.getSource() == twentyp)
    			  paid=paid+2000;
    			  amount =amount - 2000;
    			  if (ticket<paid)
    			  {
    				  change=ticket-paid;
    				  }
    	}
     
    	{
    double tp = 0;
    	  int noticket = Integer.parseInt(""+numtick.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;
    			  {
    			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"); 
    		 }
     
            }
    		  }
     }
    i'm not sure why my button code is not working it looks fine to me but theres errors with it can someone help me out?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: i'm not sure whats wrong with the code for my button could someone help me with t

    button code is not working
    Please explain and post any error messages.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: i'm not sure whats wrong with the code for my button could someone help me with t

    No errors you say? Hmm, my bleeding IDE says otherwise :3.
    Using '+' and '<' operators on JTextField's isn't valid syntax.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM
  2. Whats wrong with my code!!
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2011, 11:45 AM
  3. Whats Wrong with this code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 10:59 PM
  4. Whats wrong with my code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2011, 05:34 PM
  5. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 01:42 PM