📄 mycalc.java
字号:
/**
* The MyCalc class implements a simple Calculator application.
* MyCalc uses an operator code to keep track of the operation being
* performed
* - 0 = multiplication
* - 1 = division
* - 2 = subtraction
* - 3 = addition
* - 4 = raise a number to the power of the second number
* - 5 = the result of the operator acting on the two numbers
* as a result of the equals button being pressed
*
*
* Copyright (c) 2002-2003 Advanced Applications Total Applications Works.
* (AATAW) All Rights Reserved.
*
* AATAW grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to AATAW.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. AATAW AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL AATAW OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.* ;
import javax.swing.JOptionPane ;
import java.io.*;
public class MyCalc extends JFrame
implements ActionListener {
private JButton bOne, bTwo, bThree, bFour, bFive,
bSix, bSeven, bEight, bNine, bZero, bExit,
bMult, bDiv, bSub, bPlus, bCEnt, bCAll, bEquals,
bPlusMinus, bExp, bMPlus, bMMinus, bHelp,
bFact, bAbout, bOneDivX, bSqrt, bDecPt ;
private JTextField enterBox ;
private JPanel textPanel, exitPanel, buttonPanel, functionPanel ;
private Container c ;
private Font font;
private String firstNum = new String(""), secondNum = new String(""), tempStr ;
private boolean myDiag = false, result = true ,
firstNumSet = false, secondNumSet = false,
pendingOp = false ;
private double aNum, dNum1 = 0.0, dNum2 = 0.0 , answer = 0.0,
tempD = 0.0 , minusOne = -1.0 ;
private double dArray[] = new double[ 10 ] ;
private int opCode = -1, tempInt = 0, tempInt2 = 0,
dArrayPtr = 0, pendingOpCode = -1 ;
/** MyCalc() - constructor used for initialization */
MyCalc() {
super( "Calculator" );
setup() ;
setSize( 350, 300 ); /** sets the size of the frame */
setLocation( 200, 200 ) ; /** sets the location of the frame on the screen*/
show(); /** makes the frame visible */
}
/**
* The setup() method is used to allocate the panel and
* button objects using the new function.
*/
private void setup() {
c = getContentPane() ;
/** setup text field and panel */
enterBox = new JTextField( 15 ) ;
enterBox.setText( "..........." );
enterBox.setEditable( false );
enterBox.setBackground( Color.white );
enterBox.setHorizontalAlignment( JTextField.RIGHT );
enterBox.addKeyListener(
/** this code reprimes the enterbox and avoids exceptions */
new KeyAdapter() {
public void keyPressed ( KeyEvent e ) {
if ( result ) {
MyPrint( "The value of result is " + result +
"this is from the enterbox keylistener." );
result = false ;
enterBox.setText( "" );
}
}
}
);
textPanel = new JPanel() ;
textPanel.add(enterBox ) ;
c.add( textPanel , BorderLayout.NORTH ) ;
/** setup button Panel and the number buttons */
buttonPanel = new JPanel() ;
c.add( buttonPanel , BorderLayout.CENTER ) ;
bZero = new JButton( "0" ) ;
bZero.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bOne = new JButton( "1" ) ;
bTwo = new JButton( "2" ) ;
bThree = new JButton( "3" ) ;
bFour = new JButton( "4" ) ;
bFive = new JButton( "5" ) ;
bSix = new JButton( "6" ) ;
bSeven = new JButton( "7" ) ;
bEight = new JButton( "8" ) ;
bNine = new JButton( "9" ) ;
bExit = new JButton( "Exit" ) ;
bMult = new JButton( "*" ) ;
bMult.setFont( new Font("Sanserif", Font.BOLD, 20 ) );
bDiv = new JButton( "/" ) ;
bDiv.setFont( new Font("Sanserif", Font.BOLD, 20 ) );
bSub = new JButton( "-" ) ;
bSub.setFont( new Font("Sanserif", Font.BOLD, 24 ) );
bPlus = new JButton( "+" ) ;
bPlusMinus = new JButton( "+/-" ) ;
bPlusMinus.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bEquals = new JButton( "=" ) ;
bEquals.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bPlus = new JButton( "+" ) ;
bPlus.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bExp = new JButton( "Pow" ) ;
bExp.setFont( new Font("Sanserif", Font.BOLD, 12 ) );
bOneDivX = new JButton( "1/x" ) ;
bOneDivX.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bSqrt = new JButton( "SQRT" ) ;
bSqrt.setFont( new Font("Sanserif", Font.BOLD, 12 ) );
bDecPt = new JButton( "." ) ;
bDecPt.setFont( new Font("Sanserif", Font.BOLD, 22 ) );
/** assign an event handler to the button and
* indicate that the responses will be handled
* inside this class, MyCalc. */
bZero.addActionListener( this );
bOne.addActionListener( this );
bTwo.addActionListener( this );
bThree.addActionListener( this );
bFour.addActionListener( this );
bFive.addActionListener( this );
bSix.addActionListener( this );
bSeven.addActionListener( this );
bEight.addActionListener( this );
bNine.addActionListener( this );
bExit.addActionListener( this );
bMult.addActionListener( this );
bDiv.addActionListener( this );
bSub.addActionListener( this );
bPlus.addActionListener( this );
bPlusMinus.addActionListener( this );
bEquals.addActionListener( this );
bExp.addActionListener( this );
bOneDivX.addActionListener( this );
bSqrt.addActionListener( this );
bDecPt.addActionListener( this );
/** add the following buttons to the buttonPanel panel. */
buttonPanel.add( bSeven ) ;
buttonPanel.add( bEight ) ;
buttonPanel.add( bNine ) ;
buttonPanel.add( bMult ) ;
buttonPanel.add( bFour ) ;
buttonPanel.add( bFive ) ;
buttonPanel.add( bSix ) ;
buttonPanel.add( bDiv ) ;
buttonPanel.add( bOne ) ;
buttonPanel.add( bTwo ) ;
buttonPanel.add( bThree ) ;
buttonPanel.add( bSub ) ;
buttonPanel.add( bPlusMinus ) ;
buttonPanel.add( bZero ) ;
buttonPanel.add( bDecPt ) ;
buttonPanel.add( bPlus ) ;
buttonPanel.add( bExp );
buttonPanel.add( bOneDivX );
buttonPanel.add( bSqrt );
buttonPanel.add( bEquals ) ;
buttonPanel.setLayout( new GridLayout( 6, 5, 5, 5 ) );
/** setup function panel and its buttons*/
functionPanel = new JPanel() ;
c.add( functionPanel , BorderLayout.EAST ) ;
functionPanel.setLayout( new GridLayout( 6, 1, 5, 3 ) );
bCEnt = new JButton( "CE" ) ;
bCEnt.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
bCAll = new JButton( "CA" ) ;
bCAll.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
bMPlus = new JButton( "M+" ) ;
bMPlus.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
bMMinus = new JButton( "M-" ) ;
bMMinus.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
bFact = new JButton( "! n" ) ;
bFact.setFont( new Font("Sanserif", Font.BOLD, 20 ) );
/** add the following buttons to the functionPanel panel. */
functionPanel.add( bCEnt ) ;
functionPanel.add( bCAll );
functionPanel.add( bMPlus );
functionPanel.add( bMMinus );
functionPanel.add( bFact );
bCEnt.addActionListener( this );
bCEnt.setBackground( Color.blue ) ;
bCEnt.setForeground( Color.white );
bCAll.addActionListener( this );
bCAll.setBackground( Color.blue ) ;
bCAll.setForeground( Color.white );
bMPlus.addActionListener( this );
bMPlus.setBackground( Color.blue ) ;
bMPlus.setForeground( Color.white );
bMMinus.addActionListener( this );
bMMinus.setBackground( Color.blue ) ;
bMMinus.setForeground( Color.white );
bFact.addActionListener( this );
bFact.setBackground( Color.blue ) ;
bFact.setForeground( Color.white );
/** setup exit panel and its buttons */
exitPanel = new JPanel() ;
c.add( exitPanel , BorderLayout.SOUTH ) ;
bExit = new JButton( "Exit" ) ;
bExit.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bExit.setBackground( Color.red ) ;
bExit.setForeground( Color.white );
bHelp = new JButton( "Help" ) ;
bHelp.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bHelp.setBackground( Color.blue ) ;
bHelp.setForeground( Color.white );
bAbout = new JButton( "About" ) ;
bAbout.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
bAbout.setBackground( Color.darkGray ) ;
bAbout.setForeground( Color.white );
/** add the following buttons to the exitPanel panel. */
exitPanel.add( bExit ) ;
exitPanel.add( bHelp ) ;
exitPanel.add( bAbout ) ;
bExit.addActionListener( this );
bHelp.addActionListener( this );
bAbout.addActionListener( this );
}
/** This is the method that is called to respond to a button event */
public void actionPerformed( ActionEvent e ) {
if ( result ) {
/** If result =s true, set the text field to vlank */
MyPrint( "The value of result is " + result );
result = false ;
enterBox.setText( "" );
}
if ( !pendingOp && opCode == 99 ) {
/** If opCode =s 99, then the =s button was pressed */
enterBox.setText( "" );
MyPrint( "The value of opCode is " + opCode );
pendingOpCode = -1 ;
firstNumSet = false ;
firstNum = "" ;
}
if ( e.getSource() == bDecPt ) {
MyPrint( "The decimal point button was pressed." );
enterBox.setText( enterBox.getText() + "." ) ;
}
else if ( e.getSource() == bZero ) {
MyPrint( "The zero button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "0" ) ;
}
else if ( e.getSource() == bOne ) {
MyPrint( "The one button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "1" ) ;
}
else if ( e.getSource() == bTwo ) {
MyPrint( "The two button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "2" ) ;
}
else if ( e.getSource() == bThree ) {
MyPrint( "The Three button was pressed. And pendingOp is " + pendingOp);
enterBox.setText( enterBox.getText() + "3" ) ;
}
else if ( e.getSource() == bFour ) {
MyPrint( "The Four button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "4" ) ;
}
else if ( e.getSource() == bFive ) {
MyPrint( "The Five button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "5" ) ;
}
else if ( e.getSource() == bSix ) {
MyPrint( "The Six button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "6" ) ;
}
else if ( e.getSource() == bSeven ) {
MyPrint( "The Seven button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "7" ) ;
}
else if ( e.getSource() == bEight ) {
MyPrint( "The Eight button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "8" ) ;
}
else if ( e.getSource() == bNine ) {
MyPrint( "The Nine button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "9" ) ;
}
else if ( e.getSource() == bExit ) {
MyPrint( "The Exit button was pressed." );
sysExit() ;
}
else if ( e.getSource() == bMult ) {
MyPrint( "The Mult button was pressed. And pendingOp is " + pendingOp );
multOp() ;
}
else if ( e.getSource() == bDiv ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -