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

📄 jsq.java~22~

📁 用java编写计算器 适合初学java的人
💻 JAVA~22~
📖 第 1 页 / 共 2 页
字号:
package jsq;

import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class jsq
    extends JFrame {
  JPanel contentPane;
  XYLayout xYLayout1 = new XYLayout();
  JTextField Text = new JTextField();
  JButton button_new = new JButton();
  JButton button_cancel = new JButton();
  JButton operatorButton1 = new JButton();
  JButton numberButton_1 = new JButton();
  JButton numberButton_2 = new JButton();
  JButton numberButton_3 = new JButton();
  JButton numberButton_4 = new JButton();
  JButton numberButton_5 = new JButton();
  JButton numberButton_6 = new JButton();
  JButton numberButton_7 = new JButton();
  JButton numberButton_8 = new JButton();
  JButton numberButton_9 = new JButton();
  JButton numberButton_10 = new JButton();
  JButton numberButton_0 = new JButton();
  JButton numberButton_11 = new JButton();
  JButton operatorButton2 = new JButton();
  JButton operatorButton3 = new JButton();
  JButton operatorButton4 = new JButton();
  JButton operatorButton5 = new JButton();
  boolean end = true, first = true; //end判断是否结束,first判断是否按了操作键
  int sign = 0; //sing 表示操作符 ‘01234’=‘=+-*/’
  double num, num1, num2;

  public jsq() {
    try {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      jbInit();
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }

  /**
   * Component initialization.
   *
   * @throws java.lang.Exception
   */
  private void jbInit() throws Exception {
    contentPane = (JPanel) getContentPane();
    contentPane.setLayout(xYLayout1);
    setSize(new Dimension(400, 330));
    setTitle("Frame Title");
    contentPane.setToolTipText("");
    Text.setEditable(false);
    Text.setText("0");
    button_new.setFont(new java.awt.Font("Dialog", Font.BOLD, 18));
    button_new.setText("清零");
    button_new.addActionListener(new jsq_button_new_actionAdapter(this));
    button_cancel.setFont(new java.awt.Font("Dialog", Font.BOLD, 18));
    button_cancel.setText("重置");
    button_cancel.addActionListener(new jsq_button_cancel_actionAdapter(this));
    operatorButton1.setFont(new java.awt.Font("Dialog", Font.BOLD, 20));
    operatorButton1.setText("=");
    operatorButton1.addActionListener(new jsq_operatorButton1_actionAdapter(this));
    numberButton_1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_1.setText("1");
    numberButton_1.addActionListener(new jsq_numberButton_1_actionAdapter(this));
    numberButton_2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_2.setText("2");
    numberButton_2.addActionListener(new jsq_numberButton_2_actionAdapter(this));
    numberButton_3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_3.setText("3");
    numberButton_3.addActionListener(new jsq_numberButton_3_actionAdapter(this));
    numberButton_4.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_4.setText("4");
    numberButton_4.addActionListener(new jsq_numberButton_4_actionAdapter(this));
    numberButton_5.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_5.setText("5");
    numberButton_5.addActionListener(new jsq_numberButton_5_actionAdapter(this));
    numberButton_6.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_6.setText("6");
    numberButton_6.addActionListener(new jsq_numberButton_6_actionAdapter(this));
    numberButton_7.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_7.setText("7");
    numberButton_7.addActionListener(new jsq_numberButton_7_actionAdapter(this));
    numberButton_8.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_8.setText("8");
    numberButton_8.addActionListener(new jsq_numberButton_8_actionAdapter(this));
    numberButton_9.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_9.setText("9");
    numberButton_9.addActionListener(new jsq_numberButton_9_actionAdapter(this));
    numberButton_10.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_10.setText("-");
    numberButton_10.addActionListener(new jsq_numberButton_10_actionAdapter(this));
    numberButton_0.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_0.setText("0");
    numberButton_0.addActionListener(new jsq_numberButton_0_actionAdapter(this));
    numberButton_11.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
    numberButton_11.setText(".");
    numberButton_11.addActionListener(new jsq_numberButton_11_actionAdapter(this));
    operatorButton2.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
    operatorButton2.setText("/");
    operatorButton2.addActionListener(new jsq_operatorButton2_actionAdapter(this));
    operatorButton3.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
    operatorButton3.setText("*");
    operatorButton3.addActionListener(new jsq_operatorButton3_actionAdapter(this));
    operatorButton4.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
    operatorButton4.setText("-");
    operatorButton4.addActionListener(new jsq_operatorButton4_actionAdapter(this));
    operatorButton5.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
    operatorButton5.setText("+");
    operatorButton5.addActionListener(new jsq_operatorButton5_actionAdapter(this));
    contentPane.add(Text, new XYConstraints(20, 21, 347, 39));
    contentPane.add(button_cancel, new XYConstraints(145, 75, 95, 31));
    contentPane.add(button_new, new XYConstraints(21, 75, 95, 31));
    contentPane.add(operatorButton1, new XYConstraints(273, 76, 95, 31));
    contentPane.add(operatorButton2, new XYConstraints(302, 126, 66, -1));
    contentPane.add(operatorButton3, new XYConstraints(302, 168, 66, -1));
    contentPane.add(numberButton_3, new XYConstraints(208, 126, 66, -1));
    contentPane.add(numberButton_1, new XYConstraints(21, 126, 66, -1));
    contentPane.add(numberButton_2, new XYConstraints(113, 126, 66, -1));
    contentPane.add(numberButton_5, new XYConstraints(113, 168, 66, -1));
    contentPane.add(numberButton_4, new XYConstraints(21, 168, 66, -1));
    contentPane.add(numberButton_6, new XYConstraints(208, 168, 66, -1));
    contentPane.add(operatorButton4, new XYConstraints(302, 215, 66, -1));
    contentPane.add(numberButton_9, new XYConstraints(208, 215, 66, -1));
    contentPane.add(numberButton_8, new XYConstraints(113, 215, 66, -1));
    contentPane.add(numberButton_7, new XYConstraints(21, 215, 66, -1));
    contentPane.add(numberButton_10, new XYConstraints(21, 263, 66, -1));
    contentPane.add(numberButton_0, new XYConstraints(113, 263, 66, -1));
    contentPane.add(numberButton_11, new XYConstraints(208, 263, 66, -1));
    contentPane.add(operatorButton5, new XYConstraints(302, 263, 66, -1));
  }

  void number(String s) {
    if (end) {
      Text.setText(s);
      end = false;
    }
    else if (Text.getText().equals("0")) {
      Text.setText(s);
    }
    else {
      String str = Text.getText() + s;
      Text.setText(str);
    }
  }

  public void numberButton_1_actionPerformed(ActionEvent e) {
    number("1");
  }

  public void numberButton_2_actionPerformed(ActionEvent e) {
    number("2");
  }

  public void numberButton_3_actionPerformed(ActionEvent e) {
    number("3");
  }

  public void numberButton_4_actionPerformed(ActionEvent e) {
    number("4");
  }

  public void numberButton_5_actionPerformed(ActionEvent e) {
    number("5");
  }

  public void numberButton_6_actionPerformed(ActionEvent e) {
    number("6");
  }

  public void numberButton_7_actionPerformed(ActionEvent e) {
    number("7");
  }

  public void numberButton_8_actionPerformed(ActionEvent e) {
    number("8");
  }

  public void numberButton_9_actionPerformed(ActionEvent e) {
    number("9");
  }

  public void numberButton_0_actionPerformed(ActionEvent e) {
    number("0");
  }

  public void numberButton_10_actionPerformed(ActionEvent e) {
    String str = Text.getText();
    if (str.indexOf("-") < 0) {  //判断负号是否出现 如果没有负号,加上负号 反之,去除负号
      str = "-" + str;
      Text.setText(str);
    }
    else {
      String substr = str.substring(1);
      Text.setText(substr);
    }
  }

  public void numberButton_11_actionPerformed(ActionEvent e) {
       String str=Text.getText();
       if(str.indexOf(".")<0){   //判断小数点是否出现 如果没有,加上小数点 反之,去除小数点
         str+=".";
         Text.setText(str);
       }
  }

  public void button_new_actionPerformed(ActionEvent e) {
          Text.setText("0");//清零
          end=true;
          first=true;
          sign=0;
  }

  public void button_cancel_actionPerformed(ActionEvent e) {
         Text.setText("0");//重置
         end=true;
  }
 void operator(int s){//"+-*/="方法
   if(first){
     num1=Double.parseDouble(Text.getText());
     sign=s;
     first=false;
     end=true;
   }
   else{
     num2=Double.parseDouble(Text.getText());
     switch(sign){
       case 1:
         num=num1+num2;
         break;
       case 2:
        num=num1-num2;
        break;
      case 3:
        num=num1*num2;
        break;
      case 4:
         num=num1/num2;
         break;
     }

⌨️ 快捷键说明

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