1
私は初心者で、簡単な電卓を作ろうとしています。私は他の回答を探しましたが、私の問題は、オペレータを押した後、それはちょうどあなたの業務リスナーの最初の文字列だけ私は操作が正常に動作するようになっています
public class CalculatorFrame extends JFrame {
public CalculatorFrame(){
this.setTitle("Calculator");
this.setLayout(new BorderLayout(2,2));
MenuBar menuBar = new MenuBar();
JTextArea txtArea = new JTextArea();
txtArea.setEditable(false);
txtArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txtArea.setText("0");
txtArea.setFont(new Font("Sans Serif", Font.PLAIN,30));
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4,3,2,2));
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(6,1,2,2));
MyActionListener al = new MyActionListener(txtArea);
MyOperatorListener ol = new MyOperatorListener(txtArea);
JButton btn = new JButton();
int k=10;
for (int i=0; i<3; i++) {
k=k-3;
for (int j=0; j<3; j++) {
btn = new JButton(Integer.toString(k++));
btn.setPreferredSize(new Dimension(70, 60));
btn.setBackground(new Color(224,224,224));
btn.setBorder(BorderFactory.createRaisedSoftBevelBorder());
btn.setFont(new Font("Sans Serif", Font.BOLD,20));
btn.addActionListener(al);
panel.add(btn);
}
k=k-3;
}
JButton btn7 = new JButton("0");
btn7.addActionListener(al);
btn7.setBackground(new Color(224,224,224));
btn7.setPreferredSize(new Dimension(70, 60));
btn7.setBorder (BorderFactory.createRaisedSoftBevelBorder()) ;
btn7.setFont(new Font("Sans Serif", Font.BOLD,20));
JButton btn8 = new JButton(".");
btn8.addActionListener(al);
btn8.setBackground(new Color(224,224,224));
btn8.setPreferredSize(new Dimension(70, 60));
btn8.setBorder (BorderFactory.createRaisedSoftBevelBorder()) ;
btn8.setFont(new Font("Sans Serif", Font.BOLD,20));
JButton btn9 = new JButton("=");
btn9.addActionListener(ol);
btn9.setBackground(new Color(0,128,255));
btn9.setBorder (BorderFactory.createEtchedBorder()) ;
btn9.setPreferredSize(new Dimension(70, 60));
btn9.setFont(new Font("Sans Serif", Font.BOLD,20));
btn9.setToolTipText("calculate result");
JButton btn1 = new JButton("CE");
btn1.addActionListener(ol);
btn1.setBackground(new Color(160,160,160));
btn1.setPreferredSize(new Dimension(50, 40));
btn1.setFont(new Font("Sans Serif", Font.BOLD,12));
btn1.setToolTipText("clear entry");
JButton btn2 = new JButton("C");
btn2.addActionListener(ol);
btn2.setBackground(new Color(160,160,160));
btn2.setPreferredSize(new Dimension(50, 40));
btn2.setFont(new Font("Sans Serif", Font.BOLD,15));
btn2.setToolTipText("clear");
JButton btn3 = new JButton("/");
btn3.addActionListener(ol);
btn3.setBackground(new Color(160,160,160));
btn3.setPreferredSize(new Dimension(50, 40));
btn3.setFont(new Font("Sans Serif", Font.BOLD,15));
btn3.setToolTipText("divide");
JButton btn4 = new JButton("*");
btn4.addActionListener(ol);
btn4.setBackground(new Color(160,160,160));
btn4.setPreferredSize(new Dimension(50, 40));
btn4.setFont(new Font("Sans Serif", Font.BOLD,15));
btn4.setToolTipText("multiplie");
JButton btn5 = new JButton("-");
btn5.addActionListener(ol);
btn5.setBackground(new Color(160,160,160));
btn5.setPreferredSize(new Dimension(50, 40));
btn5.setFont(new Font("Sans Serif", Font.BOLD,15));
btn5.setToolTipText("subtract");
JButton btn6 = new JButton("+");
btn6.addActionListener(ol);
btn6.setBackground(new Color(160,160,160));
btn6.setPreferredSize(new Dimension(50, 40));
btn6.setFont(new Font("Sans Serif", Font.BOLD,15));
btn6.setToolTipText("add");
this.add(txtArea, BorderLayout.NORTH);
this.add(panel,BorderLayout.CENTER);
this.add(panel2,BorderLayout.EAST);
panel.add(btn7);
panel.add(btn8);
panel.add(btn9);
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
panel2.add(btn4);
panel2.add(btn5);
panel2.add(btn6);
this.setJMenuBar(menuBar);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setVisible(true);
}
}
public class MyActionListener implements ActionListener {
JTextArea area;
public MyActionListener(JTextArea area){
this.area = area;
}
@Override
public void actionPerformed(ActionEvent e) {
JButton x = (JButton) e.getSource();
String y = x.getText();
String z = area.getText();
int v = area.getText().length();
int i = z.indexOf('.');
if(i==1 && "0".equals(area.getText())){
area.setText("");
}
if(v<15){
if(i>=0&&".".equals(y)){
area.append("");
}else{
area.append(y);
}
}else{
Toolkit.getDefaultToolkit().beep();
}
}
}
public class MyOperatorListener implements ActionListener {
JTextArea area;
double total;
public MyOperatorListener(JTextArea area){
this.area = area;
}
@Override
public void actionPerformed(ActionEvent e) {
JButton x = (JButton) e.getSource();
String y = x.getText();
double calc = Double.parseDouble(area.getText());
switch(y){
case "+": total = total + calc;
break;
case "-": total = total - calc;
break;
case "*": total = total * calc;
break;
case "/": if(calc!=0){
total = total/calc;
}
break;
default: break;
}
switch(y){
case "CE": calc = 0;
area.setText("0");
break;
case "C": total = 0;
calc = 0;
area.setText("0");
break;
case "=": area.setText(Double.toString(total));
break;
}
}
}
ありがとうございました!どのようにそれを行うにはどのような提案? – DimiK
電卓を作成したとき、私は各操作にブーリアンを使用しました。値が入力され、その後に操作が実行されると、操作はtrueになります。 2番目の値が入力され、equalsが押されたときには、どの操作が真であるかがわかります(+が真であれば、合計+面積) –
これは助けになりました。ありがとう! – DimiK