prog. using radio buttons for simple calculator
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Calculator extends JFrame {
private final Font BIGGER_FONT = new Font("monspaced", Font.PLAIN, 20);
private JTextField textfield;
private boolean number = true;
private String equalOp = "=";
private CalculatorOp op = new CalculatorOp();
public Calculator() {
textfield = new JTextField("0", 12);
textfield.setHorizontalAlignment(JTextField.RIGHT);
textfield.setFont(BIGGER_FONT);
ActionListener numberListener = new NumberListener();
String buttonOrder = "1234567890 ";
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 4, 4));
for (int i = 0; i < buttonOrder.length(); i++) {
String key = buttonOrder.substring(i, i+1);
if (key.equals(" ")) {
buttonPanel.add(new JLabel(""));
} else {
JButton button = new JButton(key);
button.addActionListener(numberListener);
button.setFont(BIGGER_FONT);
buttonPanel.add(button);
}
}
ActionListener operatorListener = new OperatorListener();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 4, 4));
String[] opOrder = {"+", "-", "*", "/","=","C"};
for (int i = 0; i < opOrder.length; i++) {
JButton button = new JButton(opOrder[i]);
button.addActionListener(operatorListener);
button.setFont(BIGGER_FONT);
panel.add(button);
}
JPanel pan = new JPanel();
pan.setLayout(new BorderLayout(4, 4));
pan.add(textfield, BorderLayout.NORTH );
pan.add(buttonPanel , BorderLayout.CENTER);
pan.add(panel , BorderLayout.EAST );
this.setContentPane(pan);
this.pack();
this.setTitle("Calculator");
this.setResizable(false);
}
private void action() {
number = true;
textfield.setText("0");
equalOp = "=";
op.setTotal("0");
}
class OperatorListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (number) {
action();
textfield.setText("0");
} else {
number = true;
String displayText = textfield.getText();
if (equalOp.equals("=")) {
op.setTotal(displayText);
} else if (equalOp.equals("+")) {
op.add(displayText);
} else if (equalOp.equals("-")) {
op.subtract(displayText);
} else if (equalOp.equals("*")) {
op.multiply(displayText);
} else if (equalOp.equals("/")) {
op.divide(displayText);
}
textfield.setText("" + op.getTotalString());
equalOp = e.getActionCommand();
}
}
}
class NumberListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String digit = event.getActionCommand();
if (number) {
textfield.setText(digit);
number = false;
} else {
textfield.setText(textfield.getText() + digit);
}
}
}
public class CalculatorOp {
private int total;
public CalculatorOp() {
total = 0;
}
public String getTotalString() {
return ""+total;
}
public void setTotal(String n) {
total = convertToNumber(n);
}
public void add(String n) {
total += convertToNumber(n);
}
public void subtract(String n) {
total -= convertToNumber(n);
}
public void multiply(String n) {
total *= convertToNumber(n);
}
public void divide(String n) {
total /= convertToNumber(n);
}
private int convertToNumber(String n) {
return Integer.parseInt(n);
}
}
}
View Answers
Related Tutorials/Questions & Answers:
prog. using radio buttons for simple calculatorprog.
using radio buttons for
simple calculator import java.awt....*;
class
Calculator extends JFrame {
private final Font BIGGER_FONT = new Font... = new CalculatorOp();
public
Calculator() {
textfield = new JTextField("0
prog. using radio buttons for simple calculatorprog.
using radio buttons for
simple calculator import java.awt....*;
class
Calculator extends JFrame {
private final Font BIGGER_FONT = new Font... = new CalculatorOp();
public
Calculator() {
textfield = new JTextField("0
Advertisements
3 RADIO BUTTONS3
RADIO BUTTONS 3
RADIO BUTTONS USING ALSO ONE
RADIO CLICKING HOW TO PROCESS OF NEXTPAGE
radio buttonsradio buttons write a program to create an applet button which has a list of
radio buttons with titles of various colours.set the background colour... has a list of
radio buttons with titles of various colors and a button
Radio Buttons - Java Beginners and answer page
using the
radio buttons.please help me to solve...
Radio Buttons Hello Sir,
How to create the code for the password recovery page(like forgot gmail password question and answer page)
using simple calculator - Java Beginnerssimple calculator how can i create a
simple calculator using java codes? Hi Friend,
Please visit the following link:
http://www.roseindia.net/java/example/java/swing/
calculator-in-swing.shtml
Thanks
Radio Buttons in Jsp - JSP-ServletRadio Buttons in Jsp Hi,
i have a page in which there are lot of
radio buttons
[IMG]http://i38.tinypic.com/jzi6vc.jpg[/IMG]
see the above picture..."
depending on the value in the String
radio button has to be checked.
How to do
Simple Java Calculator - Java BeginnersSimple Java Calculator Write a Java program to create
simple Calculator for 4 basic Math operations,
Addition, Subtraction, Multiplication and Division.
The
calculator should simulate the look of handheld
calculator containing
Radio Buttons in DB Very Urgent - JSP-ServletRadio Buttons in DB Very Urgent Respected Sir/Madam,
I am... in the database.Here I need
Radio Buttons added dynamically for each Row. When I click the corresponding
Radio Button and Submit,The Emp ID and Emp Name must
Tab sequence problem with Radio buttons - JSP-ServletTab sequence problem with
Radio buttons Hi,
I have membership type in application as
1 year(
radio button)
2 year(
radio button)
4 year(
radio button)
courier delivery
courier(
radio button)
currently tab sequence going
Java radio buttons and button groups two valuesJava
radio buttons and button groups two values hi i have a problem.
i have a system to input data from a jform to a mysql database. i have made all of it except the gender radiobuttons. how do i do this? i need when register
Java radio buttons and button groups two valuesJava
radio buttons and button groups two values hi i have a problem.
i have a system to input data from a jform to a mysql database. i have made all of it except the gender radiobuttons. how do i do this? i need when register
CalculatorCalculator need a
simple java program to degin a
CALCULATOR without
using ADVANCED JAVA....
Calculator in Java Swing
Simple Calculator Application In Java Script
Simple Calculator Application In Java
Script
...
Calculator
The objective of this project is learn how to write a
simple... a
simple JavaScript
calculator that can add, subtract, multiply or divide two
ButtonsButtons I have created a web page with
radio button group with two
radio buttons for accepting the home appliances categories,Kitchen appliances...
radio button is selected.Which event listener do I need to implement for this task
Radio Buttons in HTML
Radio Buttons in HTML
..._TO_REPLACE_1
The Tutorial illustrates an example from
Radio Buttons in HTML.In this Tutorial, the
code explain to create a
Radio Buttons. The code enables a user
Radio button validation using jsp - JSP-ServletRadio button validation
using jsp I had one jsp Page and servlet. I did my validations in servlet for my jsp page which contains the
radio... a value for
radio Buttons) then it will return to same jsp page with the given
Using radio button in struts - StrutsUsing radio button in struts Hello to all ,
I have a big problem... options to choose :
Serial number 12345
radio button - selection 1
radio button - selection 2
Serial number 6789
radio button - selection 1
radio button
Radio buttons in htmlRadio buttons in html
Here is an example of
radio button in html.In this example we have display
two
radio button Male and Female. The user select only one...;b>
radio buttons in html</b></font></center></td>
<
Graphical calculator using AWT - Java BeginnersGraphical
calculator using AWT hi Sir,
I need a source code for the following prgm...pls help me..
Implement a
simple graphical
calculator using AWT.The
calculator shd perform
simple operation like addition, subtraction
Create Radio Buttons in SWT
Create
Radio Buttons in SWT
This section illustrates you how to create
radio button.
In SWT, the style
RADIO defined in the Button class allows to create
radio
button. We
Graphical calculator using AWT - Java BeginnersGraphical
calculator using AWT Hi Sir,
Thanks for the reply...);
this.pack();
this.setTitle("
Simple Calculator");
this.setResizable(false.....and is it the same code we need implement on Graphical Calc
using Swing?
Here is the prg code
Simple application using hibernateSimple application
using hibernate Hi,
How to write a
simple application
using hibernate in Eclipse IDE?
I would like to to learn:
Process... of the tutorial you will like to learn for creating the
simple application
using turbo C prog.turbo C
prog. Is is possible print ASCII value of a alphabets
using turbo C
prog Dojo Radio Button;
In this section, you will learn how to create
radio
buttons in dojo... dojo.require(dijit.form.CheckBox) for RadioButtons
to work.
[
Radio buttons
are used when... in the list.]
Radio Buttons are the same
as html but dojo provides more
Simple banking system using JavaSimple banking system
using Java I am trying to make a
simple... and the other is the branch, the third interface is the main class.
Using inheritance the branch is supposed to inherit the activities of the main office...
using simple web applications using log4jsimple web applications
using log4j i am getting java.lang.NoClassDefFoundError: org/apache/log4j/Logger and javax.servlet.ServletException: Error instantiating servlet class Sample exceptions when trying to execute. this is my
NEED A PROG NEED A
PROG whats the program to add,delete, display elements of an object
using collecions. without
using linked list
Hi Friend,
Try the following code:
import java.util.*;
class CollectionExample
{
public
Hide buttons in Java SwingHide
buttons in Java Swing
In this section, you will learn how to hide
buttons using java swing. For
this, we have created three
buttons, A, B and C. We have performed an action on
C. When you click the button 'C',
buttons A and B
calculator midletcalculator midlet give me code
calculator midlet in bluetooth application with j2me
Struts 2 Radio ButtonStruts 2
Radio Button I have a search functionlaity where i have two
radio buttons and
I am
using Struts2 tag.I want first rado button to be selected by default.But when i select the second
radio button and hit on search