2013-04-20 12 views
1

누군가가 내 계산기 기록을 문자열로 저장하고 사용자가 H 키를 눌렀을 때 기록을 보여줄 수있는 기능을 도와 주길 바랬습니다. 운영자를 포함하여 눌러 진 각 버튼을 기록하려고합니다. 어떤 도움을 주셔서 감사합니다. 감사.자바 계산기 히스토리 기능

public class GUI extends JPanel implements KeyListener { 


private JButton b1; 
private JButton b2; 
private JButton b3; 
private JButton b4; 
private JButton b5; 
private JButton b6; 
private JButton b7; 
private JButton b8; 
private JButton b9; 
private JButton b0; 
private JButton add; 
private JButton subtract; 
private JButton multiply; 
private JButton divide; 
private JButton solve; 
private JButton clear; 
private JButton decimal; 
private JButton sqrt; 
private JButton recip; 
private JButton backSpace; 
private JButton ms; 
private JButton mr; 
private JButton mc; 
private JButton percent; 
private JButton love; 
private JTextField jtf; 
String display = ""; 
String mStore; 
String history; 
boolean multiplyBool = false; 
boolean divideBool = false; 
boolean addBool = false; 
boolean subtractBool = false; 
boolean percentBool = false; 
ImageIcon heart = new ImageIcon("heartt.png"); 

private CalcLogic cl = new CalcLogic(); 


public GUI(){ 
setLayout(new BorderLayout()); 

JPanel p3 = new JPanel(new BorderLayout()); 
p3.setFont(new Font("Arial", Font.BOLD, 30)); 
p3.add(jtf = new JTextField("0",12)); 
jtf.setEditable(false); 
jtf.setHorizontalAlignment(JTextField.RIGHT); 
jtf.setFont(new Font("Arial", Font.BOLD, 40)); 




JPanel p2 = new JPanel(); 
p2.setBackground(Color.CYAN); 

p2.setLayout(new GridLayout(5,5,2,2)); 


//row 1 buttons 


p2.add(mc = new JButton("MC")); 
mc.setFont(new Font("Arial", Font.BOLD, 12)); 

p2.add(mr = new JButton("MR")); 
mr.setFont(new Font("Arial", Font.BOLD, 12)); 

p2.add(ms = new JButton("MS")); 
ms.setFont(new Font("Arial", Font.BOLD, 12)); 

p2.add(divide = new JButton("\u00F7")); 
divide.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(backSpace = new JButton("\u2190")); 
backSpace.setFont(new Font("Arial Bold", Font.BOLD, 18)); 


//row 2 buttons 


p2.add(b7 = new JButton("7")); 
b7.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(b8 = new JButton("8")); 
b8.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(b9 = new JButton("9")); 
b9.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(multiply = new JButton("\u00D7")); 
multiply.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(sqrt = new JButton("\u221A")); 
sqrt.setFont(new Font("Arial", Font.PLAIN, 20)); 


//row 3 buttons 


p2.add(b4 = new JButton("4")); 
b4.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(b5 = new JButton("5")); 
b5.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(b6 = new JButton("6")); 
b6.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(subtract = new JButton("\u2212")); 
subtract.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(recip = new JButton("1/x")); 
recip.setFont(new Font("Arial", Font.PLAIN, 14)); 


//row 4 buttons 


p2.add(b1 = new JButton("1")); 
b1.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(b2 = new JButton("2")); 
b2.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(b3 = new JButton("3")); 
b3.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(add = new JButton("+")); 
add.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(percent = new JButton("%")); 
percent.setFont(new Font("Arial", Font.PLAIN, 20)); 


//row 5 buttons 


p2.add(b0 = new JButton("0")); 
b0.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(decimal = new JButton(".")); 
decimal.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(clear = new JButton("C")); 
clear.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(solve = new JButton("=")); 
solve.setFont(new Font("Arial", Font.PLAIN, 20)); 

p2.add(love = new JButton(heart)); 
love.addActionListener(new LoveListener()); 

add(p3, BorderLayout.NORTH); 
add(p2, BorderLayout.CENTER); 
this.setFocusable(true); 
addKeyListener(this); 

ActionListener buttonListener = new buttonListener(); 

ActionListener ClearListener = new ClearListener(); 



b7.addActionListener(buttonListener); 
b8.addActionListener(buttonListener); 
b9.addActionListener(buttonListener); 
add.addActionListener(new addListener()); 
b4.addActionListener(buttonListener); 
b5.addActionListener(buttonListener); 
b6.addActionListener(buttonListener); 
subtract.addActionListener(new subtractListener()); 
b1.addActionListener(buttonListener); 
b2.addActionListener(buttonListener); 
b3.addActionListener(buttonListener); 
multiply.addActionListener(new multiplyListener()); 
b0.addActionListener(buttonListener); 
decimal.addActionListener(buttonListener); 
clear.addActionListener(ClearListener); 
solve.addActionListener(new solveListener()); 
divide.addActionListener(new divideListener()); 
sqrt.addActionListener(new sqrtListener()); 
recip.addActionListener(new recipListener()); 
backSpace.addActionListener(new backSpaceListener()); 
ms.addActionListener(new msListener()); 
mr.addActionListener(new mrListener()); 
mc.addActionListener(new mcListener()); 
percent.addActionListener(new percentListener()); 

} //GUI() 

