Código java:
Ver originalpackage momentTemporal.Calculadora;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculadora{
private JButton btn_7,btn_8,btn_9,btn_div,btn_esb
; private JButton btn_4,btn_5,btn_6,btn_pro,btn_per
; private JButton btn_1,btn_2,btn_3,btn_sum,btn_arr
; private JButton btn_0,btn_dec,btn_igu,btn_res,btn_inv
;
public static void main
(String args
[]){ Calculadora calc = new Calculadora();
calc.disseny_calc();
calc.tractamentBotoNumeric();
calc.controlFinestra();
}
private void tractamentBotoNumeric(){
/*TractamentTeclat tt = new TractamentTeclat();
btn_7.addKeyListener(tt);*/
TractamentBotoNumeric tb = new TractamentBotoNumeric();
btn_7.addActionListener(tb);
btn_8.addActionListener(tb);
btn_9.addActionListener(tb);
//btn_div.addActionListener(tb);
btn_esb.addActionListener(tb);
btn_4.addActionListener(tb);
btn_5.addActionListener(tb);
btn_6.addActionListener(tb);
//btn_pro.addActionListener(tb);
//btn_per.addActionListener(tb);
btn_1.addActionListener(tb);
btn_2.addActionListener(tb);
btn_3.addActionListener(tb);
//btn_sum.addActionListener(tb);
//btn_arr.addActionListener(tb);
btn_0.addActionListener(tb);
btn_inv.addActionListener(tb);
btn_dec.addActionListener(tb);
//btn_igu.addActionListener(tb);
//btn_res.addActionListener(tb);
}
private void controlFinestra(){
}
});
}
private void disseny_calc(){
f
= new JFrame("Calculadora"); f.setSize(800,400);
//f.setLayout(new BorderLayout());
jta.setEditable(false);
jta.
setBackground(Color.
WHITE);
if (e.
getKeyCode() == KeyEvent.
VK_NUMPAD1) { //txtVisor.setText(txtVisor.getText() + "1");
jta.setText("1");
System.
out.
println(e.
getKeyCode()); }
}
});
btn_div.
setForeground(Color.
red); btn_esb.
setForeground(Color.
blue);
jp.add(btn_7);
jp.add(btn_8);
jp.add(btn_9);
jp.add(btn_div);
jp.add(btn_esb);
btn_pro.
setForeground(Color.
red); btn_per.
setForeground(Color.
blue);
jp.add(btn_4);
jp.add(btn_5);
jp.add(btn_6);
jp.add(btn_pro);
jp.add(btn_per);
btn_sum.
setForeground(Color.
red); btn_arr.
setForeground(Color.
blue);
jp.add(btn_1);
jp.add(btn_2);
jp.add(btn_3);
jp.add(btn_sum);
jp.add(btn_arr);
btn_igu.
setForeground(Color.
red); btn_res.
setForeground(Color.
blue);
jp.add(btn_0);
jp.add(btn_inv);
jp.add(btn_dec);
jp.add(btn_igu);
jp.add(btn_res);
f.pack();
f.setVisible(true);
}
private void setCalcul
(String tecla
){ String contingut
= jta.
getText();
if(digit.charAt(0)==','){
if(contingut.length()==0){
contingut = "0,";
}else{
if(contingut.indexOf(',')==-1){
contingut = contingut.concat(tecla);
}
}
jta.setText(contingut);
return;
}
if(digit.equals("+/-") == true){
if(contingut.equals("0") == false){
if(contingut.length()>0){
if(contingut.charAt(0) == '-'){
contingut = contingut.substring(1);
}else{
contingut = "-".concat(contingut);
}
}
}
jta.setText(contingut);
return;
}
if(digit.charAt(0) == 'C'){
contingut = "";
jta.setText(contingut);
return;
}
if(contingut.equals("0")){
contingut = contingut.substring(1);
contingut = contingut.concat(tecla);
}else{
contingut = contingut.concat(tecla);
}
jta.setText(contingut);
}
setCalcul(e.getActionCommand());
}
}
/*class TractamentTeclat implements KeyAdapter{
public void keyPressed(KeyEvent e){
//setCalcul(e.getKeyText());
//String num = e.getKeyText();
jta.setText("Hola");
}
}*/
}