Animations when using Gridbag Layout.

前端 未结 2 1492
南旧
南旧 2020-11-29 11:40

I have recently started Java and wondered if it was possible to make Animations whilst using GridBag Layout.

Are these possible and how? Any tutorials, help and suc

2条回答
  •  悲&欢浪女
    2020-11-29 12:26

    This is the program which i did long time back when i just started my Java classes.I did simple animations on different tabs on JTabbedPane (change the path of images/sound file as required),hope this helps:

    UPDATE:

    Sorry about not following concurrency in Swing.

    Here is updated answer:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    public class ClassTestHello extends JApplet {
      private static JPanel j1;
      private JLabel jl;
      private JPanel j2;
      private Timer timer;
      private int i = 0;
      private int[] a = new int[10];
    
      @Override
      public void init() {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            start();
            paint();
          }
        });
      }
    
      public void paint() {
        jl = new JLabel("hiii");
        j1.add(jl);
    
        a[0] = 1000;
        a[1] = 800;
        a[2] = 900;
        a[3] = 2000;
        a[4] = 500;
    
        ActionListener actionListener = new ActionListener() {
          public void actionPerformed(ActionEvent actionEvent) {
            if(i % 2 == 0)
              jl.setText("hiii");
            else
              jl.setText("byee");
    
            i++;
            if(i > 4)
              i=0;
            timer.setDelay(a[i]);
          }
        };
    
        timer = new Timer(a[i], actionListener);
        timer.setInitialDelay(0);
        timer.start();
      }
    
      @Override
      public void start() {
        j1 = new JPanel();
        j2 = new JPanel();
    
        JTabbedPane jt1 = new JTabbedPane();
    
        ImageIcon ic = new ImageIcon("e:/guitar.gif");
        JLabel jLabel3 = new JLabel(ic);
        j2.add(jLabel3);
    
        jt1.add("one", j1);
        jt1.addTab("hii", j2);
    
        getContentPane().add(jt1);    
      } 
    }
    

提交回复
热议问题