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

    perchè la mia applicazione non legge il mouseDown

    ragazzi ho fatto un codice del genere per captare il mouseDown sulla mia finestra:
    codice:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * NumberAdditionUI.java
     *
     * Created on 29-gen-2009, 16.39.39
     */
    
    package my.numeraddition;
    import javax.swing.*;
    import java.awt.event.MouseEvent;
    /**
     *
     * @author corradocacciapuoti
     */
    public class NumberAdditionUI extends javax.swing.JFrame {
    
        /** Creates new form NumberAdditionUI */
        public NumberAdditionUI() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jTabbedPane1 = new javax.swing.JTabbedPane();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jButton1.setText("Add");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(141, 141, 141)
                    .add(jButton1)
                    .addContainerGap(151, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(102, 102, 102)
                    .add(jButton1)
                    .addContainerGap(130, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
    
    public void mouseDown(MouseEvent e) {
                 JOptionPane.showMessageDialog(null, "Mouse Down");
    
    }
       
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NumberAdditionUI().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JTabbedPane jTabbedPane1;
        // End of variables declaration                   
    
    }
    ma quando faccio il click sulla finestra non accade nulla

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: perchè la mia applicazione non legge il mouseDown

    Originariamente inviato da Realscorpion
    ragazzi ho fatto un codice del genere per captare il mouseDown sulla mia finestra:

    ma quando faccio il click sulla finestra non accade nulla
    Non si usa mouseDown(). Implementa un MouseListener, registralo sul JFrame e nel mousePressed() fai quello che vuoi.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    ok però vuole tutti i metodi se no mi da questo errore:

    my.numberaddition.NumberAdditionUI is not abstact method and does not override abstract method MousePressed(java.awt.event.MouseEvent) in java.awt.event.MouseListener
    inoltre ho messo tutti i metodi ma non cambia niente...

    codice:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * NumberAdditionUI.java
     *
     * Created on 29-gen-2009, 16.39.39
     */
    
    package my.numeraddition;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    /**
     *
     * @author corradocacciapuoti
     */
    public class NumberAdditionUI extends javax.swing.JFrame implements MouseListener{
    
        /** Creates new form NumberAdditionUI */
        public NumberAdditionUI() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jTabbedPane1 = new javax.swing.JTabbedPane();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jButton1.setText("Add");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(141, 141, 141)
                    .add(jButton1)
                    .addContainerGap(151, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(102, 102, 102)
                    .add(jButton1)
                    .addContainerGap(130, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>     
                       
    public void mousePressed(MouseEvent e) {
                 JOptionPane.showMessageDialog(null, "Funziona ");
    
    }
        public void mouseReleased(MouseEvent e) {
                 JOptionPane.showMessageDialog(null, "Funziona ");
    
    }
        public void mouseEntered(MouseEvent e) {
                 JOptionPane.showMessageDialog(null, "Funziona ");
    
    }
        public void mouseExited(MouseEvent e) {
                 JOptionPane.showMessageDialog(null, "Funziona");
    
    }
    
    public void mouseClicked(MouseEvent e) {
                 JOptionPane.showMessageDialog(null, "Funziona");
    
    }                                   
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NumberAdditionUI().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JTabbedPane jTabbedPane1;
        // End of variables declaration                   
    
    }
    non legge niente ne il pressed ne il click, eccetera..

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da Realscorpion
    ok però vuole tutti i metodi se no mi da questo errore:
    In generale, per gli eventi, esistono le interfacce XYZListener e le classi XYZAdapter. Quest'ultime classi implementano già il corrispondente XYZListener definendo tutti i metodi con corpo "vuoto", quindi puoi fare l'override solo di quelli necessari.

    La tua classe principale non può estendere MouseAdapter perché già estende JFrame. Potresti però implementare il listener in una inner-class.

    Originariamente inviato da Realscorpion
    inoltre ho messo tutti i metodi ma non cambia niente...
    Manca la registrazione!!! Non è che mettendo quei metodi, per "magia" vengono invocati. Devi registrare il listener.

    Nel initComponents():

    addMouseListener(this);
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

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.