PDA

View Full Version : [Java] Problema KeyEvent(Listener), non si attiva!


malocchio
19-03-2009, 23:49
Partiamo dal codice:
package arithmeticExpression.calc;

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


/**
* @author malocchio
*/
public class CalcFrame extends JFrame {

private CalcApp app;
private JPanel top, bottom;
private String[][] labels;
private JButton[][] buttons;
private JButton clear;
private JTextField display;
private CalcListener listener;

public CalcFrame(CalcApp app) {
if (app != null)
this.app = app;
else
throw new NullPointerException("Oggetto applicazione specificato null");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.listener = new CalcListener();
this.addKeyListener( listener );
this.top = new JPanel(); this.bottom = new JPanel();
this.setLayout(new BorderLayout());
this.add(top, BorderLayout.NORTH);
this.add(bottom, BorderLayout.CENTER);
this.labels = new String[][]
{{"7", "8", "9", "/"},
{"4", "5", "6", "*"},
{"1", "2", "3", "-"},
{"0", "+/-", "=", "+"}};
this.buttons = new JButton[labels.length][labels[0].length];
this.bottom.setLayout( new GridLayout(labels.length, labels[0].length, 3, 3) );
Font bfont = new Font("Verdana", Font.PLAIN, 16);
for (int i = 0; i < labels.length; i++) {
for (int j = 0; j < labels[i].length; j++) {
this.buttons[i][j] = new JButton( this.labels[i][j] );
this.buttons[i][j].setFont(bfont);
this.buttons[i][j].addActionListener( this.listener );
this.bottom.add(buttons[i][j]);
}
}
this.top.setLayout( new GridLayout(2,1) );
this.display = new JTextField();
this.display.setEditable(false);
this.display.setBackground(Color.WHITE);
this.display.setFont( new Font("Verdana", Font.PLAIN, 24) );
this.display.setHorizontalAlignment(JTextField.TRAILING);
this.display.setText( "Testo di prova 0123456789" );
this.top.add(this.display, BorderLayout.CENTER);
this.clear = new JButton("C");
this.clear.setFont(bfont);
this.clear.addActionListener( this.listener );
this.top.add( this.clear );

}

private class CalcListener implements ActionListener, KeyListener {

public void actionPerformed(ActionEvent e) {
CalcFrame.this.app.commitAction(e.getActionCommand());
}

public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void keyPressed(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}

}

}
In rosso le parti più interessanti (a mio avviso). Tralasciando che i metodi key***ed lanciano solo eccezioni, quando lo avvio questi non vengono richiamati alla pressione di un tasto. Punto. Niente di niente.
Faccio notare che ho aggiunto il keylistener direttamente al JFrame, che secondo la mia logica dovrebbe così gestire tutti gli eventi di tastiera che accadono quando il focus è sulla mia finestra. Mi viene il dubbio che usando i JPanel per settare i layout vada a farsi friggere qualcosa...
All'interno della stessa finestra viene usato sempre l'oggetto listener (CalcListener) come ascoltatore sia di tastiera sia di click sui pulsanti. Ho provato una volta a disattivarli per vedere se era un conflitto con questi ma niente è cambiato!

L'applicazione è una semplice calcolatrice. Compilare il sorgente sarebbe inutile perché manca il resto delle classi del progetto di cui fa parte questa Gui. In pratica volevo fare in modo che premendo una cifra sulla tastiera fosse equivalente che usare i Buttons, esattamente come la calc di windows o Gnome.

Grazie a chi mi vorrà aiutare! :mano:

banryu79
20-03-2009, 09:34
Tre thread di questo Forum sull'uso del KeyListener ;)
[JAVA]Come usare un KeyListener??? (http://www.hwupgrade.it/forum/showthread.php?t=1774047&highlight=KeyListener)
[JAVA] assegnare un tasto della tastiera a un JButton (http://www.hwupgrade.it/forum/showthread.php?t=1638278&highlight=KeyListener)
[Java] Problema con label (http://www.hwupgrade.it/forum/showthread.php?t=1483775&highlight=KeyListener)

Non può mancare la fonte per eccellenza, la documentazione della Sun:
How to Write a Key Listener (http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html)

malocchio
20-03-2009, 21:39
Tre thread di questo Forum sull'uso del KeyListener ;)
[JAVA]Come usare un KeyListener??? (http://www.hwupgrade.it/forum/showthread.php?t=1774047&highlight=KeyListener)
[JAVA] assegnare un tasto della tastiera a un JButton (http://www.hwupgrade.it/forum/showthread.php?t=1638278&highlight=KeyListener)
[Java] Problema con label (http://www.hwupgrade.it/forum/showthread.php?t=1483775&highlight=KeyListener)

Non può mancare la fonte per eccellenza, la documentazione della Sun:
How to Write a Key Listener (http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html)

Ti ringrazio e chiedo perdono, non ho fatto una ricerca! :ave: