Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-28-2007, 12:06 PM
Member
 
Join Date: May 2007
Posts: 2
nemo is on a distinguished road
Swing Calculator
I want to code a Swing based calculator. Do you have any simple examples?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-28-2007, 12:07 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Here is a simple example. Good luck!

Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.DecimalFormat; class BasicCalculator extends JFrame implements ActionListener, MouseListener { JTextField display = new JTextField(); JButton[] number = new JButton[11]; JButton addBtn = new JButton("+"); double currentTotal = 0.0; DecimalFormat df = new DecimalFormat("0.00"); boolean totalDisplayed = true; public BasicCalculator() { setTitle("Calculator"); setLocation(300,100); setDefaultCloseOperation(EXIT_ON_CLOSE); display.setBackground(Color.WHITE); display.setFont(new Font("monospaced",Font.BOLD,20)); display.setEditable(false); display.setHorizontalAlignment(JTextField.RIGHT); getContentPane().add(display,BorderLayout.NORTH); JPanel numpad = new JPanel(new GridLayout(4,3)); for(int x = 7; x > 0; x -= 3) { for(int y = x; y < x+3; y++) { number[y] = new JButton(""+y); number[y].addActionListener(this); numpad.add(number[y]); } } number[0] = new JButton("0"); number[0].addActionListener(this); numpad.add(number[0]); numpad.add(new JLabel(" ")); number[10] = new JButton("."); number[10].addActionListener(this); numpad.add(number[10]); getContentPane().add(numpad,BorderLayout.CENTER); addBtn.addMouseListener(this); getContentPane().add(addBtn,BorderLayout.EAST); display.setText(df.format(currentTotal)); pack(); } public void actionPerformed(ActionEvent ae) { if(totalDisplayed) display.setText(""); totalDisplayed = false; display.setText(display.getText()+ae.getActionCommand()); } public void mouseClicked(MouseEvent me) { currentTotal += Double.parseDouble(display.getText()); display.setText(df.format(currentTotal)); totalDisplayed = true; } public void mousePressed(MouseEvent me){} public void mouseReleased(MouseEvent me){} public void mouseEntered(MouseEvent me){} public void mouseExited(MouseEvent me){} public static void main(String[] args){new BasicCalculator().setVisible(true);} }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Calculator aapanju New To Java 3 04-17-2008 06:33 AM
Java calculator decimal cart1443 New To Java 2 04-16-2008 02:19 PM
calculator not working Renegade85 New To Java 5 03-10-2008 04:27 PM
map javax.swing.text.Element to javax.swing.text.View elizabeth New To Java 1 07-30-2007 08:02 PM
Create a Calculator in Java Albert New To Java 2 07-04-2007 09:01 AM


All times are GMT +3. The time now is 12:15 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org