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

📄 calculator.java

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

⌨️ 快捷键说明

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