Foros del Web » Programación para mayores de 30 ;) » Java »

Calculadora Java

Estas en el tema de Calculadora Java en el foro de Java en Foros del Web. Buenos dias!!! estoy programando una mini calc para el cole, de momento solo me concatena los numeros y me hace la inversa y decimales. Pero ...
  #1 (permalink)  
Antiguo 22/03/2012, 04:18
 
Fecha de Ingreso: septiembre-2011
Mensajes: 87
Antigüedad: 12 años, 7 meses
Puntos: 6
Calculadora Java

Buenos dias!!! estoy programando una mini calc para el cole, de momento solo me concatena los numeros y me hace la inversa y decimales. Pero poco a poco jeje...

Ahora lo que quiero es poder escribir con el teclado, pero el problema es que no se a que objeto aplicarle el keylistener.

[HIGHLIGHpackage momentTemporal.Calculadora;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculadora{

private JFrame f;
private JPanel jp;
private JTextField jta;
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(){
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}


private void disseny_calc(){

f = new JFrame("Calculadora");
f.setSize(800,400);



//f.setLayout(new BorderLayout());

jta = new JTextField();
jta.setEditable(false);
jta.setBackground(Color.WHITE);

jp = new JPanel();
jp.setLayout(new GridLayout(4, 5));

jp.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_NUMPAD1) {
//txtVisor.setText(txtVisor.getText() + "1");
jta.setText("1");
System.out.println(e.getKeyCode());
}
}
});

btn_7 = new JButton("7");
btn_8 = new JButton("8");
btn_9 = new JButton("9");
btn_div = new JButton("/");
btn_div.setForeground(Color.red);
btn_esb = new JButton("C");
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_4 = new JButton("4");
btn_5 = new JButton("5");
btn_6 = new JButton("6");
btn_pro = new JButton("*");
btn_pro.setForeground(Color.red);
btn_per = new JButton("%");
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_1 = new JButton("1");
btn_2 = new JButton("2");
btn_3 = new JButton("3");
btn_sum = new JButton("+");
btn_sum.setForeground(Color.red);
btn_arr = new JButton("1/x");
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_0 = new JButton("0");
btn_inv = new JButton("+/-");
btn_dec = new JButton(",");
btn_igu = new JButton("=");
btn_igu.setForeground(Color.red);
btn_res = new JButton("-");
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.add(jta, BorderLayout.NORTH);
f.add(jp, BorderLayout.CENTER);
f.pack();
f.setVisible(true);

}

private void setCalcul(String tecla){
String digit = 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);
}

class TractamentBotoNumeric implements ActionListener{

public void actionPerformed(ActionEvent e){
setCalcul(e.getActionCommand());
}
}

/*class TractamentTeclat implements KeyAdapter{
public void keyPressed(KeyEvent e){
//setCalcul(e.getKeyText());
//String num = e.getKeyText();
jta.setText("Hola");
}
}*/
}T="JAVA"]

[/HIGHLIGHT]

He intentado aplicarselo al frame, panel, o incluso al textfield, pero na de na!!!


gracias de antemano!!!
  #2 (permalink)  
Antiguo 22/03/2012, 04:18
 
Fecha de Ingreso: septiembre-2011
Mensajes: 87
Antigüedad: 12 años, 7 meses
Puntos: 6
Respuesta: Calculadora Java

joder para que coño sirve el hightlight!!!! sino hace naaa
  #3 (permalink)  
Antiguo 22/03/2012, 04:19
 
Fecha de Ingreso: septiembre-2011
Mensajes: 87
Antigüedad: 12 años, 7 meses
Puntos: 6
Respuesta: Calculadora Java

