Results 1 to 2 of 2
  1. #1
    Luwine is offline Member
    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0

    Default textField and Jbutton Question

    ok im building a calculator and im trying this....

    for(int i=0;i<9;i++)//---(numbers)
    {
    if(e.getSource()==button[i])
    textf.setText(button[i].getText()+textf.getText());
    }

    -when i click a button it get the string and set the string with the one that get

    -but when i click the (plus)(+) button it get the string convert it to long and setText in the textField...

    -but when i try to enter the second number it doesnt replace the old one it just do the same thing in the for loop (textf.setText(button[i].getText()+textf.getText())
    -so...if i was to get the sum of (2+2) it suppose tu be (4)...but the sum is (2+22) because i cant replace the number in the textField..
    - im new to java and trying like crazy, i didnt want to get help but man is annoying..

    Source Code

    //Simple Calculator
    //Manuel Berrios
    //June 18, 2013

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Hello_World extends JFrame implements ActionListener
    {
    private long counter;
    private JButton[] button;
    private JPanel panel1;
    private JPanel panel2;
    private JTextField textf;

    public Hello_World()
    {
    counter = 0;
    textf = new JTextField(500);
    button = new JButton[11];

    Panels_Buttons();
    Frames();
    }
    public void Frames()
    {
    setVisible(true);
    setSize(180,190);
    setTitle("Hello World");
    //setResizable(false);

    add(panel1, BorderLayout.NORTH);
    add(panel2);

    }
    public void Panels_Buttons()
    {
    textf.setHorizontalAlignment(JTextField.TRAILING);

    FlowLayout fl = new FlowLayout();

    panel1 = new JPanel(new BorderLayout());
    panel1.add(textf);

    button[0] = new JButton("1");
    button[1] = new JButton("2");
    button[2] = new JButton("3");
    button[3] = new JButton("4");
    button[4] = new JButton("5");
    button[5] = new JButton("6");
    button[6] = new JButton("7");
    button[7] = new JButton("8");
    button[8] = new JButton("9");
    button[9] = new JButton("+");
    button[10] = new JButton("=");

    panel2 = new JPanel(new BorderLayout());
    panel2.setLayout(fl);
    panel2.setBorder(BorderFactory.createLineBorder(Co lor.black));

    for(int i=0;i<11;i++)
    {
    panel2.add(button[i]);
    button[i].addActionListener(this);
    }
    }
    public void actionPerformed(ActionEvent e)
    {

    for(int i=0;i<9;i++)//---(numbers)
    {
    if(e.getSource()==button[i])
    textf.setText(button[i].getText()+textf.getText());
    }
    if(e.getSource()==button[9])//---(+) symbol
    {
    if(textf.getText().equals(""))
    {}
    else
    counter=counter+Long.parseLong(textf.getText().toS tring());
    textf.setText(Long.toString(counter));
    System.out.println(counter);

    }
    else if(e.getSource()==button[10])//---(=) Symbol
    {

    }
    }
    public static void main(String[] args)
    {
    new Hello_World();
    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

Similar Threads

  1. Replies: 1
    Last Post: 01-15-2013, 06:44 PM
  2. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  3. Won't set to textfield
    By Steffers in forum New To Java
    Replies: 7
    Last Post: 11-04-2010, 02:32 PM
  4. 1 question regarding textfield
    By henry in forum New To Java
    Replies: 3
    Last Post: 06-16-2010, 03:56 AM
  5. Replies: 4
    Last Post: 02-26-2009, 11:46 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
  •