- 浏览: 9220756 次
- 性别:
- 来自: 上海
最新评论
-
netkongjian:
不错的报表工具知识分享![deyi]
主流报表工具推荐 -
starry8023:
您的博客很有用!而且我也出现了类似的问题,我是在vs上运行的, ...
在VC下配置openGL -
sliuxl:
...
复制表结构 -
DR玫瑰花香:
[color=darkblue][/color][size=m ...
KMP算法深度解析 -
alvin198761:
看看这两个操作系统的历史,就知道了,这个问题没法追究责任,一个 ...
一则旧闻-Linux是UNIX的盗版?SCO的三大漏洞
/**
* Calculator
* A shareware calculator
*
* @author {@link http://blog.csdn.net/hongweijin Bingbing Li}
*
* @recitation 0101 Matt Carlson
*
* @date 12/7/2005 12:08AM
*
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
public static final int WIDTH = 800; // width of the window
public static final int HEIGHT = 600; // height of the window
public static final int BUTTON_WIDTH = 80; // width of the buttons
public static final int BUTTON_HEIGHT = 60; // height of the buttons
private CardLayout dealer; // card layout
private JPanel deckPanel; // used to card layout
private JTextField result; // the calculate result
private JCheckBoxMenuItem scientificMode; // the menu item of the mode
private Box vPanel1; // the first line buttons of the
// scientific mode.
private JButton mod; // the modular button
private JButton xey; // the button of Yth root of X
private JButton ms; // save button
private JButton mr; // release the stored value
private JButton mc; // clear the stored value
private double head = 0.0; // the first number of the equation
private double tail = 0.0; // the second number of the equation
private boolean substitution = true; // whether substituted the answer or not
private String operatedCommand = "NOTHING"; // the current operated command
private String preOperatedCommand = "NOTHING"; // remember the pre-operated command
private double variableMemory = 0.0; // the variable of the memory
private JButton jButtonR; // the register's button
private JButton jButtonE; // the edit's button
private JTextField nameField; // the field of the name
private JTextField countryField; // the field of the country
private JTextField zipField; // the field of the zip
private JTextField stateField; // the field of the state
private JTextField cityField ; // the field of the city
private JTextField streetAddressField; // the field of the address
private JTextField first; // the first part of the key
private JTextField second; // the second part of the key
private JTextField third; // the third part of the key
private JTextField fourth; // the fourth part of the key
public Calculator()
{
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Normal Calculator");
Container contentPre = getContentPane(); // the container of the window
dealer = new CardLayout(); // create a new card layout
deckPanel = new JPanel();
deckPanel.setLayout(dealer);
Box content = Box.createVerticalBox(); // create a new vertical Box
Box registration = Box.createVerticalBox();
/****************************************************
* menu
* file
*****************************************************/
JMenu fileMenu = new JMenu("File");
JMenuItem exitItemOfFile = new JMenuItem("Exit");
exitItemOfFile.addActionListener(this);
fileMenu.add(exitItemOfFile);
/**
* menu
* settings
*/
JMenu settingsMenu = new JMenu("Settings");
JMenuItem registrationInfo = new JMenuItem("Registration Info");
registrationInfo.addActionListener(this);
settingsMenu.add(registrationInfo);
scientificMode = new JCheckBoxMenuItem("Scientific Mode");
scientificMode.addActionListener(this);
settingsMenu.add(scientificMode);
JMenuBar jMenuBar = new JMenuBar();
jMenuBar.add(fileMenu);
jMenuBar.add(settingsMenu);
setJMenuBar(jMenuBar);
/****************************************************
* textFiled panel
*****************************************************/
result = new JTextField("0"); // the initiated value of the answer
result.setBackground(Color.CYAN); // set the back ground color with cyan
result.setHorizontalAlignment(JTextField.RIGHT); // set the horizontal alignment
content.add(result);
content.add(paintButtons());
deckPanel.add("cal", content); // add the calculators card to the layout
registration.add(paintRegistration()); // add the register window
deckPanel.add("reg", registration); // add the register window to the layout
contentPre.add(deckPanel); // add the cards to the container
}
/**
* paint the buttons of the two models.
*
*/
public Box paintButtons()
{
/****************************************************
* Buttons
*****************************************************/
/**
* line1
*/
vPanel1 = Box.createVerticalBox();
// add the backwards's button
JButton backwards = new JButton("1/X");
backwards.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
backwards.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
backwards.addActionListener(this);
vPanel1.add(backwards);
// add the factorial button
JButton factorial = new JButton("X!");
factorial.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
factorial.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
factorial.addActionListener(this);
vPanel1.add(factorial);
// add the square's button
JButton square = new JButton("<html>X<sup>2</sup></html>");
square.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
square.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
square.addActionListener(this);
square.setActionCommand("sqr");
vPanel1.add(square);
// add the square root's button
JButton squareRoot = new JButton("\u221a");
squareRoot.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
squareRoot.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
squareRoot.addActionListener(this);
vPanel1.add(squareRoot);
// add the power's button
JButton power = new JButton("<html>X<sup>Y</sup></html>");
power.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
power.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
power.addActionListener(this);
power.setActionCommand("pow");
vPanel1.add(power);
/**
* line2
*/
Box vPanel2 = Box.createVerticalBox();
// add the modular button
mod = new JButton("Mod");
mod.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mod.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mod.addActionListener(this);
vPanel2.add(mod);
// add the seven button
JButton seven = new JButton("7");
seven.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
seven.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
seven.addActionListener(this);
vPanel2.add(seven);
// add the four button
JButton four = new JButton("4");
four.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
four.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
four.addActionListener(this);
vPanel2.add(four);
// add the one button
JButton one = new JButton("1");
one.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
one.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
one.addActionListener(this);
vPanel2.add(one);
// add the zero button
JButton zero = new JButton("0");
zero.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
zero.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
zero.addActionListener(this);
vPanel2.add(zero);
/**
* line3
*/
Box vPanel3 = Box.createVerticalBox();
// add the Yth root of X button
xey = new JButton("XeY");
xey.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
xey.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
xey.addActionListener(this);
vPanel3.add(xey);
// add the eight button
JButton eight = new JButton("8");
eight.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
eight.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
eight.addActionListener(this);
vPanel3.add(eight);
// add the five button
JButton five = new JButton("5");
five.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
five.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
five.addActionListener(this);
vPanel3.add(five);
// add the two button
JButton two = new JButton("2");
two.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
two.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
two.addActionListener(this);
vPanel3.add(two);
// add the dot button
JButton dot = new JButton(".");
dot.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
dot.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
dot.addActionListener(this);
vPanel3.add(dot);
/**
* line4
*/
Box vPanel4 = Box.createVerticalBox();
// add the MS button
ms = new JButton("MS");
ms.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
ms.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
ms.addActionListener(this);
vPanel4.add(ms);
// add the nine button
JButton nine = new JButton("9");
nine.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
nine.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
nine.addActionListener(this);
vPanel4.add(nine);
// add the six button
JButton six = new JButton("6");
six.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
six.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
six.addActionListener(this);
vPanel4.add(six);
// add the three button
JButton three = new JButton("3");
three.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
three.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
three.addActionListener(this);
vPanel4.add(three);
// add the plusMinus button
JButton plusMinus = new JButton("\u00b1");
plusMinus.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plusMinus.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plusMinus.addActionListener(this);
vPanel4.add(plusMinus);
/**
* line5
*/
Box vPanel5 = Box.createVerticalBox();
// add the MR button
mr = new JButton("MR");
mr.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mr.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mr.addActionListener(this);
vPanel5.add(mr);
// add the division button
JButton division = new JButton("\u00F7");
division.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
division.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
division.addActionListener(this);
vPanel5.add(division);
// add the multiplication button
JButton multiplication = new JButton("\u00d7");
multiplication.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
multiplication.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
multiplication.addActionListener(this);
vPanel5.add(multiplication);
// add the subtract button
JButton subtract = new JButton("-");
subtract.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
subtract.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
subtract.addActionListener(this);
vPanel5.add(subtract);
// add the plus button
JButton plus = new JButton("+");
plus.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plus.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plus.addActionListener(this);
vPanel5.add(plus);
/**
* line6
*/
Box vPanel6 = Box.createVerticalBox();
// add the MC button
mc = new JButton("MC");
mc.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mc.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mc.addActionListener(this);
vPanel6.add(mc);
// add the C button
JButton c = new JButton("C");
c.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
c.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
c.addActionListener(this);
vPanel6.add(c);
// add a vertical strut
Component verticalStrut =
Box.createVerticalStrut(BUTTON_HEIGHT);
vPanel6.add(verticalStrut);
// add the enter button
JButton enter = new JButton("=");
enter.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT * 2));
enter.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT * 2));
enter.addActionListener(this);
vPanel6.add(enter);
/**
* Buttons panel
*/
Box buttonsPanel = Box.createHorizontalBox();
buttonsPanel.add(vPanel1);
buttonsPanel.add(vPanel2);
buttonsPanel.add(vPanel3);
buttonsPanel.add(vPanel4);
buttonsPanel.add(vPanel5);
buttonsPanel.add(vPanel6);
/**********************************************************
* the initial state is normal calculator
***********************************************************/
vPanel1.setVisible(false);
mod.setVisible(false);
xey.setVisible(false);
ms.setVisible(false);
mr.setVisible(false);
mc.setVisible(false);
return buttonsPanel;
}
/**
* paint the registration window.
*
*/
public Box paintRegistration()
{
Box registration = Box.createVerticalBox();
/*
* title
*/
JLabel titleRegistration = new JLabel("Bingbing's Calculator Registration");
registration.add(titleRegistration);
/*
* information
*/
JPanel information = new JPanel();
information.setLayout(new GridLayout(6, 2));
//Name
JLabel name = new JLabel("Name:");
nameField = new JTextField();
information.add(name);
information.add(nameField);
//Street Address
JLabel streetAddress = new JLabel("Street Address:");
streetAddressField = new JTextField();
information.add(streetAddress);
information.add(streetAddressField);
//City
JLabel city = new JLabel("City:");
cityField = new JTextField();
information.add(city);
information.add(cityField);
//State
JLabel state = new JLabel("State:");
stateField = new JTextField();
information.add(state);
information.add(stateField);
//Zip
JLabel zip = new JLabel("Zip:");
zipField = new JTextField();
information.add(zip);
information.add(zipField);
//Country
JLabel country = new JLabel("Country:");
countryField = new JTextField();
information.add(country);
information.add(countryField);
registration.add(information);
/*
* Registration Code
*/
Box registrationCode = Box.createVerticalBox();
JPanel registrationCodePanel = new JPanel();
registrationCodePanel.setLayout(new FlowLayout());
//registrationCodePanel.setTitle("Registration Code");
registrationCodePanel.setBorder(new LineBorder(Color.red, 1));
registrationCode.add(registrationCodePanel);
first = new JTextField(3);
registrationCodePanel.add(first);
JLabel rail1 = new JLabel(" - ");
registrationCodePanel.add(rail1);
second = new JTextField(3);
registrationCodePanel.add(second);
JLabel rail2 = new JLabel(" - ");
registrationCodePanel.add(rail2);
third = new JTextField(3);
JLabel rail3 = new JLabel(" - ");
registrationCodePanel.add(third);
registrationCodePanel.add(rail3);
fourth = new JTextField(3);
registrationCodePanel.add(fourth);
/*
* buttons
*/
JPanel buttonsReg = new JPanel();
buttonsReg.setLayout(new FlowLayout());
jButtonR = new JButton("Register");
jButtonE = new JButton("Edit");
JButton jButtonRe = new JButton("Return");
jButtonR.addActionListener(this);
jButtonE.addActionListener(this);
jButtonRe.addActionListener(this);
buttonsReg.add(jButtonR);
buttonsReg.add(jButtonE);
buttonsReg.add(jButtonRe);
registrationCode.add(buttonsReg);
Box registrationAndPic = Box.createHorizontalBox();
registrationAndPic.add(registrationCode);
jButtonE.setVisible(false);
/*
* image
*/
JLabel imageLabel = new JLabel();
ImageIcon greatwall = new ImageIcon("greatwall.jpg");
imageLabel.setIcon(greatwall);
registrationAndPic.add(imageLabel);
registration.add(registrationAndPic);
return registration;
}
/**
* performe the action which sent by the window.
*
* @param e catch the action event of the window.
*/
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
// exit the system
if(actionCommand.equals("Exit"))
System.exit(0);
// return to the calculator
else if(actionCommand.equals("Return"))
{
dealer.show(deckPanel, "cal");
setTitle("Calculator");
validate();
pack();
}
// check the register information
else if(actionCommand.equals("Register"))
{
long check = 0;
Boolean valid = true;
if(nameField.getText().trim().length() != 0)
{
check = calChecksum(nameField.getText().trim());
if(check % 3 != Long.parseLong(first.getText().trim()))
valid = false;
else if(check% 5 != Long.parseLong(second.getText().trim()))
valid = false;
else if(check % 7 != Long.parseLong(third.getText().trim()))
valid = false;
else if(check % 11 != Long.parseLong(fourth.getText().trim()))
valid = false;
// if the information is valid, we will change the buttons
// and set the text field uneditable.
if(valid)
{
jButtonE.setVisible(true);
jButtonR.setVisible(false);
nameField.setEditable(false);
countryField.setEditable(false);
zipField.setEditable(false);
stateField.setEditable(false);
cityField.setEditable(false);
streetAddressField.setEditable(false);
first.setEditable(false);
second.setEditable(false);
third.setEditable(false);
fourth.setEditable(false);
validate();
}
}
}
// make the text field editable
else if(actionCommand.equals("Edit"))
{
jButtonE.setVisible(false);
jButtonR.setVisible(true);
nameField.setEditable(true);
countryField.setEditable(true);
zipField.setEditable(true);
stateField.setEditable(true);
cityField.setEditable(true);
streetAddressField.setEditable(true);
first.setEditable(true);
second.setEditable(true);
third.setEditable(true);
fourth.setEditable(true);
validate();
}
// turn to registration window
else if(actionCommand.equals("Registration Info"))
{
dealer.show(deckPanel, "reg");
setTitle("Registration");
validate();
pack();
}
// turn to scientific model
else if(actionCommand.equals("Scientific Mode"))
{
dealer.show(deckPanel, "cal");
vPanel1.setVisible(scientificMode.isSelected());
mod.setVisible(scientificMode.isSelected());
xey.setVisible(scientificMode.isSelected());
ms.setVisible(scientificMode.isSelected());
mr.setVisible(scientificMode.isSelected());
mc.setVisible(scientificMode.isSelected());
if(scientificMode.isSelected())
setTitle("Scientific Model");
else
setTitle("Normal Model");
validate();
pack();
}
// post the result
else if(actionCommand.equals("="))
{
// if the pre-operated command was "=", return the pre-step
if(!preOperatedCommand.equals("="))
tail = Double.parseDouble(result.getText().trim());
// if the two numbel are 0, we don't need calculate the result
if(tail!= 0.0 || head !=0.0)
{
// division
if(operatedCommand.equals("\u00F7"))
{
if(Math.abs(tail) > 0.0000000000000000001)
{
head = head/tail;
result.setText(Double.toString(head));
}
else
{
JOptionPane.showMessageDialog(this, "Cannot divide by zero.");
head = 0.0;
tail = 0.0;
}
substitution = true;
}
// multiplacation
else if(operatedCommand.equals("\u00d7"))
{
head = head*tail;
result.setText(Double.toString(head));
substitution = true;
}
// sub
else if(operatedCommand.equals("-"))
{
head = head-tail;
result.setText(Double.toString(head));
substitution = true;
}
// plus
else if(operatedCommand.equals("+"))
{
head = head+tail;
result.setText(Double.toString(head));
substitution = true;
}
// pow
else if(operatedCommand.equals("pow"))
{
head = Math.pow(head, tail);
result.setText(Double.toString(head));
substitution = true;
}
// the Yth root of the X
else if(operatedCommand.equals("XeY"))
{
head = Math.pow(head, 1/tail);
result.setText(Double.toString(head));
substitution = true;
}
// modular
else if(operatedCommand.equals("Mod"))
{
head = head % tail;
result.setText(Double.toString(head));
substitution = true;
}
}
preOperatedCommand = "=";
}
/*************************************************************
* set the action of the number
**************************************************************/
else if(actionCommand.equals("0"))
{
if(!(result.getText().equals("0") || result.getText().equals("-0")))
{
if(substitution)
{
result.setText("0");
}
else
result.setText(result.getText() + "0");
}
}
else if(actionCommand.equals("1"))
{
if(substitution)
{
result.setText("1");
substitution = false;
}
else
result.setText(result.getText() + "1");
}
else if(actionCommand.equals("2"))
{
if(substitution)
{
result.setText("2");
substitution = false;
}
else
result.setText(result.getText() + "2");
}
else if(actionCommand.equals("3"))
{
if(substitution)
{
result.setText("3");
substitution = false;
}
else
result.setText(result.getText() + "3");
}
else if(actionCommand.equals("4"))
{
if(substitution)
{
result.setText("4");
substitution = false;
}
else
result.setText(result.getText() + "4");
}
else if(actionCommand.equals("5"))
{
if(substitution)
{
result.setText("5");
substitution = false;
}
else
result.setText(result.getText() + "5");
}
else if(actionCommand.equals("6"))
{
if(substitution)
{
result.setText("6");
substitution = false;
}
else
result.setText(result.getText() + "6");
}
else if(actionCommand.equals("7"))
{
if(substitution)
{
result.setText("7");
substitution = false;
}
else
result.setText(result.getText() + "7");
}
else if(actionCommand.equals("8"))
{
if(substitution)
{
result.setText("8");
substitution = false;
}
else
result.setText(result.getText() + "8");
}
else if(actionCommand.equals("9"))
{
if(substitution)
{
result.setText("9");
substitution = false;
}
else
result.setText(result.getText() + "9");
}
else if(actionCommand.equals("."))
{
if(result.getText().length() == 0)
result.setText("0.");
if(!(result.getText().contains(".")))
result.setText(result.getText() + ".");
}
else if(actionCommand.equals("\u00b1"))
{
if(result.getText().charAt(0) != '-')
result.setText("-" + result.getText());
else
result.setText(result.getText().substring(1));
}
else if(actionCommand.equals("C"))
{
result.setText("0");
substitution = true;
}
/*************************************************************
* set the action of the arithmetic
**************************************************************/
// division
else if(actionCommand.equals("\u00F7"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "\u00F7";
substitution = true;
preOperatedCommand = "\u00F7";
}
// multiplication
else if(actionCommand.equals("\u00d7"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "\u00d7";
substitution = true;
preOperatedCommand = "\u00d7";
}
// sub
else if(actionCommand.equals("-"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "-";
substitution = true;
preOperatedCommand = "-";
}
// plus
else if(actionCommand.equals("+"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "+";
substitution = true;
preOperatedCommand = "+";
}
// backwards
else if(actionCommand.equals("1/X"))
{
head = Double.parseDouble(result.getText().trim());
if(head != 0)
result.setText(Double.toString(1/head));
else
{
JOptionPane.showMessageDialog(this, "Cannot divide by zero.");
head = 0.0;
tail = 0.0;
}
}
// factorial
else if(actionCommand.equals("X!"))
{
head = Double.parseDouble(result.getText().trim());
result.setText(Double.toString(factorial(head)));
}
// square
else if(actionCommand.equals("sqr"))
{
head = Double.parseDouble(result.getText().trim());
result.setText(Double.toString(head*head));
}
// square root
else if(actionCommand.equals("\u221a"))
{
head = Double.parseDouble(result.getText().trim());
if(head >= 0.0)
result.setText(Double.toString(Math.sqrt(head)));
else
{
JOptionPane.showMessageDialog(this, "Invalid input for function");
head = 0.0;
tail = 0.0;
}
}
// power
else if(actionCommand.equals("pow"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "pow";
substitution = true;
preOperatedCommand = "pow";
}
// the Yth of the X
else if(actionCommand.equals("XeY"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "XeY";
substitution = true;
preOperatedCommand = "XeY";
}
// modular
else if(actionCommand.equals("Mod"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "Mod";
substitution = true;
preOperatedCommand = "Mod";
}
// MS
else if(actionCommand.equals("MS"))
{
variableMemory = Double.parseDouble(result.getText().trim());
}
// MR
else if(actionCommand.equals("MR"))
{
result.setText(Double.toString(variableMemory));
}
// MC
else if(actionCommand.equals("MC"))
{
variableMemory = 0.0;
}
}
/**
* calculates and returns the factorial of the value
*
* @param value value of the root of the factorial
*
*/
public double factorial(double value)
{
if(value <= 1.0)
return value;
else
return value*factorial(value-1);
}
/**
* calculates and returns the check sum of the message.
*
* @param message message of the name whick entered by the user.
*/
public long calChecksum(String message)
{
long check=0;
for (int i=0;i<message.length();i++)
check += message.charAt(i);
return check;
}
public static void main(String[] args)
{
Calculator calculator = new Calculator();
calculator.pack();
calculator.setVisible(true);
}
}
* Calculator
* A shareware calculator
*
* @author {@link http://blog.csdn.net/hongweijin Bingbing Li}
*
* @recitation 0101 Matt Carlson
*
* @date 12/7/2005 12:08AM
*
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
public static final int WIDTH = 800; // width of the window
public static final int HEIGHT = 600; // height of the window
public static final int BUTTON_WIDTH = 80; // width of the buttons
public static final int BUTTON_HEIGHT = 60; // height of the buttons
private CardLayout dealer; // card layout
private JPanel deckPanel; // used to card layout
private JTextField result; // the calculate result
private JCheckBoxMenuItem scientificMode; // the menu item of the mode
private Box vPanel1; // the first line buttons of the
// scientific mode.
private JButton mod; // the modular button
private JButton xey; // the button of Yth root of X
private JButton ms; // save button
private JButton mr; // release the stored value
private JButton mc; // clear the stored value
private double head = 0.0; // the first number of the equation
private double tail = 0.0; // the second number of the equation
private boolean substitution = true; // whether substituted the answer or not
private String operatedCommand = "NOTHING"; // the current operated command
private String preOperatedCommand = "NOTHING"; // remember the pre-operated command
private double variableMemory = 0.0; // the variable of the memory
private JButton jButtonR; // the register's button
private JButton jButtonE; // the edit's button
private JTextField nameField; // the field of the name
private JTextField countryField; // the field of the country
private JTextField zipField; // the field of the zip
private JTextField stateField; // the field of the state
private JTextField cityField ; // the field of the city
private JTextField streetAddressField; // the field of the address
private JTextField first; // the first part of the key
private JTextField second; // the second part of the key
private JTextField third; // the third part of the key
private JTextField fourth; // the fourth part of the key
public Calculator()
{
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Normal Calculator");
Container contentPre = getContentPane(); // the container of the window
dealer = new CardLayout(); // create a new card layout
deckPanel = new JPanel();
deckPanel.setLayout(dealer);
Box content = Box.createVerticalBox(); // create a new vertical Box
Box registration = Box.createVerticalBox();
/****************************************************
* menu
* file
*****************************************************/
JMenu fileMenu = new JMenu("File");
JMenuItem exitItemOfFile = new JMenuItem("Exit");
exitItemOfFile.addActionListener(this);
fileMenu.add(exitItemOfFile);
/**
* menu
* settings
*/
JMenu settingsMenu = new JMenu("Settings");
JMenuItem registrationInfo = new JMenuItem("Registration Info");
registrationInfo.addActionListener(this);
settingsMenu.add(registrationInfo);
scientificMode = new JCheckBoxMenuItem("Scientific Mode");
scientificMode.addActionListener(this);
settingsMenu.add(scientificMode);
JMenuBar jMenuBar = new JMenuBar();
jMenuBar.add(fileMenu);
jMenuBar.add(settingsMenu);
setJMenuBar(jMenuBar);
/****************************************************
* textFiled panel
*****************************************************/
result = new JTextField("0"); // the initiated value of the answer
result.setBackground(Color.CYAN); // set the back ground color with cyan
result.setHorizontalAlignment(JTextField.RIGHT); // set the horizontal alignment
content.add(result);
content.add(paintButtons());
deckPanel.add("cal", content); // add the calculators card to the layout
registration.add(paintRegistration()); // add the register window
deckPanel.add("reg", registration); // add the register window to the layout
contentPre.add(deckPanel); // add the cards to the container
}
/**
* paint the buttons of the two models.
*
*/
public Box paintButtons()
{
/****************************************************
* Buttons
*****************************************************/
/**
* line1
*/
vPanel1 = Box.createVerticalBox();
// add the backwards's button
JButton backwards = new JButton("1/X");
backwards.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
backwards.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
backwards.addActionListener(this);
vPanel1.add(backwards);
// add the factorial button
JButton factorial = new JButton("X!");
factorial.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
factorial.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
factorial.addActionListener(this);
vPanel1.add(factorial);
// add the square's button
JButton square = new JButton("<html>X<sup>2</sup></html>");
square.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
square.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
square.addActionListener(this);
square.setActionCommand("sqr");
vPanel1.add(square);
// add the square root's button
JButton squareRoot = new JButton("\u221a");
squareRoot.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
squareRoot.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
squareRoot.addActionListener(this);
vPanel1.add(squareRoot);
// add the power's button
JButton power = new JButton("<html>X<sup>Y</sup></html>");
power.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
power.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
power.addActionListener(this);
power.setActionCommand("pow");
vPanel1.add(power);
/**
* line2
*/
Box vPanel2 = Box.createVerticalBox();
// add the modular button
mod = new JButton("Mod");
mod.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mod.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mod.addActionListener(this);
vPanel2.add(mod);
// add the seven button
JButton seven = new JButton("7");
seven.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
seven.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
seven.addActionListener(this);
vPanel2.add(seven);
// add the four button
JButton four = new JButton("4");
four.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
four.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
four.addActionListener(this);
vPanel2.add(four);
// add the one button
JButton one = new JButton("1");
one.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
one.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
one.addActionListener(this);
vPanel2.add(one);
// add the zero button
JButton zero = new JButton("0");
zero.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
zero.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
zero.addActionListener(this);
vPanel2.add(zero);
/**
* line3
*/
Box vPanel3 = Box.createVerticalBox();
// add the Yth root of X button
xey = new JButton("XeY");
xey.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
xey.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
xey.addActionListener(this);
vPanel3.add(xey);
// add the eight button
JButton eight = new JButton("8");
eight.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
eight.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
eight.addActionListener(this);
vPanel3.add(eight);
// add the five button
JButton five = new JButton("5");
five.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
five.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
five.addActionListener(this);
vPanel3.add(five);
// add the two button
JButton two = new JButton("2");
two.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
two.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
two.addActionListener(this);
vPanel3.add(two);
// add the dot button
JButton dot = new JButton(".");
dot.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
dot.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
dot.addActionListener(this);
vPanel3.add(dot);
/**
* line4
*/
Box vPanel4 = Box.createVerticalBox();
// add the MS button
ms = new JButton("MS");
ms.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
ms.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
ms.addActionListener(this);
vPanel4.add(ms);
// add the nine button
JButton nine = new JButton("9");
nine.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
nine.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
nine.addActionListener(this);
vPanel4.add(nine);
// add the six button
JButton six = new JButton("6");
six.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
six.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
six.addActionListener(this);
vPanel4.add(six);
// add the three button
JButton three = new JButton("3");
three.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
three.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
three.addActionListener(this);
vPanel4.add(three);
// add the plusMinus button
JButton plusMinus = new JButton("\u00b1");
plusMinus.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plusMinus.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plusMinus.addActionListener(this);
vPanel4.add(plusMinus);
/**
* line5
*/
Box vPanel5 = Box.createVerticalBox();
// add the MR button
mr = new JButton("MR");
mr.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mr.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mr.addActionListener(this);
vPanel5.add(mr);
// add the division button
JButton division = new JButton("\u00F7");
division.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
division.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
division.addActionListener(this);
vPanel5.add(division);
// add the multiplication button
JButton multiplication = new JButton("\u00d7");
multiplication.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
multiplication.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
multiplication.addActionListener(this);
vPanel5.add(multiplication);
// add the subtract button
JButton subtract = new JButton("-");
subtract.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
subtract.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
subtract.addActionListener(this);
vPanel5.add(subtract);
// add the plus button
JButton plus = new JButton("+");
plus.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plus.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plus.addActionListener(this);
vPanel5.add(plus);
/**
* line6
*/
Box vPanel6 = Box.createVerticalBox();
// add the MC button
mc = new JButton("MC");
mc.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mc.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mc.addActionListener(this);
vPanel6.add(mc);
// add the C button
JButton c = new JButton("C");
c.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
c.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
c.addActionListener(this);
vPanel6.add(c);
// add a vertical strut
Component verticalStrut =
Box.createVerticalStrut(BUTTON_HEIGHT);
vPanel6.add(verticalStrut);
// add the enter button
JButton enter = new JButton("=");
enter.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT * 2));
enter.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT * 2));
enter.addActionListener(this);
vPanel6.add(enter);
/**
* Buttons panel
*/
Box buttonsPanel = Box.createHorizontalBox();
buttonsPanel.add(vPanel1);
buttonsPanel.add(vPanel2);
buttonsPanel.add(vPanel3);
buttonsPanel.add(vPanel4);
buttonsPanel.add(vPanel5);
buttonsPanel.add(vPanel6);
/**********************************************************
* the initial state is normal calculator
***********************************************************/
vPanel1.setVisible(false);
mod.setVisible(false);
xey.setVisible(false);
ms.setVisible(false);
mr.setVisible(false);
mc.setVisible(false);
return buttonsPanel;
}
/**
* paint the registration window.
*
*/
public Box paintRegistration()
{
Box registration = Box.createVerticalBox();
/*
* title
*/
JLabel titleRegistration = new JLabel("Bingbing's Calculator Registration");
registration.add(titleRegistration);
/*
* information
*/
JPanel information = new JPanel();
information.setLayout(new GridLayout(6, 2));
//Name
JLabel name = new JLabel("Name:");
nameField = new JTextField();
information.add(name);
information.add(nameField);
//Street Address
JLabel streetAddress = new JLabel("Street Address:");
streetAddressField = new JTextField();
information.add(streetAddress);
information.add(streetAddressField);
//City
JLabel city = new JLabel("City:");
cityField = new JTextField();
information.add(city);
information.add(cityField);
//State
JLabel state = new JLabel("State:");
stateField = new JTextField();
information.add(state);
information.add(stateField);
//Zip
JLabel zip = new JLabel("Zip:");
zipField = new JTextField();
information.add(zip);
information.add(zipField);
//Country
JLabel country = new JLabel("Country:");
countryField = new JTextField();
information.add(country);
information.add(countryField);
registration.add(information);
/*
* Registration Code
*/
Box registrationCode = Box.createVerticalBox();
JPanel registrationCodePanel = new JPanel();
registrationCodePanel.setLayout(new FlowLayout());
//registrationCodePanel.setTitle("Registration Code");
registrationCodePanel.setBorder(new LineBorder(Color.red, 1));
registrationCode.add(registrationCodePanel);
first = new JTextField(3);
registrationCodePanel.add(first);
JLabel rail1 = new JLabel(" - ");
registrationCodePanel.add(rail1);
second = new JTextField(3);
registrationCodePanel.add(second);
JLabel rail2 = new JLabel(" - ");
registrationCodePanel.add(rail2);
third = new JTextField(3);
JLabel rail3 = new JLabel(" - ");
registrationCodePanel.add(third);
registrationCodePanel.add(rail3);
fourth = new JTextField(3);
registrationCodePanel.add(fourth);
/*
* buttons
*/
JPanel buttonsReg = new JPanel();
buttonsReg.setLayout(new FlowLayout());
jButtonR = new JButton("Register");
jButtonE = new JButton("Edit");
JButton jButtonRe = new JButton("Return");
jButtonR.addActionListener(this);
jButtonE.addActionListener(this);
jButtonRe.addActionListener(this);
buttonsReg.add(jButtonR);
buttonsReg.add(jButtonE);
buttonsReg.add(jButtonRe);
registrationCode.add(buttonsReg);
Box registrationAndPic = Box.createHorizontalBox();
registrationAndPic.add(registrationCode);
jButtonE.setVisible(false);
/*
* image
*/
JLabel imageLabel = new JLabel();
ImageIcon greatwall = new ImageIcon("greatwall.jpg");
imageLabel.setIcon(greatwall);
registrationAndPic.add(imageLabel);
registration.add(registrationAndPic);
return registration;
}
/**
* performe the action which sent by the window.
*
* @param e catch the action event of the window.
*/
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
// exit the system
if(actionCommand.equals("Exit"))
System.exit(0);
// return to the calculator
else if(actionCommand.equals("Return"))
{
dealer.show(deckPanel, "cal");
setTitle("Calculator");
validate();
pack();
}
// check the register information
else if(actionCommand.equals("Register"))
{
long check = 0;
Boolean valid = true;
if(nameField.getText().trim().length() != 0)
{
check = calChecksum(nameField.getText().trim());
if(check % 3 != Long.parseLong(first.getText().trim()))
valid = false;
else if(check% 5 != Long.parseLong(second.getText().trim()))
valid = false;
else if(check % 7 != Long.parseLong(third.getText().trim()))
valid = false;
else if(check % 11 != Long.parseLong(fourth.getText().trim()))
valid = false;
// if the information is valid, we will change the buttons
// and set the text field uneditable.
if(valid)
{
jButtonE.setVisible(true);
jButtonR.setVisible(false);
nameField.setEditable(false);
countryField.setEditable(false);
zipField.setEditable(false);
stateField.setEditable(false);
cityField.setEditable(false);
streetAddressField.setEditable(false);
first.setEditable(false);
second.setEditable(false);
third.setEditable(false);
fourth.setEditable(false);
validate();
}
}
}
// make the text field editable
else if(actionCommand.equals("Edit"))
{
jButtonE.setVisible(false);
jButtonR.setVisible(true);
nameField.setEditable(true);
countryField.setEditable(true);
zipField.setEditable(true);
stateField.setEditable(true);
cityField.setEditable(true);
streetAddressField.setEditable(true);
first.setEditable(true);
second.setEditable(true);
third.setEditable(true);
fourth.setEditable(true);
validate();
}
// turn to registration window
else if(actionCommand.equals("Registration Info"))
{
dealer.show(deckPanel, "reg");
setTitle("Registration");
validate();
pack();
}
// turn to scientific model
else if(actionCommand.equals("Scientific Mode"))
{
dealer.show(deckPanel, "cal");
vPanel1.setVisible(scientificMode.isSelected());
mod.setVisible(scientificMode.isSelected());
xey.setVisible(scientificMode.isSelected());
ms.setVisible(scientificMode.isSelected());
mr.setVisible(scientificMode.isSelected());
mc.setVisible(scientificMode.isSelected());
if(scientificMode.isSelected())
setTitle("Scientific Model");
else
setTitle("Normal Model");
validate();
pack();
}
// post the result
else if(actionCommand.equals("="))
{
// if the pre-operated command was "=", return the pre-step
if(!preOperatedCommand.equals("="))
tail = Double.parseDouble(result.getText().trim());
// if the two numbel are 0, we don't need calculate the result
if(tail!= 0.0 || head !=0.0)
{
// division
if(operatedCommand.equals("\u00F7"))
{
if(Math.abs(tail) > 0.0000000000000000001)
{
head = head/tail;
result.setText(Double.toString(head));
}
else
{
JOptionPane.showMessageDialog(this, "Cannot divide by zero.");
head = 0.0;
tail = 0.0;
}
substitution = true;
}
// multiplacation
else if(operatedCommand.equals("\u00d7"))
{
head = head*tail;
result.setText(Double.toString(head));
substitution = true;
}
// sub
else if(operatedCommand.equals("-"))
{
head = head-tail;
result.setText(Double.toString(head));
substitution = true;
}
// plus
else if(operatedCommand.equals("+"))
{
head = head+tail;
result.setText(Double.toString(head));
substitution = true;
}
// pow
else if(operatedCommand.equals("pow"))
{
head = Math.pow(head, tail);
result.setText(Double.toString(head));
substitution = true;
}
// the Yth root of the X
else if(operatedCommand.equals("XeY"))
{
head = Math.pow(head, 1/tail);
result.setText(Double.toString(head));
substitution = true;
}
// modular
else if(operatedCommand.equals("Mod"))
{
head = head % tail;
result.setText(Double.toString(head));
substitution = true;
}
}
preOperatedCommand = "=";
}
/*************************************************************
* set the action of the number
**************************************************************/
else if(actionCommand.equals("0"))
{
if(!(result.getText().equals("0") || result.getText().equals("-0")))
{
if(substitution)
{
result.setText("0");
}
else
result.setText(result.getText() + "0");
}
}
else if(actionCommand.equals("1"))
{
if(substitution)
{
result.setText("1");
substitution = false;
}
else
result.setText(result.getText() + "1");
}
else if(actionCommand.equals("2"))
{
if(substitution)
{
result.setText("2");
substitution = false;
}
else
result.setText(result.getText() + "2");
}
else if(actionCommand.equals("3"))
{
if(substitution)
{
result.setText("3");
substitution = false;
}
else
result.setText(result.getText() + "3");
}
else if(actionCommand.equals("4"))
{
if(substitution)
{
result.setText("4");
substitution = false;
}
else
result.setText(result.getText() + "4");
}
else if(actionCommand.equals("5"))
{
if(substitution)
{
result.setText("5");
substitution = false;
}
else
result.setText(result.getText() + "5");
}
else if(actionCommand.equals("6"))
{
if(substitution)
{
result.setText("6");
substitution = false;
}
else
result.setText(result.getText() + "6");
}
else if(actionCommand.equals("7"))
{
if(substitution)
{
result.setText("7");
substitution = false;
}
else
result.setText(result.getText() + "7");
}
else if(actionCommand.equals("8"))
{
if(substitution)
{
result.setText("8");
substitution = false;
}
else
result.setText(result.getText() + "8");
}
else if(actionCommand.equals("9"))
{
if(substitution)
{
result.setText("9");
substitution = false;
}
else
result.setText(result.getText() + "9");
}
else if(actionCommand.equals("."))
{
if(result.getText().length() == 0)
result.setText("0.");
if(!(result.getText().contains(".")))
result.setText(result.getText() + ".");
}
else if(actionCommand.equals("\u00b1"))
{
if(result.getText().charAt(0) != '-')
result.setText("-" + result.getText());
else
result.setText(result.getText().substring(1));
}
else if(actionCommand.equals("C"))
{
result.setText("0");
substitution = true;
}
/*************************************************************
* set the action of the arithmetic
**************************************************************/
// division
else if(actionCommand.equals("\u00F7"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "\u00F7";
substitution = true;
preOperatedCommand = "\u00F7";
}
// multiplication
else if(actionCommand.equals("\u00d7"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "\u00d7";
substitution = true;
preOperatedCommand = "\u00d7";
}
// sub
else if(actionCommand.equals("-"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "-";
substitution = true;
preOperatedCommand = "-";
}
// plus
else if(actionCommand.equals("+"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "+";
substitution = true;
preOperatedCommand = "+";
}
// backwards
else if(actionCommand.equals("1/X"))
{
head = Double.parseDouble(result.getText().trim());
if(head != 0)
result.setText(Double.toString(1/head));
else
{
JOptionPane.showMessageDialog(this, "Cannot divide by zero.");
head = 0.0;
tail = 0.0;
}
}
// factorial
else if(actionCommand.equals("X!"))
{
head = Double.parseDouble(result.getText().trim());
result.setText(Double.toString(factorial(head)));
}
// square
else if(actionCommand.equals("sqr"))
{
head = Double.parseDouble(result.getText().trim());
result.setText(Double.toString(head*head));
}
// square root
else if(actionCommand.equals("\u221a"))
{
head = Double.parseDouble(result.getText().trim());
if(head >= 0.0)
result.setText(Double.toString(Math.sqrt(head)));
else
{
JOptionPane.showMessageDialog(this, "Invalid input for function");
head = 0.0;
tail = 0.0;
}
}
// power
else if(actionCommand.equals("pow"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "pow";
substitution = true;
preOperatedCommand = "pow";
}
// the Yth of the X
else if(actionCommand.equals("XeY"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "XeY";
substitution = true;
preOperatedCommand = "XeY";
}
// modular
else if(actionCommand.equals("Mod"))
{
head = Double.parseDouble(result.getText().trim());
operatedCommand = "Mod";
substitution = true;
preOperatedCommand = "Mod";
}
// MS
else if(actionCommand.equals("MS"))
{
variableMemory = Double.parseDouble(result.getText().trim());
}
// MR
else if(actionCommand.equals("MR"))
{
result.setText(Double.toString(variableMemory));
}
// MC
else if(actionCommand.equals("MC"))
{
variableMemory = 0.0;
}
}
/**
* calculates and returns the factorial of the value
*
* @param value value of the root of the factorial
*
*/
public double factorial(double value)
{
if(value <= 1.0)
return value;
else
return value*factorial(value-1);
}
/**
* calculates and returns the check sum of the message.
*
* @param message message of the name whick entered by the user.
*/
public long calChecksum(String message)
{
long check=0;
for (int i=0;i<message.length();i++)
check += message.charAt(i);
return check;
}
public static void main(String[] args)
{
Calculator calculator = new Calculator();
calculator.pack();
calculator.setVisible(true);
}
}
相关推荐
Java课程设计实训大作业:记事本+简易计算器+聊天系统+日历+中英查询 基础任务一:设计日历软件 根据如下图,综合运用GUI编程、事件处理、Calendar类应用等知识设计一款月历,要求能通过输入(或选择)年月的方式...
【标题】"简单记事本C#源代码2"揭示了这是一个基于C#编程语言实现的文本编辑器项目。在Windows操作系统环境下,记事本是一个基础的文本编辑工具,而这个"简单记事本"则在原版基础上增加了额外的功能,以提供更丰富的...
7、需要当记事本用时,在本行最前面加入 ; 号,可不出现错误提示! 8、表达式可以是加+、减-、乘*、除/,以及多种函数组合 二、其它说明 1、用户可到QQ群104837062更新,作者可根据用户的意见,增加部分适用功能; 2...
这个压缩包文件包含的源代码示例,向我们展示了如何在Delphi环境下通过编写代码来启动Windows系统自带的这些应用程序。 首先,我们要理解Delphi中的核心概念——VCL(Visual Component Library,可视化组件库),它...
这个列表表明压缩包中包含了实现这两个功能的源代码文件。可能有分别对应计算器和记事本的Java类,例如"Calculator.java"和"NotePad.java"。这些文件可能还包含其他辅助类,用于数据处理、界面组件等。 详细知识点...
标题中的“VB编程记事本计算器选择题”指的是使用Visual Basic(VB)编程语言实现的三个基础项目:记事本程序、简易科学计算器以及选择题系统。这些项目是初学者学习VB时常见的实践任务,有助于理解和掌握VB的基本...
除了记事本的功能,描述中还提到了GUI计算器的源代码。计算器的实现涉及数字和运算符的解析、表达式求值等,可能使用了栈数据结构来实现计算逻辑。这部分虽然不是记事本的核心,但同样展示了Java GUI编程的另一面。 ...
- **项目结构**: 了解标准的Android项目目录结构,如`res`(资源文件)、`src`(源代码)、`AndroidManifest.xml`(应用配置)等。 - **调试工具**: 使用Eclipse的调试器进行代码调试,检查变量值和应用行为。 5....
在Java中,这样的练习可能包括创建一个计算器或记事本应用程序,这些都是常见的编程练习,因为它们可以展示基本的输入/输出操作、条件判断和逻辑流程控制。 提到的"计算器"和"记事本"项目,是典型的控制台应用程序...
在这个名为“4643189_vc6.0可执行源码”的压缩包中,包含了一系列使用VC6.0开发的可执行源代码,包括音乐播放器、记事本、计算器以及QQ和五子棋等应用程序。这些项目不仅是学习C++语言和Windows API编程的绝佳实例,...
`Calculator.java`是计算器程序的源代码,可能会包含`Scanner`类用于获取用户输入,然后根据输入执行相应的计算逻辑,并显示结果。 3. **QQ聊天室**: - QQ聊天室是一个简单的多用户通信应用,可能使用了网络编程...
【描述】:“计算器代码”指的是实现计算器功能的源代码或程序,这通常涉及基本的算术运算,如加、减、乘、除,以及可能包括更复杂的运算,如求平方根、对数或指数。代码与程序是计算机科学中的核心概念,它们是...
在程序源代码中,可以看到Calculator类继承自JFrame,这是Java Swing中创建顶级窗口的主要方式。类中包含了计算器所需的各种变量,如存储字符的数组、临时变量以及用于计算的变量。此外,还创建了多个JPanel来组织...
【记事本的设计与功能实现】的Java课程设计报告中,学生通过开发一个基于Java的图形界面记事本应用,深化了对面向对象编程的理解和...这个记事本项目的源代码对于学习Java GUI编程的初学者来说是一个宝贵的实践案例。
andriod闹钟源代码.rar Android Gps日志记录程序源码.rar Android MP3播放器,带卡拉OK字幕.rar Android 个人记账程序源码.rar Android 仿Mac的Dock.rar Android 口袋微博服务器客户端代码.rar Android 手电筒源码....
2009-09-19 23:57 <DIR> Ok一个比较完整的源代码 定时提醒 1.0 2009-09-19 20:37 <DIR> Ok一个非常Cool的图像编辑软件 2009-09-20 00:27 <DIR> Ok个简单的计算器 2009-09-19 23:51 <DIR> Ok中国象棋的源程序,支持...
Java访问权限控制源代码 1个目标文件 摘要:Java源码,文件操作,权限控制 Java访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流...
在【标题】"经典java程序源代码"中,我们可以看到这是一个关于Java编程的学习资源,包含了加法器、小型记事本等小程序的源代码。这些示例程序可以帮助初学者更好地理解和实践Java编程语言的基本概念和组件。 首先,...
虽然提供的文件名只有"记事本"和"新记事本",这通常指的是文本编辑器程序,可能是用于打开和查看源代码的。在C#项目中,源代码文件通常以.cs为扩展名。不过,由于具体文件名没有提供,我们只能推测这里可能包含的是...
本压缩包中的源代码是针对C#初学者的一系列练习,旨在帮助学习者巩固基础知识并提升编程技能。 1. **计算器.txt** 这个文件很可能是实现一个简单的命令行或图形界面的计算器程序,涵盖了基本的数学运算,如加减...