Código java:
Ver original
  1. package momentTemporal.Calculadora;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. public class Calculadora{
  8.    
  9.     private JFrame f;
  10.     private JPanel jp;
  11.     private JTextField jta;
  12.     private JButton btn_7,btn_8,btn_9,btn_div,btn_esb;
  13.     private JButton btn_4,btn_5,btn_6,btn_pro,btn_per;
  14.     private JButton btn_1,btn_2,btn_3,btn_sum,btn_arr;
  15.     private JButton btn_0,btn_dec,btn_igu,btn_res,btn_inv;
  16.    
  17.     public static void main(String args[]){
  18.         Calculadora calc = new Calculadora();
  19.         calc.disseny_calc();
  20.         calc.tractamentBotoNumeric();
  21.         calc.controlFinestra();
  22.     }
  23.    
  24.     private void tractamentBotoNumeric(){
  25.         /*TractamentTeclat tt = new TractamentTeclat();
  26.         btn_7.addKeyListener(tt);*/
  27.        
  28.         TractamentBotoNumeric tb = new TractamentBotoNumeric();
  29.        
  30.         btn_7.addActionListener(tb);
  31.         btn_8.addActionListener(tb);
  32.         btn_9.addActionListener(tb);
  33.         //btn_div.addActionListener(tb);
  34.         btn_esb.addActionListener(tb);
  35.        
  36.         btn_4.addActionListener(tb);
  37.         btn_5.addActionListener(tb);
  38.         btn_6.addActionListener(tb);
  39.         //btn_pro.addActionListener(tb);
  40.         //btn_per.addActionListener(tb);
  41.        
  42.         btn_1.addActionListener(tb);
  43.         btn_2.addActionListener(tb);
  44.         btn_3.addActionListener(tb);
  45.         //btn_sum.addActionListener(tb);
  46.         //btn_arr.addActionListener(tb);
  47.        
  48.         btn_0.addActionListener(tb);
  49.         btn_inv.addActionListener(tb);
  50.         btn_dec.addActionListener(tb);
  51.         //btn_igu.addActionListener(tb);
  52.         //btn_res.addActionListener(tb);
  53.     }
  54.    
  55.     private void controlFinestra(){
  56.         f.addWindowListener(new WindowAdapter() {
  57.                                     public void windowClosing(WindowEvent e) {
  58.                                         System.exit(0);
  59.                                     }
  60.                             });
  61.     }
  62.    
  63.  
  64.     private void disseny_calc(){
  65.    
  66.         f = new JFrame("Calculadora");
  67.         f.setSize(800,400);
  68.        
  69.        
  70.  
  71.         //f.setLayout(new BorderLayout());
  72.        
  73.         jta = new JTextField();
  74.         jta.setEditable(false);
  75.         jta.setBackground(Color.WHITE);
  76.        
  77.         jp = new JPanel();
  78.         jp.setLayout(new GridLayout(4, 5));
  79.        
  80.         jp.addKeyListener(new KeyAdapter(){
  81.                         public void keyPressed(KeyEvent e){
  82.                             if (e.getKeyCode() == KeyEvent.VK_NUMPAD1) {
  83.                                 //txtVisor.setText(txtVisor.getText() + "1");
  84.                                 jta.setText("1");
  85.                                 System.out.println(e.getKeyCode());
  86.                             }
  87.                         }
  88.                     });
  89.  
  90.         btn_7 = new JButton("7");
  91.         btn_8 = new JButton("8");
  92.         btn_9 = new JButton("9");
  93.         btn_div = new JButton("/");
  94.         btn_div.setForeground(Color.red);
  95.         btn_esb = new JButton("C");
  96.         btn_esb.setForeground(Color.blue);
  97.                
  98.         jp.add(btn_7);
  99.         jp.add(btn_8);
  100.         jp.add(btn_9);
  101.         jp.add(btn_div);
  102.         jp.add(btn_esb);
  103.        
  104.         btn_4 = new JButton("4");
  105.         btn_5 = new JButton("5");
  106.         btn_6 = new JButton("6");
  107.         btn_pro = new JButton("*");
  108.         btn_pro.setForeground(Color.red);
  109.         btn_per = new JButton("%");
  110.         btn_per.setForeground(Color.blue);
  111.        
  112.        
  113.        
  114.         jp.add(btn_4);
  115.         jp.add(btn_5);
  116.         jp.add(btn_6);
  117.         jp.add(btn_pro);
  118.         jp.add(btn_per);
  119.        
  120.         btn_1 = new JButton("1");
  121.         btn_2 = new JButton("2");
  122.         btn_3 = new JButton("3");
  123.         btn_sum = new JButton("+");
  124.         btn_sum.setForeground(Color.red);
  125.         btn_arr = new JButton("1/x");
  126.         btn_arr.setForeground(Color.blue);
  127.        
  128.        
  129.         jp.add(btn_1);
  130.         jp.add(btn_2);
  131.         jp.add(btn_3);
  132.         jp.add(btn_sum);
  133.         jp.add(btn_arr);
  134.        
  135.         btn_0 = new JButton("0");
  136.         btn_inv = new JButton("+/-");
  137.         btn_dec = new JButton(",");
  138.         btn_igu = new JButton("=");
  139.         btn_igu.setForeground(Color.red);
  140.         btn_res = new JButton("-");
  141.         btn_res.setForeground(Color.blue);
  142.        
  143.        
  144.        
  145.         jp.add(btn_0);
  146.         jp.add(btn_inv);
  147.         jp.add(btn_dec);
  148.         jp.add(btn_igu);
  149.         jp.add(btn_res);
  150.        
  151.        
  152.        
  153.         f.add(jta, BorderLayout.NORTH);
  154.         f.add(jp, BorderLayout.CENTER);
  155.         f.pack();
  156.         f.setVisible(true);
  157.        
  158.     }
  159.    
  160.     private void setCalcul(String tecla){
  161.         String digit = tecla;
  162.         String contingut = jta.getText();
  163.        
  164.         if(digit.charAt(0)==','){
  165.             if(contingut.length()==0){
  166.                 contingut = "0,";
  167.             }else{
  168.                 if(contingut.indexOf(',')==-1){        
  169.                     contingut = contingut.concat(tecla);
  170.                 }
  171.             }
  172.             jta.setText(contingut);
  173.             return;
  174.         }
  175.         if(digit.equals("+/-") == true){
  176.             if(contingut.equals("0") == false){
  177.                 if(contingut.length()>0){
  178.                     if(contingut.charAt(0) == '-'){
  179.                         contingut = contingut.substring(1);
  180.                     }else{
  181.                         contingut = "-".concat(contingut);
  182.                     }
  183.                    
  184.                 }
  185.             }
  186.             jta.setText(contingut);
  187.             return;
  188.         }
  189.        
  190.         if(digit.charAt(0) == 'C'){
  191.             contingut = "";
  192.             jta.setText(contingut);
  193.             return;
  194.         }
  195.        
  196.         if(contingut.equals("0")){
  197.             contingut = contingut.substring(1);
  198.             contingut = contingut.concat(tecla);
  199.         }else{
  200.             contingut = contingut.concat(tecla);
  201.         }
  202.        
  203.         jta.setText(contingut);
  204.     }
  205.    
  206.     class TractamentBotoNumeric implements ActionListener{
  207.  
  208.         public void actionPerformed(ActionEvent e){
  209.             setCalcul(e.getActionCommand());           
  210.         }
  211.     }
  212.    
  213.     /*class TractamentTeclat implements KeyAdapter{
  214.         public void keyPressed(KeyEvent e){
  215.             //setCalcul(e.getKeyText());
  216.             //String num = e.getKeyText();
  217.             jta.setText("Hola");
  218.         }
  219.     }*/
  220. }
  #4 (permalink)  
