import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class browser extends JFrame
{
public browser()
{
setTitle("图片浏览器");
getContentPane().add(new DrawPanel());
}
public static void main(String[] args)
{
browser jf=new browser();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500,300);
jf.setVisible(true);
}
}
class DrawPanel extends JPanel implements ActionListener
{
int totle=10;
Image[] image;
JButton nextButton=new JButton("next");
int current=0;
public DrawPanel()
{
image=new Image[totle];
for(int i=0;i<=9;i++)
{
try
{
image[i]=ImageIO.read(new File("./T"+(i+1)+".gif"));
}
catch(IOException e)
{
e.printStackTrace();
}
}
add(nextButton);
nextButton.addActionListener(this);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image[current],10,50,Color.CYAN,null);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==nextButton)
{
current=(++current)%10;
repaint();
}
}
}
java简易图片浏览器代码
最新推荐文章于 2021-02-18 16:16:51 发布
本文介绍了一个使用Java实现的图片浏览器应用,详细解释了如何加载、显示和切换图片,并通过按钮实现图片的前后翻页功能。
3007

被折叠的 条评论
为什么被折叠?



