Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    40

    [Java] Effetto trillo su JFrame

    Ragazzi ho un JFrame, come posso fare in modo che esso reagisca allo stesso modo di quando una finestra di MSN riceve un trillo?
    Grazie a chi risponderà

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Prova questo che ho appena scritto e poi dimmi se ti piace.
    È pure facilmente riutilizzabile, in quanto ho definito una classe JRingerFrame che basta poi estendere per avere "di serie" il metodo ring().


    codice:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TestFrame extends JRingerFrame
    {
        public TestFrame ()
        {
            super ("Ring Frame");
    
            setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            setSize (300, 300);
    
            JButton button = new JButton ("RING!!");
            add (button);
    
            button.addActionListener (new ActionListener ()
            {
                public void actionPerformed (ActionEvent e)
                {
                    ring (2.0f);      // circa 2 secondi
                }
            });
    
        }
    
        public static void main (String[] args)
        {
            SwingUtilities.invokeLater (new Runnable ()
            {
                public void run ()
                {
                    TestFrame f = new TestFrame ();
                    f.setVisible (true);
                }
            });
        }
    }
    
    
    class JRingerFrame extends JFrame
    {
        private Timer timer;
        private int count;
        private int maxCount;
        private int offsetX;
        private int offsetY;
    
        public JRingerFrame (String title)
        {
            super (title);
            timer = new Timer (50, new RingTimerActionListener ());
        }
    
        public void ring (float seconds)
        {
            if (!timer.isRunning ())
            {
                maxCount = (int) (seconds * 20);
                count = 0;
                timer.start ();
            }
        }
    
        private class RingTimerActionListener implements ActionListener
        {
            public void actionPerformed (ActionEvent e)
            {
                Point p = getLocation ();
    
                if (count > 0)
                {
                    p.x -= offsetX;
                    p.y -= offsetY;
                }
    
                if (count++ < maxCount)
                {
                    offsetX = (int) (Math.random () * 9) - 4;   // range -4 .. +4
                    offsetY = (int) (Math.random () * 9) - 4;   // range -4 .. +4
    
                    p.x += offsetX;
                    p.y += offsetY;
                }
                else
                    timer.stop ();
    
                setLocation (p);
            }
        }
    }
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    40
    L'ho provato: veramente notevole! Ti ringrazio

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.