Ok, i am just a beginner programmer, so i am having a lot of difficulty in figuring this out. Basically i am trying to create a one digit calculator(meaning that calculations only occur with single digits of numbers). I have created the buttons, assigned them action listener and their classes, and all those stuff. And then i try to display those numbers to a label. Now the problem i have is, that, i have a button, which when clicked, will use a class. From that class, what i want to do is, remove all the buttons form the panel, and add new buttons. But when i try to remove the buttons, something weird happens. If i click that button, the buttons instead of getting removed/disappering, they stay there, but i cant interact with them. Any help to fix that? I want to completely remove them from the panel. Then i want to add new buttons in their place.
Here is the code of the main class
package onecalculator;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class code {
static JLabel see = new JLabel("Int a");
static JLabel no = new JLabel("Int b");
static JLabel lol = new JLabel("Answer");
static JPanel area = new JPanel();
static JButton secn = new JButton("next");
static JButton one = new JButton("1");
static JButton two = new JButton("2");
static JButton three = new JButton("3");
static JButton four = new JButton("4");
static JButton five = new JButton("5");
static JButton six = new JButton("6");
static JButton seven = new JButton("7");
static JButton eight = new JButton("8");
static JButton nine = new JButton("9");
static JButton bone = new JButton("1");
static JButton btwo = new JButton("2");
static JButton bthree = new JButton("3");
static JButton bfour = new JButton("4");
static JButton bfive = new JButton("5");
static JButton bsix = new JButton("6");
static JButton bseven = new JButton("7");
static JButton beight = new JButton("8");
static JButton bnine = new JButton("9");
static JButton div = new JButton("div");
static JButton mul = new JButton("mul");
static JButton add = new JButton("add");
public int a;
public int b;
public static void main(String[] args) {
JFrame screen = new JFrame("One Digit Calculator");
screen.setSize(400,600);
screen.setResizable(false);
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.add(area);
area.add(see);
area.add(no);
area.add(lol);
area.add(secn);
area.add(one);
area.add(two);
area.add(three);
area.add(add);
area.add(four);
area.add(five);
area.add(six);
area.add(mul);
area.add(seven);
area.add(eight);
area.add(nine);
area.add(div);
secn.addActionListener(new secn());
two.addActionListener(new Twoc());
three.addActionListener(new Threec());
four.addActionListener(new Fourc());
five.addActionListener(new Fivec());
six.addActionListener(new Sixc());
seven.addActionListener(new Sevenc());
eight.addActionListener(new Eightc());
nine.addActionListener(new Ninec());
one.addActionListener(new Onec());
area.setLayout(new GridLayout(4,4));
screen.setVisible(true);
}
}
Then here is the code of the class that removes the buttons in the panel
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class secn implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
code.area.remove(code.one);
code.area.remove(code.two);
code.area.remove(code.three);
code.area.remove(code.four);
code.area.remove(code.five);
code.area.remove(code.six);
code.area.remove(code.seven);
code.area.remove(code.eight);
code.area.remove(code.nine);
code.area.add(code.bone);
code.area.add(code.btwo);
code.area.add(code.bthree);
code.area.add(code.bfour);
code.area.add(code.bfive);
code.area.add(code.bsix);
code.area.add(code.bseven);
code.area.add(code.beight);
code.area.add(code.bnine);
}
}
Please help.