📄 calculator.java
字号:
// Calculator.java
//<applet code = Calculator.class,width = 200,height = 50></applet>
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JApplet implements ActionListener , KeyListener
{
boolean flag = false, first = true;
int opTag = 0, numTag = 0;
int[] op = new int[ 2 ];
double[] num = new double[ 2 ];
public String[] s = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/", "Back", "CE", "C"};
JTextField textfield = new JTextField( 10 );
JPanel p4 = new JPanel();
public void init()
{
Container container = getContentPane();
container.setLayout( new BorderLayout( 10, 10) );
// Create panel p1 for the buttons and set GridLayout
textfield.setHorizontalAlignment( JTextField.RIGHT );
textfield.setText( "0.0" );
p4.setLayout( new BorderLayout());
p4.add( textfield, BorderLayout.CENTER);
JPanel p1 = new JPanel();
p1.setLayout( new GridLayout( 4, 3, 5, 5 ) );
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout( 1, 3, 10, 20 ) );
// Add buttons to the panel
for( int i = 0; i < 19; i ++ )
{
JButton button = new JButton();
button.setText( s[i] );
button.addKeyListener( this );
button.addActionListener( this );
if( i < 16 )
p1.add( button );
else
p2.add( button );
}
//Add p2 and a textfield to the panel
JPanel p3 = new JPanel();
p3.setLayout( new BorderLayout( 10, 10) );
p3.add( p4, BorderLayout.NORTH );
p3.add( p2, BorderLayout.CENTER );
//Add p3 and p1 to the containter
container.add( p3, BorderLayout.NORTH );
container.add( p1, BorderLayout.CENTER );
p4.addKeyListener( this );
}
public void focus()
{
p4.requestFocus();
}
// Main method
public static void main( String[] args )
{
Calculator applet = new Calculator();
JFrame frame = new JFrame();
frame.setTitle( "Calculator" );
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.pack();
frame.setVisible( true );
frame.setLocation( 350, 250 );
applet.focus();
}
public void actionPerformed( ActionEvent e )
{
String actionCommand = e.getActionCommand();
if ( e.getSource() instanceof JButton )
input( actionCommand );
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
String actionCommand = String.valueOf( e.getKeyChar() );
/** deal the exception -- the input is not a number */
try
{
int j = 0;
for( int i = 0; i < 16; i ++ )
if( actionCommand.equals( s[ i ] ) || e.getKeyChar() == '\b' || e.getKeyChar() == '\n' )
j ++;
if( j == 0 )
throw new NotNumberException( actionCommand );
}
catch( NotNumberException ex )
{
textfield.setText( "0.0" );
System.out.println( ex );
}
input( actionCommand );
}
/** input the arithmetic and get its result */
public void input( String actionCommand )
{
for( int i = 0; i < 10; i ++ )
if ( Integer.toString( i ).equals( actionCommand ) )
{
if( flag == false )
{
textfield.setText( String.valueOf( i ) );
flag = true;
}
else
textfield.setText( textfield.getText().trim() + i );
num[ numTag ] = Double.parseDouble( textfield.getText().trim() );
if( numTag == 1)
first = false;
}
if( ".".equals( actionCommand ) )
textfield.setText( textfield.getText().trim() + "." );
if( flag == true && ( "Back".equals( actionCommand ) || String.valueOf( '\b' ).equals( actionCommand ) ) )
if( Integer.parseInt( textfield.getText().trim() + 1 ) != 1 )
textfield.setText( textfield.getText().substring( 0, textfield.getText().trim().length() - 1 ) );
if( "CE".equals( actionCommand ) )
{
textfield.setText( "0.0" );
flag = false;
}
if( "C".equals( actionCommand ) )
{
textfield.setText( "0.0" );
for( int i = 0; i < 2; i ++ )
num[ i ] = 0;
op[ 0 ] = 1;
numTag = 0;
flag = false;
first = true;
}
/** deal the exception -- Denominator is zero */
try
{
if( "+".equals( actionCommand ) )
opOperate( 1 );
if( "-".equals( actionCommand ) )
opOperate( 2 );
if( "*".equals( actionCommand ) )
opOperate( 3 );
if( "/".equals( actionCommand ) )
opOperate( 4 );
if( "=".equals( actionCommand ) || String.valueOf( '\n' ).equals( actionCommand ) )
{
if( flag == false && numTag == 1 )
num[ 1 ] = num [ 0 ];
num[ 0 ] = operate( num[ 0 ], num[ 1 ], op[ 0 ] );
textfield.setText( Double.toString( num[ 0 ] ) );
first = true;
numTag = 0;
flag = false;
}
}
catch( Exception ex )
{
textfield.setText( "0.0" );
System.out.println( ex );
}
}
/** the method of calculation and display */
public void opOperate( int n )
{
if( first == true )
opTag = 0;
else
opTag = 1;
op[ opTag ] = n;
if( first == false )
{
num[ 0 ] = operate( num[ 0 ], num[ 1 ], op[ 0 ] );
op[ 0 ] = op[ 1 ];
textfield.setText( String.valueOf( num[ 0 ] ) );
}
flag = false;
numTag = 1;
}
/** the method of calculator */
public double operate( double x, double y, int op ) throws RuntimeException
{
switch( op )
{
case 1 :
return x + y;
case 2 :
return x - y;
case 3 :
return x * y;
case 4 :
if( y == 0 )
throw new RuntimeException( "Denominator cannot be zero" );
else
return x / y;
}
return 0;
}
/** the notnumberexception */
private class NotNumberException extends Exception
{
private String letter;
public NotNumberException( String letter )
{
super( "It can't be a letter!" );
this.letter = letter;
}
public String getLetter()
{
return letter;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -