alexa-tracking

Main Content

1024
1024
KASKUS
51
244
https://www.kaskus.co.id/thread/53268022f9ca17b5178b4637/ce-dan-c
CE dan C
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Calculator extends JFrame {
private JTextField displayField;
private boolean angkaPertama = true;
private String operator = "=";
private OtakKalkulator logik = new OtakKalkulator();
public static void main(String args[]){

Calculator window = new Calculator();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(800,800);
window.setVisible(true);
}

public Calculator()
{
super("Calculator");

displayField = new JTextField("0",20);
displayField.setHorizontalAlignment(JTextField.LEFT);

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4,3));
ActionListener numListener = new NumListener();
ActionListener clearListener = new ClearListener();
ActionListener hapusListener = new HapusListener();
JButton tombol0 = new JButton("0");
JButton tombol1 = new JButton("1");
JButton tombol2 = new JButton("2");
JButton tombol3 = new JButton("3");
JButton tombol4 = new JButton("4");
JButton tombol5 = new JButton("5");
JButton tombol6 = new JButton("6");
JButton tombol7 = new JButton("7");
JButton tombol8 = new JButton("8");
JButton tombolHapusSemua = new JButton("CE");
JButton tombol9 = new JButton("9");
JButton tombolHapus = new JButton("C");
buttonPanel.add(tombol1);
tombol1.addActionListener(numListener);
buttonPanel.add(tombol2);
tombol2.addActionListener(numListener);
buttonPanel.add(tombol3);
tombol3.addActionListener(numListener);
buttonPanel.add(tombol4);
tombol4.addActionListener(numListener);
buttonPanel.add(tombol5);
tombol5.addActionListener(numListener);
buttonPanel.add(tombol6);
tombol6.addActionListener(numListener);
buttonPanel.add(tombol7);
tombol7.addActionListener(numListener);
buttonPanel.add(tombol8);
tombol8.addActionListener(numListener);
buttonPanel.add(tombol9);
tombol9.addActionListener(numListener);
buttonPanel.add(tombolHapusSemua);
tombolHapusSemua.addActionListener(clearListener);
buttonPanel.add(tombol0);
tombolHapus.addActionListener(hapusListener);
buttonPanel.add(tombolHapus);

ActionListener opListener = new OpListener();

JPanel opPanel = new JPanel();
opPanel.setLayout(new GridLayout(5,1));
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton kali = new JButton("*");
JButton bagi = new JButton("/");
JButton samadengan = new JButton("=");
opPanel.add(plus);
plus.addActionListener(opListener);
opPanel.add(minus);
minus.addActionListener(opListener);
opPanel.add(kali);
kali.addActionListener(opListener);
opPanel.add(bagi);
bagi.addActionListener(opListener);
opPanel.add(samadengan);
samadengan.addActionListener(opListener);




JPanel isi = new JPanel();
isi.setLayout(new BorderLayout());
isi.add(displayField, BorderLayout.NORTH );
isi.add(buttonPanel, BorderLayout.CENTER);
isi.add(opPanel, BorderLayout.EAST);

this.setContentPane(isi);
this.pack();

}
private void actionHapusSemua() {
angkaPertama = true;
displayField.setText("0");
operator = "=";
logik.totalSekarang("0");
}

private void actionHapus() {
angkaPertama = false;
displayField.setText("0");
operator = "=";

}


class OpListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
angkaPertama = true;
String displayText = displayField.getText();

if (operator.equals("=")) {
logik.totalSekarang(displayText);
} else if (operator.equals("+")) {
logik.tambah(displayText);
} else if (operator.equals("-")) {
logik.kurang(displayText);
} else if (operator.equals("*")) {
logik.kali(displayText);
} else if (operator.equals("/")) {
logik.bagi(displayText);
}

displayField.setText("" + logik.keluarkanInput());
operator = e.getActionCommand();

}
}


class NumListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String numerik = e.getActionCommand();
if (angkaPertama) {
displayField.setText(numerik);
angkaPertama = false;
} else {

displayField.setText(displayField.getText() + numerik);
}
}
}



class ClearListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
actionHapusSemua();
}
}

class HapusListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
actionHapusSemua();
}
}


}


Kalau CE(Clear Everything) kan menghapus semuanya, masalah tinggal C(Clear), gimana implementasi dari logika ke program yang bingung. Kalau logikanya 6+2 = 8 tapi kalau diclear keluar 0 tapi kalau ditambah 3 jadi 6+3 = 9. Ada yang bisa bantu?

Spoiler for Kelas tambahan:
×