Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [Java] Chiudere JFrame con tasto ESC

    sto cercando di impostare la chiusura di un jframe col tasto esc.
    codice:
        private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
            if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                System.out.println("CIAO");
                this.dispose();
            }
        }
    nn succede nulla.
    cosa mi manca??

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2009
    Messaggi
    1,123
    Non so di preciso che hai fatto... quindi ho fatto prima a scrivere tutto.

    codice:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    class EscFrame extends JFrame {
      EscFrame() {
        
        addKeyListener(new KeyAdapter() {
          public void keyPressed(KeyEvent ke) {
            System.out.println(ke.getKeyCode());
            if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
              dispose();
            }
          }
        });
      }
     
    
    
      public static void makeGUI() {
        EscFrame ef = new EscFrame();
        ef.setSize(500,500);
        ef.setVisible(true);
      }
     
      public static void main(String[] args) {
        try {
          SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
              makeGUI();
            }
          });
        } catch(Exception e) {}
      }
    }

  3. #3
    questo jframe viene richiamato da un altro jframe e serve per modificare dei dati in un db.
    ho impostato questo evento:
    codice:
    public class FormModifica extends javax.swing.JFrame {
        public FormModifica() { // COSTRUTTORE
            addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    formKeyPressed(evt);
                }
            });
    .............
         }
        private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
            if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                System.out.println("CIAO");
                dispose();
            }
        }
    .........
    ho provato sia con dispose() che con this.dispose e nn succede nulla.
    infatti nn mi sembra di aver fatto sbagliato.

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da fermat
    ho provato sia con dispose() che con this.dispose e nn succede nulla.
    C'è una questione "subdola". Se il frame è vuoto (senza componenti), un ESC va al frame e il KeyListener registrato sul frame farebbe il suo dovere.
    Ma se il frame contiene componenti e il "focus" è ad esempio su un text field ... l'ESC va al text field!

    Quindi la questione è: vuoi che l'ESC sia gestito solo ed esclusivamente per il dispose() o accetti che un componente possa magari trattarlo in qualche modo?
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    bhe a questo nn avevo pensato in verità.
    di primo acchitto direi che mi serve solo per gestire il dispose ed ho risolto con:
    this.requestFocsu();

    ma se volessi gestire la seconda opzione?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.