Graphical calculator using AWT

Graphical calculator using AWT

View Answers

May 20, 2008 at 7:12 PM

Hi friend,

import javax.swing.*;

public class Calc {
public static void main(String[] args) {
JFrame window = new CalcGUI();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}//end main
}
-------------------

May 20, 2008 at 7:17 PM

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

class CalcGUI extends JFrame {
private final Font BIGGER_FONT = new Font("monspaced", Font.PLAIN, 20);

private JTextField m_displayField;

private boolean m_startNumber = true;
private String m_previousOp = "=";
private CalcLogic m_logic = new CalcLogic();

public CalcGUI() {
//--- Display field
m_displayField = new JTextField("0", 12);
m_displayField.setHorizontalAlignment(JTextField.RIGHT);
m_displayField.setFont(BIGGER_FONT);

//--- Clear button
JButton clearButton = new JButton("CLEAR");
clearButton.setFont(BIGGER_FONT);
clearButton.addActionListener(new ClearListener());
//--- One listener for all numeric keys.
ActionListener numListener = new NumListener();
String buttonOrder = "789456123 0 ";
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 3, 5, 5));
for (int i = 0; i < buttonOrder.length(); i++) {
String keyTop = buttonOrder.substring(i, i+1);
if (keyTop.equals(" ")) {
buttonPanel.add(new JLabel(""));
} else {
JButton b = new JButton(keyTop);
b.addActionListener(numListener);
b.setFont(BIGGER_FONT);
buttonPanel.add(b);
}
}
ActionListener opListener = new OpListener();

JPanel opPanel = new JPanel();
opPanel.setLayout(new GridLayout(5, 1, 5, 5));
String[] opOrder = {"+", "-", "*", "/", "="};
for (int i = 0; i < opOrder.length; i++) {
JButton b = new JButton(opOrder[i]);
b.addActionListener(opListener);
b.setFont(BIGGER_FONT);
opPanel.add(b);
}
//--- Layout the top-level panel.
JPanel content = new JPanel();
content.setLayout(new BorderLayout(5, 5));
content.add(m_displayField, BorderLayout.NORTH );
content.add(buttonPanel , BorderLayout.CENTER);
content.add(opPanel , BorderLayout.EAST );
content.add(clearButton , BorderLayout.SOUTH );
//--- Finish building the window (JFrame)
this.setContentPane(content);
this.pack();
this.setTitle("Simple Calculator");
this.setResizable(false);
}//end constructor
private void action_clear() {
m_startNumber = true;
m_displayField.setText("0");
m_previousOp = "=";
m_logic.setTotal("0");
}
class OpListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

if (m_startNumber) {
action_clear();
m_displayField.setText("ERROR - No operator");
} else {
m_startNumber = true;
try {
String displayText = m_displayField.getText();

if (m_previousOp.equals("=")) {
m_logic.setTotal(displayText);
} else if (m_previousOp.equals("+")) {
m_logic.add(displayText);
} else if (m_previousOp.equals("-")) {
m_logic.subtract(displayText);
} else if (m_previousOp.equals("*")) {
m_logic.multiply(displayText);
} else if (m_previousOp.equals("/")) {
m_logic.divide(displayText);
}

May 20, 2008 at 7:17 PM

m_displayField.setText("" + m_logic.getTotalString());

} catch (NumberFormatException ex) {
action_clear();
m_displayField.setText("Error");
}
m_previousOp = e.getActionCommand();
}//endif m_startNumber
}//endmethod
}//end class

class NumListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String digit = e.getActionCommand();
if (m_startNumber) {
m_displayField.setText(digit);
m_startNumber = false;
} else {
m_displayField.setText(m_displayField.getText() + digit);
}
}
}

class ClearListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
action_clear();
}
}
}

May 20, 2008 at 7:19 PM

public class CalcLogic {
//-- Instance variables.
private int total;
public CalcLogic() {
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);
}
}

-------------------------------

read for more information.

http://www.roseindia.net/java/









