|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Feb 2007
Città: Verona
Messaggi: 1060
|
[Java] Problema KeyEvent(Listener), non si attiva!
Partiamo dal codice:
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."); } } } 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! ![]()
__________________
|
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Tre thread di questo Forum sull'uso del KeyListener
![]() [JAVA]Come usare un KeyListener??? [JAVA] assegnare un tasto della tastiera a un JButton [Java] Problema con label Non può mancare la fonte per eccellenza, la documentazione della Sun: How to Write a Key Listener
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 20-03-2009 alle 09:40. |
![]() |
![]() |
![]() |
#3 | |
Senior Member
Iscritto dal: Feb 2007
Città: Verona
Messaggi: 1060
|
Quote:
![]()
__________________
|
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 11:19.