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 2 of 2

Thread: Can any one help me to design layout of this Calculator and add one more button to clear textbox one by one

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Can any one help me to design layout of this Calculator and add one more button to clear textbox one by one

    Hi all
    can any one help me to design the layout of this calculator and also add one more button
    that clears textbox strings one by one instead of whole.

    import javax.swing.*;
    import java.awt.FlowLayout;
    import java.awt.event.*;
    public class Calculator extends JFrame{
    double value1;
    double value2;
    String operator;
    double result;
    JTextField txt1;
    JButton btn1;
    JButton btn2;
    JButton btn3;
    JButton btndivide;

    JButton btn4;
    JButton btn5;
    JButton btn6;
    JButton btnmult;

    JButton btn7;
    JButton btn8;
    JButton btn9;
    JButton btnminus;

    JButton btn0;
    JButton btndot;
    JButton btnequel;
    JButton btnplus;
    JButton clear;
    public Calculator()
    {
    super("Calculator");
    setLayout(new FlowLayout());
    txt1=new JTextField(15);
    btn1=new JButton("1");
    btn2=new JButton("2");
    btn3=new JButton("3");
    btndivide=new JButton("/");



    btn4=new JButton("4");
    btn5=new JButton("5");
    btn6=new JButton("6");
    btnmult=new JButton("*");


    btn7=new JButton("7");
    btn8=new JButton("8");
    btn9=new JButton("9");
    btnminus=new JButton("-");


    btn0=new JButton("0");
    btndot=new JButton(".");
    btnequel=new JButton("=");
    btnplus=new JButton("+");
    clear=new JButton("CLEAR");





    add(txt1);
    add(btn1);
    add(btn2);
    add(btn3);
    add(btndivide);

    add(btn4);
    add(btn5);
    add(btn6);
    add(btnmult);

    add(btn7);
    add(btn8);
    add(btn9);
    add(btnminus);

    add(btn0);
    add(btndot);
    add(btnequel);
    add(btnplus);
    add(clear);
    ButtonHolder holder=new ButtonHolder();
    btn1.addActionListener(holder);
    btn2.addActionListener(holder);
    btn3.addActionListener(holder);
    btn4.addActionListener(holder);
    btn5.addActionListener(holder);
    btn6.addActionListener(holder);
    btn7.addActionListener(holder);
    btn8.addActionListener(holder);
    btn9.addActionListener(holder);
    btn0.addActionListener(holder);
    btndot.addActionListener(holder);
    btnplus.addActionListener(holder);
    btnminus.addActionListener(holder);
    btnmult.addActionListener(holder);
    btndivide.addActionListener(holder);
    btnequel.addActionListener(holder);
    clear.addActionListener(holder);
    }
    private class ButtonHolder implements ActionListener {
    public void actionPerformed(ActionEvent event)

    {
    if (event.getSource()==btn1)
    {
    txt1.setText(txt1.getText()+"1");
    }
    if (event.getSource()==btn2)
    {
    txt1.setText(txt1.getText()+"2");
    }
    if (event.getSource()==btn3)
    {
    txt1.setText(txt1.getText()+"3");
    }
    if (event.getSource()==btn4)
    {
    txt1.setText(txt1.getText()+"4");
    }
    if (event.getSource()==btn5)
    {
    txt1.setText(txt1.getText()+"5");
    }
    if (event.getSource()==btn6)
    {
    txt1.setText(txt1.getText()+"6");
    }
    if (event.getSource()==btn7)
    {
    txt1.setText(txt1.getText()+"7");
    }
    if (event.getSource()==btn8)
    {
    txt1.setText(txt1.getText()+"8");
    }
    if (event.getSource()==btn9)
    {
    txt1.setText(txt1.getText()+"9");
    }
    if (event.getSource()==btn0)
    {
    txt1.setText(txt1.getText()+"0");
    }
    if (event.getSource()==btndot)
    {
    if (txt1.getText().contains("."))
    {
    return;
    }
    txt1.setText(txt1.getText()+".");

    }
    if (event.getSource()==btnplus)
    {

    value1=Double.parseDouble(txt1.getText());
    operator="+";
    txt1.setText("");
    //JOptionPane.showMessageDialog(rootPane, operator);
    }
    if (event.getSource()==btnequel)
    {

    value2=Double.parseDouble(txt1.getText());
    if (operator=="+")
    {
    result=value1+value2;
    txt1.setText(String.valueOf(result));
    }
    if (operator=="-")
    {
    result=value1-value2;
    txt1.setText(String.valueOf(result));
    }
    if (operator=="*")
    {
    result=value1*value2;
    txt1.setText(String.valueOf(result));
    }
    if (operator=="/")
    {
    result=value1/value2;
    txt1.setText(String.valueOf(result));
    }
    }
    if (event.getSource()==btnminus)
    {
    value1=Double.parseDouble(txt1.getText());
    operator="-";
    txt1.setText("");
    }
    if (event.getSource()==btnmult)
    {
    value1=Double.parseDouble(txt1.getText());
    operator="*";
    txt1.setText("");
    }
    if (event.getSource()==btndivide)
    {
    value1=Double.parseDouble(txt1.getText());
    operator="/";
    txt1.setText("");
    }
    if (event.getSource()==clear)
    {
    txt1.setText("");
    }
    }


    }
    public static void main(String[] args)
    {
    Calculator cal=new Calculator();
    cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    cal.setSize(200,210);
    cal.setVisible(true);
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Can any one help me to design layout of this Calculator and add one more button to clear textbox one by one

    Since I missed you the first time, welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.

    You've posted too much code incorrectly, without code or highlight tags and formatting, making it difficult to read. Please correct your post so that the code is posted per the link I've provided.

Similar Threads

  1. why does the clear sign not work on my calculator?
    By gopster01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 16th, 2014, 09:44 AM
  2. Interest Calculator Design
    By heyya99 in forum Object Oriented Programming
    Replies: 4
    Last Post: April 10th, 2013, 08:07 AM
  3. let an HTML button and textbox change a java parameter
    By marlon006 in forum Java Theory & Questions
    Replies: 7
    Last Post: February 26th, 2013, 04:15 PM
  4. Calculator layout problem
    By Choiseymitsu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2011, 08:04 AM
  5. My Clear Info button is not working with Screenshot
    By drkossa in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 15th, 2010, 08:06 AM