Related Tutorials/Questions & Answers:
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
Graphical calculator using AWT - Java Beginners
Graphical calculator using AWT  Hi Sir, Thanks for the reply.....and is it the same code we need implement on Graphical Calc using Swing? Here is the prg code...); this.pack(); this.setTitle("Simple Calculator"); this.setResizable(false
Advertisements
Program for Calculator - Swing AWT
Program for Calculator  write a program for calculator?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Hope that it will be helpful
another frame by using awt or swings
another frame by using awt or swings  how to connect one frame to another frame by using awt or swings
Calculator
Calculator  need a simple java program to degin a CALCULATOR without using ADVANCED JAVA....   Calculator in Java Swing
Graphical User Interfaces
(AWT) contains several graphical widgets which can be added and positioned... elements provided by the AWT is done using every platform's native GUI toolkit. One... be preserved. ADS_TO_REPLACE_1 AWT Basics The superclass of all graphical user
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
how to print all colors using awt
how to print all colors using awt  how to print all colors using awt
awt
Java AWT Applet example  how to display data using JDBC in awt/applet
awt
JDBC and AWT to display data  how to display data using JDBC in awt/applet
awt
JDBC in awt applet  how to display data using JDBC in awt/applet
awt
JDBC in awt  how to display data using JDBC in awt/applet
how to implements jdbc connections using awt
how to implements jdbc connections using awt  sir, My name... me sample example awt with jdbc. Thanking you sir.  ...(); } } For more information, please go through the following link: AWT Examples
how to implements jdbc connections using awt?
how to implements jdbc connections using awt?   My name is Aditya... example awt with jdbc.   We are proving you a simple application of Login and Registration using java swing. 1)LoginDemo.java: import javax.swing.
AWT
AWT  How to set size of button in awt
how to create a text box using awt
how to create a text box using awt  give an example how creat multi buttons & text boxes
compareing images using java - Swing AWT
compareing images using java  hi can u please give me the code in java to compare imges..i have the program to get the pixel values using pixel... with another image,i.e,program to compare two images using the pixel values.. thank
How to put the logo in login form using swings/awt?
How to put the logo in login form using swings/awt?  Hi, How to put the logo in login form using swings/awt? I write the login form is working but i want to put the logo in login form plz help   Here is an example
AWT basics
. Now a day?s developers are using Swing components instead of AWT to develop...AWT basics Are you looking for the AWT Basics to help you learn AWT quickly? Here we have provided you the links to our AWT tutorials. AWT stands
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 --%> <
awt in java
awt in java  using awt in java gui programming how to false the maximization property of a frame
plz help me to write a snake game using swings - Swing AWT
plz help me to write a snake game using swings  write snake game program using swings
How to set the border in header and footer from login page using swings/awt
How to set the border in header and footer from login page using swings/awt  Hi, How to set the border in header and footer from login page using swings/awt
hybrid digital image embedding process using invisible watermarking - Swing AWT
hybrid digital image embedding process using invisible watermarking   sir/madam, I am subramanian from chennai.i did not create this project using rsa algorithm and i did not understand.please help me with source
generate columns in file using tab delimeter - Swing AWT
generate columns in file using tab delimeter   Hi, I have problem ........i have imported all the files in a directory using JFIleChooser... one please help me how to generate the columns and rows using tab delimiter.(I
code to display images using 2d array - Swing AWT
code to display images using 2d array  HI !! I have an image file in ascii/binary format (lines & pixels) in the form of a 2d-array. I would like to display this using java code. I am relatively new to coding
how to backup and restore database using java - Swing AWT
how to backup and restore database using java  sir my project is to backup and restore my datas using java so i want to know how to backup and restore databases using java give ur ideas and suggestions to me immediately  
virtual onscreen keyboard project using java and swings - Swing AWT
virtual onscreen keyboard project using java and swings  sir thanks a lot for ur reply for my last question, can u please send me the entire project code of virtual on screen keyboard.i did but im not sure as it was right
how to set image in button using swing? - Swing AWT
how to set image in button using swing?  how to set the image in button using swing?  Hi friend, import java.awt.*; import java.awt.event.*; import com.sun.java.swing.*; public class ImageButton extends JFrame
create, edit ,open simple documents using java - Swing AWT
create, edit ,open simple documents using java   In my program I have a text area.How to create document which will contain text in text area with font style like bold,italic etc. and with font size of text
virtual onscreen keyboard project using java and swings - Swing AWT
virtual onscreen keyboard project using java and swings  Im doing virtual on screen keyboard as my project. i didnt get the method to performthe backspace and space operation using swings in java.can please help me  Hi
java awt package tutorial
. The implementation of the user interface elements provided by the AWT is done using...Java AWT Package In Java, Abstract Window Toolkit(AWT) is a platform... programming language, the AWT is also platform-independent. A common set
show the database values graphical represantation
show the database values graphical represantation   show the database values graphical represantation and auto refresh for every 30 secand displaying in webpage
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
simple calculator program in javascript
simple calculator program in javascript  strong textsimple calculator in javascript
Calculator class
Calculator class  I am a beginner in Eclipse. I have to do a program called calculator that adds numbers. This is my code so far: //Margaret //ICS... class Calculator extends JFrame implements ActionListener { JTextField text
Java Graphical user interface
Java Graphical user interface  Hi, guys. I'm christina, need help with my home work Task 1: GUI Design and Implementation The user requirements of your Java quiz GUI application are specified by the following program flow
Calculator
Scientific Calculator - Java Beginners
Scientific Calculator  Develop a scientific calculator using even-driven programming paradigm of Java.? Thanks in ADVANCE  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/java
AWT & SWING
AWT & SWING  What is diffennce between AWT & SWING
AWT programmes - Swing AWT
AWT programmes  I need few programs for menus in AWT. how to open a filed dialog window, and how to save a image in the save window when we press... has all AWT programms which must open,save and make changes to a particular
ModuleNotFoundError: No module named 'Calculator'
ModuleNotFoundError: No module named 'Calculator'  Hi, My Python... 'Calculator' How to remove the ModuleNotFoundError: No module named 'Calculator' error? Thanks   Hi, In your python environment you
graphical user interface - Java Beginners
graphical user interface  how do i write a code for a jmenu bar, File with items like open, save , save as. that lead to another interface?  Hi Friend, Please visit the following link: http://www.roseindia.net
awt - Swing AWT
awt  dear sir my proble is , i have applet code which is stock market chart this code made using "AWT" . in this chart one textbox when user.../java/awt/ Thanks
thread program for calculator implementation
thread program for calculator implementation  Hi i'm prem i need calculator progrm in java that are implemented by Thread interface.....pls strong text
base calculator.. - Java Beginners
base calculator..  Help, i need some help about a base calculator.. i don't know how to start
calculator - Java Interview Questions
calculator  create calculator by java code  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Thanks
create , edit MS WORD like document file using Java Swing - Swing AWT
create , edit MS WORD like document file using Java Swing   In my program I have JTextArea. Text in JTextArea can be set to selected font...? I am using Java SWING. Plz. email your answer

Ads