I'm currently working in a simple calculator project and I found myself in a small problem. I'm not able to write on the text field using the buttons. I want to be able to write on the text using the buttons on the frame.
Can someone please help me?
Here's my code:
import javax.swing.*;
import java.awt.*;
public class Calculator extends JFrame{
JPanel NumberPanel = new JPanel();
String [] ButtonString = {"7","8","9","4","5","6","1","2","3","0",".","+/-"};
JButton ButtonArray [] = new JButton[ButtonString.length];
JPanel OperationPanel = new JPanel();
String [] OperationString = {"Erase","Ac","*","/","+","-","Ans","="};
JButton [] OperationArray = new JButton [OperationString.length];
Calculator(){
for (int a = 0 ; a < ButtonArray.length ; a++){
ButtonArray[a]= new JButton (ButtonString[a]);
}
NumberPanel.setLayout(new GridLayout(4,3,5,5));
for (int a = 0 ; a < ButtonArray.length ; a++){
NumberPanel.add(ButtonArray[a]);
}
for (int a = 0 ; a < OperationArray.length ; a++){
OperationArray[a]= new JButton(OperationString[a]);
}
OperationPanel.setLayout(new GridLayout(4,2,5,5));
for (int a = 0 ; a < OperationArray.length ; a++){
OperationPanel.add(OperationArray[a]);
}
JPanel Finalpanel = new JPanel();
Finalpanel.setLayout(new FlowLayout());
Finalpanel.add(NumberPanel);Finalpanel.add(OperationPanel);
JTextField WritingZone = new JTextField(27);
WritingZone.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
WritingZone.setBorder(BorderFactory.createLoweredSoftBevelBorder());
WritingZone.setEditable(false);
JPanel TextPanel = new JPanel();
TextPanel.add(WritingZone);
JPanel AllPanel = new JPanel();
AllPanel.setLayout(new BorderLayout(5,5));
AllPanel.add(BorderLayout.NORTH, TextPanel);
AllPanel.add(BorderLayout.CENTER, Finalpanel);
AllPanel.setBorder(BorderFactory.createTitledBorder("Simple Calculator"));
add(AllPanel);
}
public static void main (String [] arg){
JFrame frame = new Calculator();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(400,250);
frame.setResizable(false);
}
}