sinat_36832192
sinat_36832192
2017-08-22 08:00

JAVA JPanel轮播图无法实现

1
  • 界面设计
  • java
  • 轮播图
 package ui;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Demo4 extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    MyJPanel mp;
    int index;
    ImageIcon[] imgs = {
            new ImageIcon("src/images/1.jpg"),
            new ImageIcon("src/images/2.jpg"),
            new ImageIcon("src/images/3.jpg"),
            new ImageIcon("src/images/4.jpg"),
            new ImageIcon("src/images/5.jpg"),
            new ImageIcon("src/images/6.jpg"),
            new ImageIcon("src/images/7.jpg"),
            new ImageIcon("src/images/8.jpg"),
        };
    public Demo4() {
        mp = new MyJPanel();
        mp.setOpaque(false);
        this.add(mp);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setTitle("窗口");
        this.setVisible(true);

        Timer timer = new Timer(500,new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mp.repaint();
            }
        });
        timer.start();
    }
    public static void main(String[] args) {
        new Demo4();
    }
    class MyJPanel extends JPanel{
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.drawImage(imgs[index%imgs.length].getImage(), 0, 0,this);
            index++;
        }
    }
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