I have made a Calculator and i need to add a Clear button but i don't know how to clear the screen in the program can someone tell me how i will be thank full. i have no errors or warnings.
This is the code.
This is the code.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calclator extends JFrame{
public JButton addbutton;
public JButton subtractbutton;
public JButton multiplicationbutton;
public JButton divisionbutton;
I will insert button here
public JTextField firstNumber;
public JLabel firstNumberLabel;
public JTextField secondNumber;
public JLabel secondNumberLabel;
public JTextField answer;
public JLabel answerLabel;
/*
*/
public Calclator(){
this.setTitle("Self-Calculator");
this.setSize(new Dimension(250,250));
this.setLocation(MouseInfo.getPointerInfo().getLocation());
this.setLayout(new FlowLayout());
this.setResizable(false);
this.addComponentListener(new ComponentListener(){
public void componentHidden(ComponentEvent arg0) {}
public void componentMoved(ComponentEvent arg0) {}
public void componentResized(ComponentEvent arg0) {}
public void componentShown(ComponentEvent arg0) {
Calclator_load();
}
});
this.addbutton = new JButton();
this.addbutton.setText("+");
this.addbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
addbutton_ActionPerformed();
}
});
this.subtractbutton = new JButton();
this.subtractbutton.setText("-");
this.subtractbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
subtractbutton_ActionPerformed();
}
});
this.multiplicationbutton = new JButton();
this.multiplicationbutton.setText("*");
this.multiplicationbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
multiplicationbutton_ActionPerformed();
}
});
this.divisionbutton = new JButton();
this.divisionbutton.setText("/");
this.divisionbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
divisionbutton_ActionPerformed();
}
});
this.firstNumber = new JTextField();
this.firstNumber.setText(" ");
this.firstNumberLabel = new JLabel();
this.firstNumberLabel.setText("FirstNumber");
this.secondNumber = new JTextField();
this.secondNumber.setText(" ");
this.secondNumberLabel = new JLabel();
this.secondNumberLabel.setText("SecondNumber");
this.answer = new JTextField();
this.answer.setText(" ");
this.answer.setEditable(false);
this.answerLabel = new JLabel();
this.answerLabel.setText("Answer");
add(addbutton);
add(subtractbutton);
add(multiplicationbutton);
add(divisionbutton);
add(firstNumberLabel);
add(firstNumber);
add(secondNumberLabel);
add(secondNumber);
add(answerLabel);
add(answer);
}
private void Calclator_load(){
firstNumber.setText("");
secondNumber.setText("");
answer.setText("");
}
private void addbutton_ActionPerformed(){
try{
int num1, num2;
num1 = Integer.parseInt(firstNumber.getText());
num2 = Integer.parseInt(secondNumber.getText());
int answer2 = num1+num2;
answer.setText(String.valueOf(answer2));
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Invalid Numbers");
}
}
private void subtractbutton_ActionPerformed(){
try{
int num1, num2;
num1 = Integer.parseInt(firstNumber.getText());
num2 = Integer.parseInt(secondNumber.getText());
int answer2 = num1-num2;
answer.setText(String.valueOf(answer2));
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Invalid Numbers");
}
}
private void multiplicationbutton_ActionPerformed(){
try{
int num1, num2;
num1 = Integer.parseInt(firstNumber.getText());
num2 = Integer.parseInt(secondNumber.getText());
int answer2 = num1*num2;
answer.setText(String.valueOf(answer2));
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Invalid Numbers");
}
}
private void divisionbutton_ActionPerformed(){
try{
int num1, num2;
num1 = Integer.parseInt(firstNumber.getText());
num2 = Integer.parseInt(secondNumber.getText());
int answer2 = num1/num2;
answer.setText(String.valueOf(answer2));
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Invalid Numbers");
}
}
Clear screen button function goes here
}
Comment