Şimdi Ara

Butona Basınca KeyListener Çalışmıyor

Daha Fazla
Bu Konudaki Kullanıcılar: Daha Az
2 Misafir - 2 Masaüstü
5 sn
1
Cevap
0
Favori
653
Tıklama
Daha Fazla
İstatistik
  • Konu İstatistikleri Yükleniyor
0 oy
Öne Çıkar
Sayfa: 1
Giriş
Mesaj
  • Arkadaşlar basit bir hesap makinesi kodu yazdım. Program ilk açılışta klavyeden sayıları okuyabiliyor. Ama herhangi bir butona veya textboxa basınca okumayı bırakıyor. Kod şu şekilde.

    quote:

    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.SwingConstants;
    import java.awt.Color;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    public class HesapMakinesi {

    /**
    * Launch the application.
    */
    protected JFrame frame;
    protected String islem = "";
    protected double ilkSayi, ikinciSayi;
    protected boolean islemYapildi = true, noktaKoyuldu = false;
    protected JTextField txt_ekran;

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    HesapMakinesi window = new HesapMakinesi();
    window.frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the application.
    */
    public HesapMakinesi() {
    initialize();
    }

    /**
    * Initialize the contents of the frame.
    */
    private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 553, 298);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    frame.addKeyListener(klavyeDinleyici);
    frame.setFocusable(true);

    JPanel panel = new JPanel();
    panel.setBounds(0, 0, 537, 259);
    frame.getContentPane().add(panel);
    panel.setLayout(null);

    txt_ekran = new JTextField();
    txt_ekran.setBackground(Color.WHITE);
    txt_ekran.setEditable(false);
    txt_ekran.setText("0");
    txt_ekran.setHorizontalAlignment(SwingConstants.RIGHT);
    txt_ekran.setBounds(10, 11, 402, 46);
    panel.add(txt_ekran);
    txt_ekran.setColumns(10);

    JButton btn1 = new JButton("1");
    btn1.setBounds(10, 86, 89, 31);
    panel.add(btn1);

    JButton btn2 = new JButton("2");
    btn2.setBounds(109, 86, 89, 31);
    panel.add(btn2);

    JButton btn3 = new JButton("3");
    btn3.setBounds(208, 86, 89, 31);
    panel.add(btn3);

    JButton btn4 = new JButton("4");
    btn4.setBounds(10, 130, 89, 31);
    panel.add(btn4);

    JButton btn5 = new JButton("5");
    btn5.setBounds(109, 128, 89, 31);
    panel.add(btn5);

    JButton btn6 = new JButton("6");
    btn6.setBounds(208, 128, 89, 31);
    panel.add(btn6);

    JButton btn7 = new JButton("7");
    btn7.setBounds(10, 173, 89, 31);
    panel.add(btn7);

    JButton btn8 = new JButton("8");
    btn8.setBounds(109, 170, 89, 31);
    panel.add(btn8);

    JButton btn9 = new JButton("9");
    btn9.setBounds(208, 170, 89, 31);
    panel.add(btn9);

    JButton btn0 = new JButton("0");
    btn0.setBounds(109, 212, 89, 31);
    panel.add(btn0);

    JButton btn_nokta = new JButton(".");
    btn_nokta.setBounds(10, 212, 89, 31);
    panel.add(btn_nokta);

    JButton btn00 = new JButton("00");
    btn00.setBounds(208, 212, 89, 31);
    panel.add(btn00);

    btn1.addActionListener(dinleyici);
    btn2.addActionListener(dinleyici);
    btn3.addActionListener(dinleyici);
    btn4.addActionListener(dinleyici);
    btn5.addActionListener(dinleyici);
    btn6.addActionListener(dinleyici);
    btn7.addActionListener(dinleyici);
    btn8.addActionListener(dinleyici);
    btn9.addActionListener(dinleyici);
    btn0.addActionListener(dinleyici);
    btn00.addActionListener(dinleyici);
    btn_nokta.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (!noktaKoyuldu) {
    if (!islemYapildi)
    txt_ekran.setText(txt_ekran.getText() + ".");
    else {
    islemYapildi = false;
    txt_ekran.setText("0.");
    }
    noktaKoyuldu = true;
    }

    }
    });

    JButton btn_Ce = new JButton("CE");
    btn_Ce.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    txt_ekran.setText("");
    }
    });
    btn_Ce.setBounds(422, 11, 99, 46);
    panel.add(btn_Ce);

    JButton btn_arti = new JButton("+");
    btn_arti.setBounds(330, 85, 89, 46);
    panel.add(btn_arti);

    JButton btn_eksi = new JButton("-");
    btn_eksi.setBounds(432, 85, 89, 46);
    panel.add(btn_eksi);

    JButton btn_carpi = new JButton("X");
    btn_carpi.setBounds(330, 142, 89, 46);
    panel.add(btn_carpi);

    JButton btn_bolu = new JButton("/");
    btn_bolu.setBounds(432, 142, 89, 46);
    panel.add(btn_bolu);

    btn_arti.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    islem = "topla";
    try {
    ilkSayi = Double.parseDouble(txt_ekran.getText());
    } catch (NumberFormatException e1) {
    }

    txt_ekran.setText("");
    }
    });

    btn_eksi.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    islem = "çıkar";
    try {
    ilkSayi = Double.parseDouble(txt_ekran.getText());
    } catch (NumberFormatException e1) {
    }
    txt_ekran.setText("");
    noktaKoyuldu = false;
    }
    });

    btn_carpi.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    islem = "çarp";
    try {
    ilkSayi = Double.parseDouble(txt_ekran.getText());
    } catch (NumberFormatException e1) {
    }
    txt_ekran.setText("");
    noktaKoyuldu = false;
    }
    });

    btn_bolu.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    islem = "böl";
    try {
    ilkSayi = Double.parseDouble(txt_ekran.getText());
    } catch (NumberFormatException e1) {
    }
    txt_ekran.setText("");
    noktaKoyuldu = false;
    }
    });

    JButton btn_esittir = new JButton("=");
    btn_esittir.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    ikinciSayi = Double.parseDouble(txt_ekran.getText());
    switch (islem) {
    case "topla":
    ekranaYaz(String.valueOf(ilkSayi + ikinciSayi));
    break;
    case "çıkar":
    ekranaYaz(String.valueOf(ilkSayi - ikinciSayi));
    break;
    case "çarp":
    ekranaYaz(String.valueOf(ilkSayi * ikinciSayi));
    break;
    case "böl":
    ekranaYaz(String.valueOf(ilkSayi / ikinciSayi));
    break;
    default:
    break;
    }
    islemYapildi = true;
    islem = "";
    noktaKoyuldu = false;
    }
    });
    btn_esittir.setBounds(330, 199, 191, 45);
    panel.add(btn_esittir);
    }

    ActionListener dinleyici = new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    if (!islemYapildi) {
    txt_ekran.setText(txt_ekran.getText() + e.getActionCommand());
    } else {
    islemYapildi = false;
    txt_ekran.setText(e.getActionCommand());
    }
    }
    };

    KeyListener klavyeDinleyici = new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
    if (e.getKeyChar() == '0' || e.getKeyChar() == '1' || e.getKeyChar() == '2' || e.getKeyChar() == '3'
    || e.getKeyChar() == '4' || e.getKeyChar() == '5' || e.getKeyChar() == '6' || e.getKeyChar() == '7'
    || e.getKeyChar() == '8' || e.getKeyChar() == '9') {
    if (!islemYapildi) {
    txt_ekran.setText(txt_ekran.getText() + e.getKeyChar());
    } else {
    islemYapildi = false;
    txt_ekran.setText("" + e.getKeyChar());
    }
    }
    }
    };

    public void ekranaYaz(String sayi) {
    if (sayi.endsWith(".0"))
    txt_ekran.setText(sayi.substring(0, sayi.length() - 2));
    else
    txt_ekran.setText(sayi);
    }
    }







  • 
Sayfa: 1
- x
Bildirim
mesajınız kopyalandı (ctrl+v) yapıştırmak istediğiniz yere yapıştırabilirsiniz.