    private void action_clear(){ 

jtf.setText(""); 
display = jtf.getText(); 
//op = ""; 
cl.setTotal("0"); 
multiplyBool = false; 
addBool = false; 
subtractBool = false; 
divideBool = false; 

    } 




    public class buttonListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{  
    String digit = e.getActionCommand(); 


    jtf.setText(display + digit); 
    display = jtf.getText(); 
    playSound("Click.wav"); 
} 
    }//buttonListener 

    public class multiplyListener implements ActionListener 
    { 
private CalcLogic cl = new CalcLogic(); 

public void actionPerformed(ActionEvent e) 
{ 
    if(multiplyBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
     } 
     cl.multiply(display); 
     jtf.setText(cl.getString()); 
     display = ""; 
    }else if(divideBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
     } 
     cl.divide(display); 
     jtf.setText(cl.getString()); 
     display = ""; 
    }else if(addBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
      cl.percMult(display); 
      display = cl.getPercent(); 

     } 

     cl.add(display); 

     jtf.setText(cl.getString()); 
     display = ""; 
    }else if (subtractBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
      cl.percMult(display); 
      display = cl.getPercent(); 
     } 
     cl.subtract(display); 
     jtf.setText(cl.getString()); 
     display = ""; 
    }else 
    { 
    cl.setTotal(display); 
    jtf.setText(""); 
    multiplyBool = true; 
    divideBool = false; 
    addBool = false; 
    subtractBool = false; 
    percentBool = false; 
    display = ""; 
    } 

    multiplyBool = true; 
    divideBool = false; 
    addBool = false; 
    subtractBool = false; 
    percentBool = false; 
} 

    } 

    public class divideListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 
    if(multiplyBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
     } 
     cl.multiply(display); 
     jtf.setText(cl.getString()); 
     display =""; 
    }else if(divideBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
     } 
     cl.divide(display); 
     jtf.setText(cl.getString()); 
     display = ""; 
    }else if(addBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
      cl.percMult(display); 
      display = cl.getPercent(); 

     } 

     cl.add(display); 

     jtf.setText(cl.getString()); 
     display = ""; 
    }else if (subtractBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
      cl.percMult(display); 
      display = cl.getPercent(); 
     } 
     cl.subtract(display); 
     jtf.setText(cl.getString()); 
     display = ""; 
    }else 
    { 
    cl.setTotal(display); 
    jtf.setText(""); 
    divideBool = true; 
    multiplyBool = false; 
    addBool = false; 
    subtractBool = false; 
    percentBool = false; 
    display = ""; 
    } 

    multiplyBool = false; 
    divideBool = true; 
    addBool = false; 
    subtractBool = false; 
    percentBool = false; 
} 
    } 
    public class addListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 
    if(multiplyBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
     } 
     cl.multiply(display); 
     jtf.setText(cl.getString()); 
     display =""; 
    }else if(divideBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
     } 
     cl.divide(display); 
     jtf.setText(cl.getString()); 
     display = ""; 
    }else if(addBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
      cl.percMult(display); 
      display = cl.getPercent(); 

     } 

     cl.add(display); 

     jtf.setText(cl.getString()); 
     display = ""; 
    }else if (subtractBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
      cl.percMult(display); 
      display = cl.getPercent(); 
     } 
     cl.subtract(display); 
     jtf.setText(cl.getString()); 
     display = ""; 
    }else 
    { 
    cl.setTotal(display); 
    jtf.setText(""); 
    divideBool = false; 
    multiplyBool = false; 
    addBool = true; 
    subtractBool = false; 
    percentBool = false; 
    display = ""; 
    } 

    divideBool = false; 
    multiplyBool = false; 
    subtractBool = false; 
    addBool = true; 
    percentBool = false; 

} 
    }//addListener 

    public class subtractListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 
    if(multiplyBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
     } 
     cl.multiply(display); 
     jtf.setText(cl.getString()); 
     display =""; 
    }else if(divideBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
     } 
     cl.divide(display); 
     jtf.setText(cl.getString()); 
     display = ""; 
    }else if(addBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
      cl.percMult(display); 
      display = cl.getPercent(); 

     } 

     cl.add(display); 

     jtf.setText(cl.getString()); 
     display = ""; 
    }else if (subtractBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
      cl.percMult(display); 
      display = cl.getPercent(); 
     } 
     cl.subtract(display); 
     jtf.setText(cl.getString()); 
     display = ""; 
    }else 
    { 
    cl.setTotal(display); 
    jtf.setText(""); 
    divideBool = false; 
    multiplyBool = false; 
    addBool = false; 
    subtractBool = true; 
    percentBool = false; 
    display = ""; 
    } 
    divideBool = false; 
    multiplyBool = false; 
    addBool = false; 
    subtractBool = true; 
    percentBool = false; 

} 
    }//subtractListener 

    public class sqrtListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 
    cl.setTotal(display); 
    cl.squareRoot(display); 
    jtf.setText(cl.getString()); 
    playSound("Click.wav"); 
} 
    } 

    public class recipListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 
    cl.setTotal(display); 
    cl.reciprical(display); 
    jtf.setText(cl.getString()); 
    playSound("Click.wav"); 
} 
    } 

    public class backSpaceListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 
    jtf.setText(""); 
    display = ""; 
} 


    } 

    public class msListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 
    mStore = jtf.getText(); 
} 
    } 

    public class mrListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 
    jtf.setText(mStore); 
    display = mStore; 

} 
    } 

    public class mcListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 
    mStore = ""; 
} 
    } 

    public class percentListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 

    percentBool = true; 
} 
    } 


    public class solveListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 
    playSound("that_was_easy.wav"); 

    if(multiplyBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
     } 
     cl.multiply(display); 
     jtf.setText(cl.getString()); 
    }else if(divideBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
     } 
     cl.divide(display); 
     jtf.setText(cl.getString()); 
    }else if(addBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
      cl.percMult(display); 
      display = cl.getPercent(); 

     } 

     cl.add(display); 

     jtf.setText(cl.getString()); 
    }else if (subtractBool == true) 
    { 
     if (percentBool==true) 
     { 
      cl.percentage(display); 
      display = cl.getPercent(); 
      cl.percMult(display); 
      display = cl.getPercent(); 
     } 
     cl.subtract(display); 
     jtf.setText(cl.getString()); 
    } 
    multiplyBool = false; 
    addBool = false; 
    subtractBool = false; 
    divideBool = false; 
    percentBool = false; 

    //display = jtf.getText(); 

} 

    } 


public class ClearListener implements ActionListener 
    { 
public void actionPerformed(ActionEvent e) 
{ 
    action_clear(); 
    playSound("Click.wav"); 
}//ClearListener 


    } 
public void keyTyped(KeyEvent e){ 

if(e.getKeyCode() == KeyEvent.VK_H){ 
    System.out.println(history); 
) 
public void keyTyped(KeyEvent e){ 

} 
public void keyReleased(KeyEvent e){ 

} 
} 

답변

0

지우기 전에 각 단일 라인 계산기 명령을 문자열로 배열에 저장하십시오. 당신은 예를 들어 H.

를 누를 때 그리고 그것을 보여

배열 배열 = 새로운 배열 (5)

(당신은 또한 역동적이고보다 효율적으로하기 위해 컬렉션 클래스를 사용할 수 있습니다)
123 (Line#1 in calculator) >> array[0] 
+ (Line#2 in calculator) >> array[1] 
34567 (Line#3 in calculator) >> array[2] 
% (Line#4 in calculator) >> array [3] 
345 (Line#5 in calculator) >> array [4] 

등등.

감사합니다.