Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
package lab5;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener {
 
private final Container con = getContentPane();
JLabel prompt = new JLabel("First Number");
JLabel prompt2 = new JLabel("Second Number");
JLabel result = new JLabel();
Font promptFont = new Font("Times", Font.BOLD, 12);
Font resultFont = new Font("Times", Font.BOLD, 20);

JTextField input = new JTextField(20);
JTextField input2 = new JTextField(20);

JButton add = new JButton("Add");
JButton subtract = new JButton("Subtract");
JButton multiply = new JButton("Multiply");
JButton divide = new JButton("Divide");

public Calculator(){
 
 super("CALCULATOR");
 setSize(300,300);
 con.setLayout(new FlowLayout());
 prompt.setFont(promptFont);
 prompt2.setFont(promptFont);
 result.setFont(resultFont);
 
 con.add(prompt);
 con.add(input);
 con.add(prompt2);
 con.add(input2);
 con.add(add);
 con.add(subtract);
 con.add(multiply);
 con.add(divide);
 con.add(result);

 add.addActionListener(this);
 subtract.addActionListener(this);
 multiply.addActionListener(this);
 divide.addActionListener(this);
   
} 
public static void main(String[] args){
 
 Calculator demo = new Calculator();
 demo.setVisible(true);
 
}

@Override
public void actionPerformed(ActionEvent e) {
 
 Object source = e.getSource();
 if(source == add)
 {
  String ans1 = input.getText();
  int num1 = Integer.parseInt(ans1);
  String ans2 = input2.getText();
  int num2 = Integer.parseInt(ans2); 
  
  int RESULT = num1 + num2;
  String res = Integer.toString(RESULT);
  result.setText("The result is" + res);       
 }
 
 if(source == subtract)
 {
  String ans1 = input.getText();
  int num1 = Integer.parseInt(ans1);
  String ans2 = input2.getText();
  int num2 = Integer.parseInt(ans2); 
  
  int RESULT = num1 - num2;
  String res = Integer.toString(RESULT);
  result.setText("The result is" + res); 
 }
 
 if(source == multiply)
 {
  String ans1 = input.getText();
  int num1 = Integer.parseInt(ans1);
  String ans2 = input2.getText();
  int num2 = Integer.parseInt(ans2); 
  
  int RESULT = num1 * num2;
  String res = Integer.toString(RESULT);
  result.setText("The result is" + res);
 }
 
 if(source == divide)
 {
  String ans1 = input.getText();
  double num1 = Double.parseDouble(ans1);
  String ans2 = input2.getText();
  double num2 = Double.parseDouble(ans2); 
  
  Double RESULT = num1 / num2;
  String res = RESULT.toString();
  result.setText("The result is" + res);  
 }
 
}

}


What I have tried:

I've tried this and it didn't work

ExitValidator validator = new ExitValidator();
JTextField bla = bew JTextField(2);
bla.addFocusListener(validator);
Posted
Updated 2-Nov-22 5:10am

1 solution

Plenty of suggested options from Google: jtextfield numeric only - Google Search[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900