java图片浏览器(娱乐)

本文详细介绍了如何使用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();

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值