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

📄 计算器java代码.txt

📁 基于Eclipse 开发的一个功能非常全的计算器!注意需要安装一些插件
💻 TXT
📖 第 1 页 / 共 3 页
字号:
								if (operater.firstNode.data.toString().intern() == "*") {
									c = b * a;
								}
								if (operater.firstNode.data.toString().intern() == "/") {
									c = b / a;
								}
								operater.pop();
								dt.push(c.toString());
							}
							jTextField.setText(c.toString());
						}

					}

				}
			});
		}
		return jButton14;
	}

	/**
	 * This method initializes jButton15
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton15() {
		if (jButton15 == null) {
			jButton15 = new JButton();
			jButton15.setBounds(new Rectangle(14, 255, 45, 41));
			jButton15.setCursor(new Cursor(Cursor.HAND_CURSOR));
			jButton15.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
			jButton15.setFont(new Font("Dialog", Font.BOLD, 24));
			jButton15.setText(".");
			jButton15.addMouseListener(new java.awt.event.MouseListener() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if (q && jTextField.getText().intern() != "+"
							&& jTextField.getText().intern() != "-"
							&& jTextField.getText().intern() != "*"
							&& jTextField.getText().intern() != "/"
							&& jTextField.getText().intern() != "("
							&& jTextField.getText().intern() != ")" && !p) {
						jTextField.setText(jTextField.getText() + ".");
						q = false;
					} else if (q) {
						jTextField.setText(".");
						q = false;
					}
				}

				public void mousePressed(java.awt.event.MouseEvent e) {
				}

				public void mouseReleased(java.awt.event.MouseEvent e) {
				}

				public void mouseEntered(java.awt.event.MouseEvent e) {
				}

				public void mouseExited(java.awt.event.MouseEvent e) {
				}
			});
		}
		return jButton15;
	}

	/**
	 * This method initializes jButton16
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton16() {
		if (jButton16 == null) {
			jButton16 = new JButton();
			jButton16.setBounds(new Rectangle(78, 254, 51, 43));
			jButton16.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
			jButton16.setFont(new Font("Dialog", Font.BOLD, 24));
			jButton16.setText("(");
			jButton16.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					on5++; // 计数器加1
					q = true; // 小数点可用
					on6 = false; // "="不可用
					if (jTextField.getText().intern() == "(") {
						operater.push(jTextField.getText()); // 把前一个"("压入符号栈
					} else if (jTextField.getText().intern() == "" ||p) {
						operater.push("("); // 把"("压入符号栈,此时"("为第一个输入的元素
						jTextField.setText("(");
					} else if (jTextField.getText().intern() != ")"
							&& (jTextField.getText().intern() == "*"
									|| jTextField.getText().intern() == "+"
									|| jTextField.getText().intern() == "-" || jTextField
									.getText().intern() == "/")) {
						operater.push("(");
						jTextField.setText("(");
					}
				}
			});
		}
		return jButton16;
	}

	/**
	 * This method initializes jButton17
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton17() {
		if (jButton17 == null) {
			jButton17 = new JButton();
			jButton17.setBounds(new Rectangle(151, 253, 48, 44));
			jButton17.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
			jButton17.setFont(new Font("Dialog", Font.BOLD, 24));
			jButton17.setText(")");
			jButton17.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if (on5 > 0) {
						on6 = true; // "="可用
						on5--;
						if (jTextField.getText().intern() != ")") {
							dt.push(jTextField.getText());
						}
						if (jTextField.getText().intern() != "(") {
							jTextField.setText(")");
							q = true; // 小数点可用
							while (operater.firstNode.data.toString().intern() != "(") {
								a = Double.parseDouble(dt.pop().toString());
								b = Double.parseDouble(dt.pop().toString());
								if (operater.firstNode.data.toString().intern() == "+") {
									c = b + a;
								}
								if (operater.firstNode.data.toString().intern() == "-") {
									c = b - a;
								}
								if (operater.firstNode.data.toString().intern() == "*") {
									c = b * a;
								}
								if (operater.firstNode.data.toString().intern() == "/") {
									c = b / a;
								}
								operater.pop();
								dt.push(c.toString());
							}
							operater.pop();
						}
					}
				}
			});
		}
		return jButton17;
	}

	/**
	 * This method initializes jButton18
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton18() {
		if (jButton18 == null) {
			jButton18 = new JButton();
			jButton18.setBounds(new Rectangle(214, 15, 53, 45));
			jButton18.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
			jButton18.setFont(new Font("Dialog", Font.BOLD, 14));
			jButton18.setText("Delete");
			jButton18.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					bb=jTextField.getText();
					
					if(jTextField.getText().length()!=0){
						jTextField.setText(jTextField.getText().substring(0, jTextField.getText().length()-1));
					}
				}
			});
		}
		return jButton18;
	}

	/**
	 * This method initializes jButton19
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton19() {
		if (jButton19 == null) {
			jButton19 = new JButton();
			jButton19.setBounds(new Rectangle(215, 79, 49, 44));
			jButton19.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
			jButton19.setFont(new Font("Dialog", Font.BOLD, 18));
			jButton19.setText("CE");
			jButton19.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					p = false; // 指明不是计算结果
					on1 = false; // "*"不可用
					on2 = false; // "/"不可用
					on3 = false; // "+"不可用
					on4 = true; // "-"不可用
					on6 = false; // "="不可用
					on5=0;   //")"不可用
					jTextField.setText("");
					while (operater.firstNode.data.toString().intern() != "#") {
						operater.pop();
					}
					while (dt.firstNode.data.toString().intern() != "#") {
						dt.pop();
					}
				}
			});
		}
		return jButton19;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				jsq1 thisClass = new jsq1();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public jsq1() {
		super();
		operater.push("#"); // 初始化符号栈,让栈存放"#"
		dt.push("#");// 初始化符号栈,让栈存放"#"
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 408);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("计算器");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanel1(), null);
			jContentPane.add(getJPanel2(), null);
		}
		return jContentPane;
	}

} // @jve:decl-index=0:visual-constraint="22,3"

// Class ListNode defination
class ListNode {
	// fridendly data so class List can access it directly
	Object data;
	ListNode next;

	// Constructor:Create a ListNode that refers to Object o
	ListNode(Object o) {
		data = o; // this node refers to Object o
		next = null;// Set next to null
	}

	// Constructor:Create a ListNode that refers to Object o and
	// to the next ListNode int the List
	ListNode(Object o, ListNode nextNode) {
		data = o; // this node refers to Objext o
		next = nextNode; // set next to refer to next
	}

	// Return the Object in this node
	Object getObject() {
		return data;
	}

	// Return the next node
	ListNode getnext() {
		return next;
	}
}

// Class List defination
class List {
	protected ListNode firstNode;
	private ListNode lastNode;
	private String name; // String like "list" used in printing

	// Constructor:Construct an empty List with s as the name
	public List(String s) {
		name = s;
		firstNode = lastNode = null;
	}

	// Constructor:Construct an empty List with "list" as the name
	public List() {
		this("list");
	}

	// Insert an Object at the front of the List
	// If List is empty,firstNode and lastNode refer to
	// same Object.Otherwise,firstNode refers to new node;
	public void insertAtFront(Object insertItem) {
		if (isEmpty()) {
			firstNode = lastNode = new ListNode(insertItem);
		} else {
			firstNode = new ListNode(insertItem, firstNode);
		}
	}

	// Insert an Object at the end of the List
	// If List is empty,firstNode and lastNode refer to same Object
	// Otherwise,lastNode's next instance variable refers to new node
	public void insertAtBack(Object insertItem) {
		if (isEmpty()) {
			firstNode = lastNode = new ListNode(insertItem);
		} else {
			lastNode = lastNode.next = new ListNode(insertItem);
		}
	}

	// Remove the first node from the List
	public Object removeFromFront() throws EmptyListException {
		Object removeItem = null;
		if (isEmpty()) {
			throw new EmptyListException(name);
		}
		removeItem = firstNode.data;// retrieve the data
		// reset the firstNode and lastNode references
		if (firstNode.equals(lastNode)) {
			firstNode = lastNode = null;
		} else {
			firstNode = firstNode.next;
		}
		return removeItem;
	}

	// Remove the last node from the List
	public Object removeFromBack() throws EmptyListException {
		Object removeItem = null;
		if (isEmpty()) {
			throw new EmptyListException(name);
		}
		removeItem = lastNode.data;// retrieve the data
		// reset the firstNode and lastNode references
		if (firstNode.equals(lastNode)) {
			firstNode = lastNode = null;
		} else {
			ListNode current = firstNode;
			while (current.next != lastNode) {
				current = current.next;
			}
			lastNode = current;
			current.next = null;
		}
		return removeItem;
	}

	// Return true if the List is empty
	public boolean isEmpty() {
		return firstNode == null;
	}

	// Output the List contents
	public void print() {
		if (isEmpty()) {
			System.out.println("Empty" + name);
			return;
		}
		System.out.print("The" + name + "is:");
		ListNode current = firstNode;
		while (current != null) {
			System.out.print(current.data.toString() + " ");
			current = current.next;
		}
		System.out.println();
		System.out.println();
	}
}

// Class EmptyListException definition
class EmptyListException extends RuntimeException {
	public EmptyListException(String name) {
		super("The" + name + "is empty");
	}
}

// Class StackInheritance definiton
// Derived from class List
class StackInheritance extends List {
	StackInheritance() {
		super("stack");
	}

	StackInheritance(String s) {
		super(s);
	}

	public void push(Object o) {
		super.insertAtFront(o);
	}

	public Object pop() throws EmptyListException {
		return super.removeFromFront();
	}

	public boolean isEmpty() {
		return super.isEmpty();
	}

	public void print() {
		super.print();
	}

}

⌨️ 快捷键说明

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