Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Fresher Job


 

Search Host

Monthly Fee($)
Disk Space (MB)
Register With us for Newsletter!
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

Tutorials

Java Server Pages

JAXB

Java Beans

JDBC

MySQL

Java Servlets

Struts

Bioinformatics

Java Code Examples

Interview Questions

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Web Promotion

Web Submission

Submit Sites

Manual Submission?

Web Promotion Guide

Hosting Companies

Web Hosting Guide

Web Hosting

Linux

Beginner Guide to Linux Server

Frameworks

Persistence Framework

Web Frameworks

Free EAI Tools

Web Servers

Aspect Oriented Programming

Free Proxy Servers

Softwares

Adware & Spyware Remover

Open Source Softwares

Graphical calculator using AWT
Expert:manshi
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, multiplication and division.
Answers
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
}
-------------------

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);
}

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();
}
}
}

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/

More Questions
Post Answers
 
Ask Question Facing Programming Problem?
Useful Links
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.