I'm trying to make my first real project: A calculator, but when I press the "clear" button (the only button i've done so far) the JLabel doesn't go from two to zero, any help would be really appreciated :)

code:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.*;
public class Calculator extends JFrame implements ActionListener
{

	float num1 = 0;
	float num2 = 2;
	float answerWindow = num1 + num2;
	
	public static void main(String[] args) 
	{
		new Calculator();
		

	}
	
	public Calculator()
	{
		
		String ansString = Float.toString(answerWindow);
		JLabel labelAnsWin = new JLabel(ansString);
		this.setSize(250, 350);
		this.setResizable(false);
		this.setTitle("Calculator");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		Color dark_green = new Color(10,10,255);
		labelAnsWin.setBorder(BorderFactory.createLineBorder(Color.black));
		labelAnsWin.setOpaque(true);
		labelAnsWin.setBackground(dark_green = new Color(10,225,25));
		labelAnsWin.setHorizontalAlignment( SwingConstants.CENTER );
		this.setLayout(new GridLayout(6, 3, 5, 5));
		this.setBackground(Color.LIGHT_GRAY);
		JButton ON = new JButton("ON");
		JButton C = new JButton("C");
		JButton one = new JButton("1");
		JButton two = new JButton("2");
		JButton three = new JButton("3");
		JButton four = new JButton("4");
		JButton five = new JButton("5");
		JButton six = new JButton("6");
		JButton seven = new JButton("7");
		JButton eight = new JButton("8");
		JButton nine = new JButton("9");
		JButton zero = new JButton("0");
		JButton EQ = new JButton("=");
		JButton PL = new JButton("+");
		JButton MI = new JButton("-");
		JButton MU = new JButton("x");
		JButton DI = new JButton("%");
		this.add(ON);
		this.add(labelAnsWin);
		this.add(C);
		this.add(one);
		this.add(two);
		this.add(three);
		this.add(four);
		this.add(five);
		this.add(six);
		this.add(seven);
		this.add(eight);
		this.add(nine);
		this.add(zero);
		this.add(EQ);
		this.add(PL);
		this.add(MI);
		this.add(MU);
		this.add(DI);
		this.setVisible(true);
		C.addActionListener(new ActionListener()

		{
			
		public void actionPerformed(ActionEvent C)
		{
			Events events = new Events();
			events.erase();
		}
		
		});
	}

	
	public void actionPerformed(ActionEvent dnu) {
		
		
	}

}

well .. I'm not sure what you want that 'Events' to do but ..
anyway, you'll want to declare your labelAnsWin outside of your main method, that way you'll be able to use it, or set its value outside of the main method.
and then, you'll replace that:

Events events = new Events();
events.erase();

by

labelAnsWin.setText("");

I don't know anything about your Events class, but since you've placed that JLabel completely in the scope of the main method, it's (afaic) impossible to (re-)set the value from within your Events class anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.