2

GlassPaneを使用してアイコンをユーザーにフラッシュしようとしています。私はjavax.swing.Timer基本的にこれを実行するを実行しています:

for (int i = 0; i < 3; i++) {
    frame.getGlassPane().setVisible(true);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e1) {
       //To change body of catch statement use File | Settings | File Templates.
        e1.printStackTrace();
    }
    frame.getGlassPane().setVisible(false);
}

残念ながら、EDT(タイマー内の現在のスレッド)paintComponentをスリープすると、スレッドがスリープする前にメソッドが完全に呼び出されなかったため、アイコンが表示されません。したがって、次の命令が開始されると、ガラスペインが非表示になり、その結果、アイコンが表示されることはありません。この(同様の)アプローチを使用して、私が望むことを達成する方法はありますか?

4

2 に答える 2

5

あなたは使用することができますjavax.swing.Timer

public FlashTimer() {

    javax.swing.Timer flashTimer = new javax.swing.Timer(500, new FlashHandler());
    flashTimer.setCoalesce(true);
    flashTimer.setRepeats(true);
    flashTimer.setInitialDelay(0);

}

public class FlashHandler implements ActionListener {

    private int counter;

    @Override
    public void actionPerformed(ActionEvent ae) {

        countrol.setVisible(counter % 2 == 0);
        counter++;
        if (counter > 3) {

            ((Timer)ae.getSource()).stop();

        }

    }

}
于 2012-08-15T10:09:41.663 に答える
3

明らかなはずです。別のスレッドを使用し、そこで「点滅ロジック」を実行しますが、EDTのUIを変更します。これは簡単な例です(アイデアを理解するには十分なはずです):

public static void main ( String[] args )
{
    JFrame frame = new JFrame ();

    final JLabel label = new JLabel ( "X" );
    label.setBorder ( BorderFactory.createEmptyBorder ( 90, 90, 90, 90 ) );
    frame.add ( label );

    frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );

    new Thread ( new Runnable ()
    {
        public void run ()
        {
            for ( int i = 0; i < 15; i++ )
            {
                try
                {
                    setVisible ( false );
                    Thread.sleep ( 500 );
                    setVisible ( true );
                    Thread.sleep ( 500 );
                }
                catch ( InterruptedException e1 )
                {
                    //
                }
            }
        }

        private void setVisible ( final boolean visible )
        {
            SwingUtilities.invokeLater ( new Runnable ()
            {
                public void run ()
                {
                    label.setVisible ( visible );
                }
            } );
        }
    } ).start ();
}
于 2012-08-15T09:30:32.593 に答える