AyhamSYR
AyhamSYR

Reputation: 13

How to empty a label before displaying it again? in java

I am trying to make a calculator but I have a problem. When I display the answer more than one time the program put the answers over one another, as you can see in the picture below here.

Here is my code, could anyone please help me figure this out (emptying the label before displaying the next answer. Thanks!

   package Räknare;

        import javax.swing.JOptionPane;
        import javax.swing.JPanel;
        import javax.swing.JTextField;
        import javax.swing.ImageIcon;
        import javax.swing.JButton;

        import java.awt.BorderLayout;
        import java.awt.Component;
        import java.awt.Dimension;
        import java.awt.FlowLayout;
        import java.awt.Frame;
        import java.awt.Graphics;
        import java.awt.GraphicsConfiguration;
        import java.awt.LayoutManager;
        import java.awt.TextField;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        import javax.swing.JFrame;
        import javax.swing.JLabel;


        public class huvudklassen extends JFrame {
            static GraphicsConfiguration gc = null;

        // Variables 
            static JLabel welcome = new JLabel("Welcome to my calculator");
            static JLabel theequation = new JLabel("what do you want to caculate?");




            static JFrame frame1 = new JFrame(gc);
            static JFrame frame2 = new JFrame(gc);

            static JTextField s = new JTextField();

            static JButton start = new JButton("start");
            static JButton calculate = new JButton("Calculate");
        //
                public static void frame() {

                frame1.setResizable(false);
                frame1.setVisible(true);
                frame1.setSize(400, 500);
                frame1.setTitle("My Calculatur");
                frame1.setLayout(null);
                frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame1.setLocationRelativeTo(null);


                welcome.setBounds(120, 155, 200, 50);;
                start.setBounds(150,200,100,50);

                frame1.add(welcome);
                frame1.add(start);

                start.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                frame1.setVisible(false);
                frame2.setSize(400, 500);
                frame2.setResizable(false);
                frame2.setVisible(true);
                frame2.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame2.setLayout(null);
                frame2.setTitle("Caculation");
                frame2.setLocationRelativeTo(null);

                s.setBounds(150,200,100,30);
                theequation.setBounds(120,155,200,50);
                calculate.setBounds(120, 235, 150, 40);

                frame2.add(s);
                frame2.add(theequation);
                frame2.add(calculate);


                calculate.addActionListener(new ActionListener() {

                    public void actionPerformed1(ActionEvent a) {


                    String theequation2 = s.getText();

                    String left = "";
                    String tecken = "";
                    String right = "";

        //          if (theequation2 == null || theequation2.equals(""))


                    if (theequation2.contains("+")) {
                        left = left + theequation2.substring(0, theequation2.indexOf("+"));
                        right = right + theequation2.substring(theequation2.indexOf("+") + 1);
                        tecken = tecken + "+";
                        }

                    if (theequation2.contains("-")) {
                        left = left + theequation2.substring(0, theequation2.indexOf("-"));
                        right = right + theequation2.substring(theequation2.indexOf("-") + 1);
                        tecken = tecken + "-";
                        }

                    if (theequation2.contains("*")) {
                        left = left + theequation2.substring(0, theequation2.indexOf("*"));
                        right = right + theequation2.substring(theequation2.indexOf("*") + 1);
                        tecken = tecken + "*";
                        }

                    if (theequation2.contains("/")) {
                        left = left + theequation2.substring(0, theequation2.indexOf("/"));
                        right = right + theequation2.substring(theequation2.indexOf("/") + 1);
                        tecken = tecken + "/";
                        }

                    if (theequation2.contains("^")) {
                        left = left + theequation2.substring(0, theequation2.indexOf("^"));
                        right = right + theequation2.substring(theequation2.indexOf("^") + 1);
                        tecken = tecken + "^";
                        }

                    left = left.trim();
                    right = right.trim();   
                    double left1 = Double.parseDouble(left);
                    double right1 = Double.parseDouble(right);

                    double svar = 0;

                    if (tecken.equals("+"))
                        svar = svar + left1 + right1;

                    if (tecken.equals("-"))
                        svar = svar + (left1 - right1);

                    if (tecken.equals("*"))
                        svar = svar + left1 * right1;

                    if (tecken.equals("/"))
                        svar = svar + left1 * right1;

                    if (tecken.equals("%"))
                        svar = svar + left1 % right1;

                    if (tecken.equals("^")) {
                            svar = Math.pow(left1, right1); 
                    }

                    String answer1 = Double.toString(svar);
        //          JOptionPane.showMessageDialog(null, answer1);

                    JLabel answer = new JLabel();
                    answer.setBounds(180, 300, 150, 40);
                    answer.setText(answer1);


                    frame2.add(answer);
                    frame2.revalidate();
                    frame2.repaint();



                    }

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        ActionEvent ActionEvent = null;
                        actionPerformed1(ActionEvent);

                    }

                });

            }
                });
                }


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

        }



    JLabel answer = new JPanel();
    //          answer.setBounds(180, 300, 150, 40);

Upvotes: 0

Views: 445

Answers (1)

Charles
Charles

Reputation: 954

Instead of using a new label each time you should use the same label.

                JLabel answer = new JLabel();
                answer.setBounds(180, 300, 150, 40);
                answer.setText(answer1);

Upvotes: 2

Related Questions