⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculator.java

📁 俄罗斯方块
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
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));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -