📄 awtcalcapp.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.lang.*;
import java.util.*;
class AwtCalcApp {
public static void main(String args[]){
Frame fr = new Frame();
fr.setTitle( "Awt Calculator" );
fr.setSize( 220, 175 );
fr.setResizable( false );
fr.add( new AwtCalc(), BorderLayout.CENTER );
fr.setVisible( true );
fr.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit( 1 );
}
});
}
}
class AwtCalc extends Panel{
//Labels for the number panel of the calculator
private String[] numPanelText = { " 1 ", " 2 ", " 3 ",
" 4 ", " 5 ", " 6 ",
" 7 ", " 8 ", " 9 ",
Operator.CLEAR , " 0 ", Operator.DOT };
//Labels for the operator panel of the calculator
private String[] operPanelText = { Operator.ADD, Operator.SUBTRACT,
Operator.MULTIPLY, Operator.DIVIDE,
Operator.POW, Operator.SQRT,
Operator.NEGATE, Operator.EQUALS };
private Panel numButtonPanel; //used to hold the number buttons
private Panel operButtonPanel; //used to hold the operator buttons
private Panel3D displayPanel; //used for the calculator's display
private ButtonHandler handler; //action listener for the buttons
private CalcDisplay display; //displays the output
private Font buttonfont;
public AwtCalc(){
//Initialize
buttonfont = new Font( "Courier", Font.PLAIN, 13 );
setLayout( new BorderLayout() );
setBackground( new Color( 212, 208, 200 ) );
Panel3D mainPanel = new Panel3D( Border3D.EXCLUDE_TOP_BORDER );
numButtonPanel = new Panel( new GridLayout(4,3, 1, 1) );
operButtonPanel = new Panel( new GridLayout(4, 2, 1, 1) );
displayPanel = new Panel3D( Border3D.EXCLUDE_BOTTOM_BORDER );
display = new CalcDisplay( 192,26);
handler = new ButtonHandler( display );
displayPanel.add( display );
mainPanel.add( createNumberPanel() );
mainPanel.add( createOperPanel() );
add( displayPanel, BorderLayout.NORTH );
add( mainPanel, BorderLayout.CENTER );
}
/*
* Method: createNumberPanel
* Description: contructs and returns the calculator's number panel
*/
private Panel createNumberPanel(){
if ( display != null ) {
ButtonComponent btn = null;
for ( int i = 0; i < numPanelText.length; i++ ){
btn = new ButtonComponent( numPanelText[i] );
btn.addActionListener( handler );
btn.setFont( buttonfont );
numButtonPanel.add( btn );
}
}
return numButtonPanel;
}
/**
* Method: createOperPanel
* Description: contructs and returns the calculator's number panel
**/
private Panel createOperPanel(){
ButtonComponent btn = null;
for ( int i = 0; i < operPanelText.length; i++ ){
btn = new ButtonComponent( operPanelText[i] );
btn.setFont( buttonfont );
btn.addActionListener( handler );
operButtonPanel.add( btn );
}
return operButtonPanel;
}
}
class Border3D {
/*****************************************************************
* Border Types:
*
* Static variables that allows control of how the the borders is
* displayed.
*
* EXCLUDE_TOP_BORDER: Use to create components that lack a
* top border.
*
* EXCLUDE_BOTTOM_BORDER: Use to create components that lack a
* bottom border.
*
* FULL_BORDER: Default type. Creates a component with
* a full border.
******************************************************************/
static final int EXCLUDE_TOP_BORDER = 1,
EXCLUDE_BOTTOM_BORDER = 2,
FULL_BORDER = 3;
//used to store border types
private int type;
//used to store the component
private Component comp;
public Border3D( Component comp ) { this( comp, FULL_BORDER ); }
public Border3D( Component comp, int type ){
this.comp = comp;
this.type = type;
}
/*
* Method: draw3DBorder
*
* Description: Draws a 3D border around components perimeter.
*
*/
public void draw3DBorder( Graphics g ){
draw3DBorder( g, comp.size() );
}
public void draw3DBorder( Graphics g, Dimension area ) {
draw3DBorder( g, 0, 0, area.width, area.height );
}
public void draw3DBorder( Graphics g, int x, int y, int width, int height ) {
//Draws top part of border
if ( type == EXCLUDE_BOTTOM_BORDER || type == FULL_BORDER ){
g.setColor( comp.getBackground().darker() );
g.drawLine( x, y, width, y );
g.setColor( Color.white );
g.drawLine( x+1, y+1, width-1, y+1 );
}
//Draws bottom portion of border
if ( type == EXCLUDE_TOP_BORDER || type == FULL_BORDER ){
g.setColor( comp.getBackground().brighter() );
g.drawLine( x, height, width, height);
g.setColor( comp.getBackground().darker() );
g.drawLine( x+1, height-1, width-1, height -1);
}
//draws the sides of the border
g.setColor( comp.getBackground().brighter() );
g.drawLine( x+1, y+1, x+1, height - 1 );
g.drawLine( width, y, width, height );
g.setColor( comp.getBackground().darker() );
g.drawLine( x, y, x, height );
g.drawLine( width-1, y+1, width-1, height - 1);
}
}
class ButtonComponent extends Component {
private String label; //the button's label
private boolean pressed = false; //used to determine button presses
private ActionListener action;
private boolean mouseOver = false; //used to determine mouse overs
private Border3D border; //to draw a 3D border around button
public ButtonComponent(String label){
this.label = label;
border = new Border3D( this );
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
/**
* Returns the preferred size of the button. This method is called automatically
* when the component is painted.
*
*/
public Dimension getPreferredSize() {
Font f = getFont();
if(f != null) {
FontMetrics fm = getFontMetrics(getFont());
return new Dimension(fm.stringWidth(label) + 10, fm.getHeight() + 5);
}
else {
return new Dimension(25, 25);
}
}
/**
* Returns the minimum size of the button.
*/
public Dimension getMinimumSize() { return new Dimension(20, 20); }
/**
* Detects mouse events on the component.
*/
public void processMouseEvent(MouseEvent e){
switch(e.getID()) {
case MouseEvent.MOUSE_PRESSED:
//Create a new actionevent and pass to the action listener -
//( the Button Handler )
if ( action != null ) {
ActionEvent event = new ActionEvent( this, e.getID(), label );
action.actionPerformed( event );
}
pressed = true;
//Invoke the repaint method which draws the button to appear pressed
repaint();
break;
case MouseEvent.MOUSE_RELEASED:
//When repaint is invoked, component color is returned to normal
if(pressed == true) {
pressed = false;
repaint();
}
break;
case MouseEvent.MOUSE_ENTERED:
//repaint method lighten's component's color to give a hover effect
mouseOver = true;
repaint();
break;
case MouseEvent.MOUSE_EXITED:
//cancel hover effect
mouseOver = false;
if(pressed == true) pressed = false;
repaint();
break;
}
super.processMouseEvent(e);
}
//Add's action listener
public void addActionListener( ActionListener a ){
action = a; //this is the ButtonHandler
}
//returns the buttons label
public String getLabel() { return label; }
/*
* repaints the background
*/
public void paint(Graphics g) {
int width = getSize().width - 1;
int height = getSize().height - 1;
//set background darker to give a pressed effect
if(pressed) {
g.setColor(getBackground().darker().darker());
}else if(mouseOver){//set background lighter to give hover effect
g.setColor( getBackground().brighter() );
//set background to normal
}else{
g.setColor(getBackground());
}
//fill background
g.fillRect(0, 0, width, height );
//draws 3D border
border.draw3DBorder(g, 0, 0, width, height );
//center and draw the button's label
Font f = getFont();
if(f != null) {
FontMetrics fm = getFontMetrics(getFont());
g.setColor(getForeground());
g.drawString(label,width/2 - fm.stringWidth(label)/2,
height/2 + fm.getMaxDescent());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -