Results 1 to 8 of 8

Thread: button problem

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

    Default button problem

    hi i'm having trouble with my button could someone help me with this please

    thanks

    PHP Code:
      if (e.getSource() == ten)
    			  paid=paid + 10;
    			  amount =amount - 10;
    			  if (ticket<paid)
    			  {
    				  change=ticket-paid;
    				  }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What's the problem? All you've done is show a small snippet and state that you have a problem. Specific questions are needed to give you good answers.

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

    Default

    sorry about that basically i've been creating a button inwhich once clicked it will put the number 10 into a textarea box which i've called paid. with this it will take away how much that was paid from the textarea i've called ticket and thus once clicked it will pay 10

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

    Default

    the errors i've been getting are the following when i compile it

    ---------- javac ----------
    ticket.java:127: operator + cannot be applied to javax.swing.JTextField,int
    paid=paid + 10;
    ^
    ticket.java:129: operator < cannot be applied to javax.swing.JTextField,javax.swing.JTextField
    if (ticket<paid)
    ^
    ticket.java:131: operator - cannot be applied to javax.swing.JTextField,javax.swing.JTextField
    change=ticket-paid;
    ^

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Still not enough information.

    Post your Short, Self Contained, Correct Example that demonstrates the problem.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you want to modify a text field, you must use the proper methods to access them. Here is a quick calculator example:

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class GUICalc{
    	private JFrame frame;
    	private JPanel panel;
    	private JButton add;
    	private JTextField addend;
    	private JTextField rValue;
    	private JLabel sum;
    	private JLabel equals;
    	private JLabel plus;
    	
    	public GUICalc(){
    		frame = new JFrame("Calculater");
    		panel = new JPanel();
    		addend = new JTextField(5);
    		rValue = new JTextField(5);
    		sum = new JLabel("0");
    		equals = new JLabel(" = ");
    		plus = new JLabel(" + ");
    		add = new JButton("Add");
    		
    		add.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				int l = (addend.getText().equals("")) ? 0 : Integer.parseInt(addend.getText());
    				int r = (rValue.getText().equals("")) ? 0 : Integer.parseInt(rValue.getText());
    				sum.setText(l + r + "");
    				frame.pack();
    			}
    		});
    		
    		panel.add(addend);
    		panel.add(plus);
    		panel.add(rValue);
    		panel.add(equals);
    		panel.add(sum);
    		panel.add(add);
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(panel);
    		frame.pack();
    		frame.setVisible(true);
    	}
    	
    	public static void main(String[] args){
    		GUICalc gc = new GUICalc();
    	}
    }

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

    Default

    this is what i'm trying to do with most of the buttons from the code are gone so theres only one
    Java Code:
    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 ticket1 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 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 ticket1();
    	}
    	public ticket1()
    	{
    		  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(cancel);
    		 ten.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;
    				  }
    
    	{
    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"); 
    		 }
    
            }
    		  }
     }
    }

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    The posted code doesn't compile.

    Your question is about a button and a text field. So create your SSCCE from scratch. It should contain only a text field and a button. When you click on the button some text is displayed in the text field. Then you add a second button. Start small and build big instead of writing the entire program first and then start debugging it.

Similar Threads

  1. Problem with button.setEnabled
    By Dycu in forum SWT / JFace
    Replies: 0
    Last Post: 07-19-2010, 12:49 PM
  2. problem in my button
    By jperson in forum New To Java
    Replies: 4
    Last Post: 03-13-2010, 06:02 PM
  3. Problem w/ ActionListener on a button
    By qwertyuiop23 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2009, 06:25 AM
  4. problem with back button of the browser.
    By shivakumari in forum Java Servlet
    Replies: 2
    Last Post: 03-25-2009, 07:41 AM
  5. problem on button
    By udayforu_1986 in forum Java Applets
    Replies: 1
    Last Post: 08-03-2008, 01:57 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
  •