用java寫一個簡易的電腦
運作結果如圖所示,輸入四則運算表達式,右下角顯示結果。
項目源檔案
https://download.csdn.net/download/kuangpeng1956/10791222
屬性及方法清單
屬性清單
JTextArea topArea =new JTextArea(); //四則運算b表達式顯示部分
JTextField resultArea =new JTextField();//結果顯示部分
int btnNum =19; //按鈕數目
JButton btn[] =new JButton[btnNum]; //界面上的按鈕
private boolean firstPress ;//是否還沒有按下按鈕過
private double result; //運算結果
方法清單
public void initTop() //初始化界面頭部
public void initCenter() //初始化界面中間部分
public void initFrame(JFrame frame) //初始化界面
public calculatorDemo() //構造函數
public void initValue() //初始化屬性值
public void handleCE() //當CE按鈕點選時的處理函數
public void HandleC() //當C按鈕點選時的處理函數
public void HandleOperator(String sym)//當運算符點選時的處理函數,sym點選的運算符
public void HandleEqual() //當等于号點選時的處理函數
public void handleDigit(String sym) //當數字點選時的處理函數
public void handleDot() //當小數點點選時處理函數
public boolean canDot() //此時是否能輸入小數點
public void calValue() //計算表達式的結果
public void actionPerformed(ActionEvent e) //按鈕監聽器
package CalculatorDemo;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.omg.CORBA.INTERNAL;
import testDemo.testDemo;
public class calculatorDemo extends JFrame implements ActionListener{
JTextArea topArea =new JTextArea();
JTextField resultArea =new JTextField();
int btnNum =19;
JButton btn[] =new JButton[btnNum];
private boolean firstPress ;
private double result;
public void initTop(){
//設計頭部
JPanel topPanel =new JPanel();
topPanel.setLayout(new GridLayout(2, 1));
topArea.setEditable(false);
topArea.setText("0");
resultArea.setEditable(false);
resultArea.setBackground(Color.white);
resultArea.setBorder(null);
resultArea.setHorizontalAlignment(JTextField.RIGHT);
resultArea.setText("");
topPanel.add(topArea);
topPanel.add(resultArea);
getContentPane().add(topPanel, "North");
}
public void buildConstraints(GridBagConstraints gbc,int gx,int gy,int gw,int gh,int wx,int wy){
gbc.gridx =gx;
gbc.gridy = gy;
gbc.gridwidth=gw;//橫跨幾個單元格
gbc.gridheight=gh;//豎跨幾個單元格
gbc.weightx=wx;
gbc.weighty=wy;
}
public void initCenter(){
//設計中間部分
JPanel centerPanel = new JPanel();
GridBagLayout centerLayout= new GridBagLayout();
GridBagConstraints gbc =new GridBagConstraints();
centerPanel .setLayout(centerLayout);
String btnText[] =new String[]{"C","/","*","CE","7","8","9","-","4","5","6",
"+","1","2","3","=","%","0","." };
for(int i=0;i<btnNum;i++){
btn[i] =new JButton(btnText[i]);
btn[i].addActionListener(this);
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor =GridBagConstraints.EAST;
if(btnText[i]!="=")
buildConstraints(gbc,i-i/4*4,i/4,1,1,1,1);
else{
buildConstraints(gbc,i-i/4*4,i/4,1,2,1,1);
}
centerLayout.setConstraints(btn[i], gbc);
btn[i].setActionCommand(btnText[i]);
centerPanel.add(btn[i]);
}
getContentPane().add(centerPanel, "Center");
}
public void initValue(){
firstPress =true;
result =0.0;
}
public void initFrame(JFrame frame){
initTop();
initCenter();
initValue();
pack();
setVisible(true);
}
public calculatorDemo(){
super("電腦示例");
setDefaultCloseOperation(EXIT_ON_CLOSE);
initFrame(this);
}
public void handleCE(){
initValue();
initTop();
}
public void HandleC(){
String text = topArea.getText();
if(firstPress){
return;
}else{
if(text.length()==1){
handleCE();
}else{
text = text.substring(0, text.length()-1);
topArea.setText(text);
calValue();
}
}
}
public void HandleOperator(String sym){
String text = topArea.getText();
int len = text.length();
if(firstPress){
return ;
}else{
if("0123456789".indexOf(text.charAt(len-1))>=0){
topArea.append(sym);
}
}
}
public void HandleEqual(){
calValue();
resultArea.setText("");
if(result==(int)result)
topArea.setText((int)result+"");
else{
DecimalFormat dFormat =new DecimalFormat("0.000000");
System.out.println(dFormat.format(result)+"");
}
firstPress =true;
}
public void handleDigit(String sym){
if(firstPress){
firstPress = false;
topArea.setText("");
}
topArea.append(sym);
calValue();
}
public void handleDot(){
System.out.println(canDot());
if(canDot())
topArea.append(".");
}
public boolean canDot(){
String text =topArea.getText();
int len =text.length();
if(firstPress||("0123456789".indexOf(text.charAt(len-1))<0))
return false;
boolean hasDot = false;
for(int i=len-1;i>=0;i--){
char ch =text.charAt(i);
if(("0123456789".indexOf(text.charAt(i))>0)){
continue;
}else if(ch=='.'){
hasDot =true;
break;
}else{
break;
}
}
if(!hasDot)
return true;
else
return false;
}
public void calValue(){
result =0;
int k=0;
String text =topArea.getText();
String tem =text;
double cal[] =new double[text.length()];
for(int i=0;i<=text.length();i++){
if(i<text.length()&&"0123456789.".indexOf(text.charAt(i))>=0){
continue;
}else{
cal[0] =Double.parseDouble(tem.substring(0,i));
tem =tem.substring(i);
break;
}
}
while(tem.length()>1){
char ch =tem.charAt(0);
tem =tem.substring(1);
double mid =0;
for(int i=0;i<=tem.length();i++){
if(i<tem.length()&&"0123456789.".indexOf(tem.charAt(i))>=0){
continue;
}else{
mid = Double.parseDouble(tem.substring(0,i));
tem =tem.substring(i);
break;
}
}
switch(ch){
case '+':
++k;
cal[k] = mid;
break;
case '-':
++k;
cal[k]=-mid;
break;
case '*':
cal[k]= cal[k]*mid;
break;
case '/':
cal[k]=cal[k]/mid;
break;
case '%':
cal[k] =cal[k]/mid;
break;
}
}
for(int j=0;j<=k;j++)
result +=cal[j];
if(result==(int)result)
resultArea.setText((int)result+"");
else{
DecimalFormat dFormat =new DecimalFormat("0.000000");
resultArea.setText(dFormat.format(result)+"");
}
}
@Override
public void actionPerformed(ActionEvent e) {
String pressValue = e.getActionCommand();
if(pressValue=="CE"){
handleCE();
}else if(pressValue=="C"){
HandleC();
}else if("+-*/%".indexOf(pressValue)>=0){
HandleOperator(pressValue);
}else if("0123456789".indexOf(pressValue)>=0){
handleDigit(pressValue);
}else if(".".indexOf(pressValue)>=0){
handleDot();
}else if("="==pressValue){
HandleEqual();
}
}
public static void main(String[] args) {
JFrame myFrame =new calculatorDemo();
}
}