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!!!