2016-11-30 12 views
-1

MouseListener.mouseClickedメソッドでコードを実行しているときにgifイメージを実行する必要があります。しかし、gif画像を実行しているmouseClickedメソッドが静的な場合。JPanelがmouseClickedメソッドで再描画しない

例アニメーションGIF:のmouseClickedが終了した後

gif image

アニメーションが開始されます。 GIFのために私は

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.plaf.LayerUI; 

public class Test { 
    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new Test().createUI(); 
      } 
     }); 
    } 

    private JButton mOrderButton; 

    public void createUI() { 
     JFrame f = new JFrame(); 

     final WaitLayerUI layerUI = new WaitLayerUI(f); 
     JPanel panel = createPanel(); 
     JLayer<JPanel> jlayer = new JLayer<JPanel>(panel, layerUI); 

     mOrderButton.addMouseListener(new MouseListener() { 
      public void mouseClicked(MouseEvent e) { 
       layerUI.start(); 
       // response imitation from server 
       try { 
        Thread.sleep(3000); 
       } catch (InterruptedException e1) { 
        e1.printStackTrace(); 
       } 
       layerUI.stop(); 
      } 

      public void mousePressed(MouseEvent e) {} 
      public void mouseReleased(MouseEvent e) {} 
      public void mouseEntered(MouseEvent e) {} 
      public void mouseExited(MouseEvent e) {} 
     }); 

     f.add (jlayer); 
     f.setSize(500, 500); 
     f.setLocationRelativeTo(null); 
     f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     f.setVisible (true); 
    } 

    private JPanel createPanel() { 
     JPanel p = new JPanel(); 
     mOrderButton = new JButton("btn"); 
     p.add(mOrderButton); 
     return p; 
    } 
} 

class WaitLayerUI extends LayerUI<JPanel> { 
    private static final int IMAGE_WIDTH = 120; 
    private static final int IMAGE_HEIGHT = 105; 
    private static final String IMAGE_PATH = "/busy_indicator_login_window.gif"; 
    private final Image busyIndicatorImage; 
    private final JFrame loginWindowFrame; 
    private boolean isRunning; 

    public WaitLayerUI(JFrame loginWindowFrame) { 
     busyIndicatorImage = new ImageIcon(this.getClass().getResource(IMAGE_PATH)).getImage(); 
     this.loginWindowFrame = loginWindowFrame; 
    } 

    @Override 
    public void paint(Graphics g, JComponent c) { 
     super.paint(g, c); 

     if (!isRunning) { 
      return; 
     } 

     Graphics2D g2 = (Graphics2D) g.create(); 
     g2.setColor(new Color(0, 0, 0, 0.1f)); 
     g2.fillRect(0,0,500, 500); 

     g2.drawImage(busyIndicatorImage, 
       loginWindowFrame.getWidth()/2 - IMAGE_WIDTH/2, 
       loginWindowFrame.getHeight()/2 - IMAGE_HEIGHT/2, 
       IMAGE_WIDTH, IMAGE_HEIGHT, loginWindowFrame); 

     g2.dispose(); 
    } 

    public void start() { 
     isRunning = true; 
    } 

    public void stop() { 
     isRunning = false; 
    } 
} 
+0

LayerUI<JPanel>あなたは()のmousePressedへのmouseClicked()内のコードを移動しようとしたことがありでしょうか? mouseClicked()はmousePressed()イベントとmouseReleased()イベントの両方です。 –

+1

コードがここでコンパイルされない理由はたくさんあるようです:1)すぐに役立つようにするには、[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/) 。 2)画像を取得する方法の1つは、[このQ&A](http://stackoverflow.com/q/19209650/418556)に表示されている画像にホットリンクすることです。 –

答えて

0
public void mouseClicked(MouseEvent e) { 
    new Thread(new Runnable() { 
     layerUI.start(); 
     // response imitation from server 
     try { 
      Thread.sleep(3000); 
     } catch (InterruptedException e1) { 
      e1.printStackTrace(); 
     } 
     layerUI.stop(); 
    }).start(); 
} 
関連する問題