java图片浏览器(娱乐)

package org.tarena.day02;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class TestCardLayout implements ActionListener {

    JPanel jp1;

    CardLayout c;

    Timer time = new Timer(1000, this);

    public void getJModel() {
        JFrame jf = new JFrame("图片浏览器");
        c = new CardLayout();
        jp1 = new JPanel(c);
        JPanel jp2 = new JPanel(new FlowLayout());

        String[] name = { "1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg" };// 图片名称
        for (int i = 0; i < name.length; i++) {
            Icon ic = new ImageIcon("//home//soft22//桌面图片//" + name[i]);
            JLabel jl = new JLabel(ic);
            jp1.add(jl, i + "");// 一定要加名字
        }
        jf.add(jp1);

        String[] ope = { "first", "next", "previous", "last", "start", "stop"};
        for (int i = 0; i < ope.length; i++) {
            JButton jb = new JButton(ope[i]);
            jb.addActionListener(this);// 注册监听;
            jp2.add(jb);
        }

        jf.add(jp2, BorderLayout.SOUTH);
        jf.setSize(450, 500);
        jf.setLocation(450, 500);
        jf.setVisible(true);
        jf.setResizable(false);
        jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        jf.addWindowListener(new WindowListener() {//匿名内部类

            public void windowActivated(WindowEvent e) {
            }

            public void windowClosed(WindowEvent e) {
            }

            public void windowClosing(WindowEvent e) {
                int i = JOptionPane.showConfirmDialog(null, "是否真的要退出?",
                        "图片浏览器", JOptionPane.YES_NO_CANCEL_OPTION);
                if (i == 0) {
                    System.exit(0);
                } else if (i == 1) {
                    //
                } else if (i == 2) {
                    //
                }
            }

            public void windowDeactivated(WindowEvent e) {
            }

            public void windowDeiconified(WindowEvent e) {
            }

            public void windowIconified(WindowEvent e) {
            }

            public void windowOpened(WindowEvent e) {
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        String comm = e.getActionCommand();
        if ("first".equals(comm)) {
            c.first(jp1);
        } else if ("next".equals(comm)) {
            c.next(jp1);
        } else if ("previous".equals(comm)) {
            c.previous(jp1);
        } else if ("last".equals(comm)) {
            c.last(jp1);
        } else if ("start".equals(comm)) {
            time.start();
        } else if ("stop".equals(comm)) {
            time.stop();
        } else {//Timer分支
            c.next(jp1);
        }
    }

    public static void main(String[] args) {
        TestCardLayout t = new TestCardLayout();
        t.getJModel();

    }

}

你可能感兴趣的:(java,C++,c,浏览器,swing)