Antiguo 22/03/2012, 09:13
Avatar de luna690  
Fecha de Ingreso: marzo-2012
Ubicación: Barcelona
Mensajes: 61
Antigüedad: 12 años, 1 mes
Puntos: 3
Respuesta: Calculadora Java

Joanan, prova amb:

Código:
class TractarTeclat extends KeyAdapter
   /** En aquesta classe fem el tractament de totes les tecles*/
   {
      public void keyTyped (KeyEvent e)
      {
         //System.out.println ("Han premut la tecla " + e.getKeyChar());
         //permet veure quin caràcter s'ha premut
         char c = e.getKeyChar();
         
         if ((c>='0' && c<='9') || c==',' || c=='.')
         {
            TractarDigitComaPunt(c);
         }
         if (c=='+' || c=='-' || c=='*' || c=='/' || c=='=' || c=='%' || c=='c' || c=='C')
         {
            TractarOperador(c);
         }
      }
   }
  #5 (permalink)  
Antiguo 06/02/2013, 21:09
 
Fecha de Ingreso: febrero-2013
Mensajes: 2
Antigüedad: 11 años, 2 meses
Puntos: 0
Respuesta: Calculadora Java

Como aria para implementar el teclado en diferentes txt osea al hacer un requestFocus que escriba en ese txt y asi en los demas txt.??
  #6 (permalink)  
Antiguo 08/02/2013, 05:14
Avatar de jomaruro
Colaborador
 
Fecha de Ingreso: junio-2002
Ubicación: Naboo
Mensajes: 5.442
Antigüedad: 21 años, 10 meses
Puntos: 361
Respuesta: Calculadora Java

Hola:



Cita:
Iniciado por joanan46 Ver Mensaje
joder para que coño sirve el hightlight!!!! sino hace naaa
Ahora ya sabes lo que hace, si te fijas en el primer mensaje te falta T]

Cita:
[HIGHLIGHpackage momentTemporal.Calculadora;
Saludos.


Etiquetas: calculadora, programa, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:47.