prog. using radio buttons for simple calculator

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 calculator
prog. 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 calculator
prog. 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 BUTTONS
3 RADIO BUTTONS  3 RADIO BUTTONS USING ALSO ONE RADIO CLICKING HOW TO PROCESS OF NEXTPAGE
radio buttons
radio 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 program in javascript
simple calculator program in javascript  strong textsimple calculator in javascript
simple calculator - Java Beginners
simple 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-Servlet
Radio 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 Beginners
Simple 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-Servlet
Radio 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-Servlet
Tab 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 values
Java 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 values
Java 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
ModuleNotFoundError: No module named 'koji-simple-calculator'
ModuleNotFoundError: No module named 'koji-simple-calculator'  Hi...: No module named 'koji-simple-calculator' How to remove the ModuleNotFoundError: No module named 'koji-simple-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'koji-simple-calculator'
ModuleNotFoundError: No module named 'koji-simple-calculator'  Hi...: No module named 'koji-simple-calculator' How to remove the ModuleNotFoundError: No module named 'koji-simple-calculator' error? Thanks   Hi
Calculator
Calculator  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
WRITE A CODE IN STRUTS ACTION CLASS FOR CHECK BOXES AND RADIO BUTTONS - Struts
for check boxes and radio buttons and for submit buttons. i have a jsp page which contains check boxes,radio buttons.and when i click submit button the related...WRITE A CODE IN STRUTS ACTION CLASS FOR CHECK BOXES AND RADIO BUTTONS 
inserting data from radio buttons to database - JSP-Servlet
inserting data from radio buttons to database  hi, i am getting error in my code of feedback form. once i finished selecting all the radio buttons and when i click on delete button. it points to the answer.jsp. answer.jsp
inserting data from radio buttons to database - JSP-Servlet
inserting data from radio buttons to database  hi, my problem... of radio buttons. the feedback.jsp should look like same as follows: Please... as per his choice. when user completes all the selection of radio buttons and he
code to enter values in multiple textfields throught 10 buttons (0-9) (as if a calculator programe)
code to enter values in multiple textfields throught 10 buttons (0-9) (as if a calculator programe)  how can i enter values in two different textfield's through 10 different buttons.?? as we saw a online calculator type
validate radio button using javascript
validate radio button using javascript  validate radio button using javascript
Multiple buttons in struts using java script
Multiple buttons in struts using java script  Multiple buttons in struts using java script
how to upload and download images using buttons in jsp?
how to upload and download images using buttons in jsp?  how to upload and download images using buttons in jsp
Display Label and radio buttons runtime with respect to DB values
Display Label and radio buttons runtime with respect to DB values  Requirement: I am getting alertCondition,Y,W values from DB the the cooresponding fields like the following: JLbel- alertCondition-"alert1","Alert2" JradioButton
Buttons
Buttons  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
inserting data from radio buttons to database-ERROR - JSP-Servlet
inserting data from radio buttons to database-ERROR  hi, i have already changed the option field of table question. the code which you give me has the option field as options. but it is option in the table,so i changed
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-Servlet
Radio 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 - Struts
Using 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 html
Radio 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 Beginners
Graphical 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 Beginners
Graphical 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
need help for writting code in struts action class for check boxes and radio buttons - Struts
people.iwould like to write code in struts action class for check boxes and radio buttons and for submit buttons. i have a jsp page which contains check boxes,radio...need help for writting code in struts action class for check boxes and radio
how to access radio buttons selected in a servlet??...iam unable to retrieve values...it is returning NULL
how to access radio buttons selected in a servlet??...iam unable to retrieve...; <p>4.what is currency of USA?</p> <input type="radio...; <input type="radio" name="d" onclick="getAnswer('b')" value="b "/>pound
how to access radio buttons selected in a servlet??...iam unable to retrieve values...it is returning NULL
how to access radio buttons selected in a servlet??...iam unable to retrieve...; <p>4.what is currency of USA?</p> <input type="radio...; <input type="radio" name="d" onclick="getAnswer('b')" value="b "/>pound
Simple application using hibernate
Simple 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 Java
Simple 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 log4j
simple 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
I'm using struts2,i'm asking about the radio button validation <s:radio>
I'm using struts2,i'm asking about the radio button validation   my form < s: form action="creatingcv" method="post" onsubmit="return valider()" id="frmSaisie"> < s:radio name="cv.gender" label="Gender" list="{'Male
I'm using struts2,i'm asking about the radio button validation <s:radio>
I'm using struts2,i'm asking about the radio button validation    i used this javascript code but it didnt work function valider(){ frm=document.forms['frmSaisie']; if(frm.elements['Gender'].value
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 Swing
Hide 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
Hibernate Simple Program Using Java Application with Eclipse
Hibernate Simple Program Using Java Application with Eclipse  How to write Hibernate Simple Program Using Java Application with Eclipse? I wish to learn Hibernate using very popular Eclipse IDE. Share me the best tutorial link
calculator midlet
calculator midlet  give me code calculator midlet in bluetooth application with j2me
total time calculator (starttime+endtime) in servlet USING DATABASE IN SQL2000 PLZ ANY ONE HELPME
total time calculator (starttime+endtime) in servlet USING DATABASE IN SQL2000 PLZ ANY ONE HELPME   <%-- Document : insertdate Created on : Jul 19, 2013, 3:48:28 PM Author : CG0T4908 --%> <
Struts 2 Radio Button
Struts 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

Ads