Java实现简单图片浏览

百度网盘链接:https://pan.baidu.com/s/1KfpjSDVHVv_oxTBP6d-c9Q 
提取码:nen4

想法:弄一个登录界面,登录进去后可以选择查看图片

实现步骤:

1、主函数Main类

package 图片展示;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Login login = new Login();

	}

}

2、login类

package 图片展示;

import java.awt.Color;
import java.awt.Image;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame implements ActionListener{
	JTextField fieldAccount = new JTextField(10);
	JPasswordField fieldPassword = new JPasswordField(10);
	public Login() {
		this.setTitle("Login");
		this.setBounds(200, 200, 440, 340);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setVisible(true);		
		this.setIconImage(new ImageIcon("./photo/background.jpg").getImage());
		
		this.setLayout(null);
		
		
		JLabel label = new JLabel("   帐  号   ");
		label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
		label.setBounds(220, 100, 100, 50);
		this.add(label );
		fieldAccount.setBounds(270, 110, 120, 25);
		this.add(fieldAccount);
		
		label = new JLabel("   密 码   ");
		label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
		label.setBounds(220, 150, 100, 50);
		this.add(label);
		
		fieldPassword.setBounds(270, 160, 120, 25);
		this.add(fieldPassword);
		
		JButton jbutton = new JButton("登录");
		jbutton.setBounds(270, 200, 60, 30);
		Color color1 = new Color(80,120,40);
		jbutton.setForeground(color1);
		this.add(jbutton);
		
		jbutton.addActionListener(this);
		fieldPassword.addActionListener(this);
		
		/*改变窗口背景*/
		ImageIcon backbround = new ImageIcon("./photo/background.jpg");
		Image image = backbround.getImage(); 
		Image smallImage = image.getScaledInstance(500, 500, Image.SCALE_FAST);
		ImageIcon backbrounds = new ImageIcon(smallImage);
		
		//将图片添加到JLable标签 
		JLabel jlabel = new JLabel(backbrounds);
		//设置标签的大小
		jlabel.setBounds(0,0, getWidth(),getHeight() );
		//将图片添加到窗口
		add(jlabel);
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String account1 = fieldAccount.getText();
		String pwd1 = new String(fieldPassword.getPassword());
		if (account1.compareTo("123") == 0 && pwd1.compareTo("123") == 0){
			JOptionPane.showMessageDialog(null, "登录成功,Welcome","Login",1);
			
			dispose();
			Home home = new Home();
						
		}
		else JOptionPane.showMessageDialog(null, "登录失败,请重试","Login",1);
	}

}
	

3、Home类

 

package 图片展示;

import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Home extends JFrame implements ActionListener{
	public Home() {
		this.setTitle("浏览界面");
		this.setBounds(100, 100, 450, 300);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setVisible(true);
		
		this.setIconImage(new ImageIcon("./photo/background.jpg").getImage());
		
		FlowLayout layout = new FlowLayout();
		this.setLayout(layout);
		
		JButton button1 = new JButton("第一张");
		ImageIcon imageIcon1 = new ImageIcon("./photo/photo1.jpg"); 
		Image image1 = imageIcon1.getImage(); 
		Image smallImage1 = image1.getScaledInstance(60, 50, Image.SCALE_FAST);
		ImageIcon smallIcon1 = new ImageIcon(smallImage1);
		button1.setIcon(smallIcon1);
		this.add(button1);
		
		button1.addActionListener(this);
		button1.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				Photo1 photo1 = new Photo1();
			}
		});
								
		JButton button2 = new JButton("第二张");
		ImageIcon imageIcon2 = new ImageIcon("./photo/photo2.jpg"); 
		Image image2 = imageIcon2.getImage(); 
		Image smallImage2 = image2.getScaledInstance(60, 50, Image.SCALE_FAST);
		ImageIcon smallIcon2 = new ImageIcon(smallImage2);
		button2.setIcon(smallIcon2);
		this.add(button2);
		
		button2.addActionListener(this);
		button2.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				Photo2 photo2 = new Photo2();
			}
		});
		
		JButton button3 = new JButton("第三张");
		ImageIcon imageIcon3 = new ImageIcon("./photo/photo3.jpg"); 
		Image image3 = imageIcon3.getImage(); 
		Image smallImage3 = image3.getScaledInstance(60, 50, Image.SCALE_FAST);
		ImageIcon smallIcon3 = new ImageIcon(smallImage3);
		button3.setIcon(smallIcon3);
		this.add(button3);
		
		button3.addActionListener(this);
		button3.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				Photo3 photo3 = new Photo3();
			}
		});
		
		JButton jb4 = new JButton("首张");
		this.add(jb4);
		jb4.addActionListener(this);
		jb4.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				Photo1 photo1 = new Photo1();
				
			}
		});
		
		JButton jb5 = new JButton("末张");
		this.add(jb5);
		jb5.addActionListener(this);
		jb5.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				Photo3 photo3 = new Photo3();
				
			}
		});
		
		
		
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

}

4、photo1类

 

package 图片展示;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Photo1 extends JFrame implements ActionListener{
	public Photo1() {
		this.setTitle("Photo1");
		this.setBounds(100, 100, 800, 600);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setIconImage(new ImageIcon("./photo/background.jpg").getImage());
		
		this.setVisible(true);
		
		FlowLayout layout = new FlowLayout();
		this.setLayout(layout);
		
		ImageIcon image1 = new ImageIcon("./photo/photo1.jpg");
		JLabel label = new JLabel(image1);		
		this.add(label);
		
		JButton button1 = new JButton("信息");
		this.add(button1);
		button1.addActionListener(this);
		button1.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showMessageDialog(null, "  鸣人","photo1信息",-1);
				
			}
		});
		
		JButton button2 = new JButton("下一张");
		this.add(button2);
		button2.addActionListener(this);
		button2.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				dispose();
				Photo2 photo2 = new Photo2();				
			}
		});
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
	}

}

5、photo2类

package 图片展示;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Photo2 extends JFrame implements ActionListener{
	public Photo2() {
		this.setTitle("Photo2");
		this.setBounds(100, 50, 1000, 750);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setIconImage(new ImageIcon("./photo/photo2.jpg").getImage());
		this.setVisible(true);
		
		FlowLayout layout = new FlowLayout();
		this.setLayout(layout);
		
		
		ImageIcon image1 = new ImageIcon("./photo/photo2.jpg");
		JLabel label = new JLabel(image1);
		this.add(label);

		JButton button1 = new JButton("信息");
		this.add(button1);
		button1.addActionListener(this);
		button1.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				Massage1 massage1 = new Massage1();
				
			}
		});
		
		
		JButton button2 = new JButton("上一张");
		this.add(button2);
		button2.addActionListener(this);
		button2.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				
				dispose();
				Photo1 photo1 = new Photo1();
				
			}
		});
		
		JButton button3 = new JButton("下一张");
		this.add(button3);
		button3.addActionListener(this);
		button3.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				dispose();
				Photo3 photo3 = new Photo3();				
			}
		});
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
	}

}

 6、photo3类

package 图片展示;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Photo3 extends JFrame implements ActionListener{
	public Photo3() {
		this.setTitle("Photo3");
		this.setBounds(100, 100, 800, 600);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setIconImage(new ImageIcon("./photo/photo3.jpg").getImage());
		this.setVisible(true);
		
		FlowLayout layout = new FlowLayout();
		this.setLayout(layout);
		
		ImageIcon image1 = new ImageIcon("./photo/photo3.jpg");
		JLabel label = new JLabel(image1);
		this.add(label);
		
		JButton button1 = new JButton("信息");
		this.add(button1);
		button1.addActionListener(this);
		button1.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showMessageDialog(null, "  佐助","photo3信息",-1);
				
			}
		});
		
		
		JButton jbutton2 = new JButton("上一张");
		this.add(jbutton2);
		jbutton2.addActionListener(this);
		jbutton2.addActionListener(new ActionListener()		
		{
			public void actionPerformed(ActionEvent e)
			{
				dispose();
				Photo2 photo2 = new Photo2();				
			}
		});
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

}

7、Massage类

package 图片展示;

import java.awt.Color;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class Massage1 extends JFrame{
	public Massage1() {
		this.setTitle("Massage");
		this.setBounds(200, 50, 600, 400);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setIconImage(new ImageIcon("./photo/photo2.jpg").getImage());
		this.setVisible(true);
		
		JTextArea  Myarea=new  JTextArea(3,10);
		Myarea.setText( " \n " 
                +"信息:\n"
                + "hello\n"
                +"你好\n"
                +"哈哈哈\n"
                + "哈哈哈哈哈哈\n" 
                );		
		   this.setLayout(null);              
           Myarea.setBounds( 10,10,600,350);             			 
           Myarea.setFont(new Font("华文正楷",Font.BOLD,25));
           Color color = new Color(0,110,0);
           Myarea.setForeground(color);
           Myarea.setEditable(false);
           this.add(Myarea);
		
		
	}

}

这些操作用到的知识,例如界面跳转、弹框、图片显示等前面都有发过单一的。 

  • 7
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现一个简单图片浏览器,可以使用Java Swing图形用户界面组件库。以下是一个简单实现步骤: 1. 创建一个JFrame窗口并设置标题和大小。 2. 创建一个JPanel面板,并将其添加到JFrame窗口中。 3. 创建一个JLabel标签,并将其添加到面板中。 4. 使用JFileChooser组件选择要浏览图片,并将其读入内存中。 5. 将图片设置为JLabel的图像,并将其显示在面板上。 6. 添加一个JButton按钮,用于在图片库中选择下一张图片。 以下是一个示例代码: ```java import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class ImageBrowser extends JFrame { private static final long serialVersionUID = 1L; private JLabel imageLabel; private JFileChooser fileChooser; private File currentImageFile; private File[] imageFiles; private int currentIndex; public ImageBrowser() { super("Image Browser"); setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(); add(panel, BorderLayout.CENTER); imageLabel = new JLabel(); panel.add(imageLabel); fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setMultiSelectionEnabled(true); JButton nextButton = new JButton("Next"); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (imageFiles != null && currentIndex < imageFiles.length - 1) { currentIndex++; loadImage(); } } }); panel.add(nextButton); JButton chooseButton = new JButton("Choose"); chooseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int result = fileChooser.showOpenDialog(ImageBrowser.this); if (result == JFileChooser.APPROVE_OPTION) { imageFiles = fileChooser.getSelectedFiles(); currentIndex = 0; loadImage(); } } }); panel.add(chooseButton); } private void loadImage() { currentImageFile = imageFiles[currentIndex]; try { imageLabel.setIcon(ImageIO.read(currentImageFile)); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { ImageBrowser browser = new ImageBrowser(); browser.setVisible(true); } } ``` 在这个示例中,我们使用JFileChooser组件选择要浏览图片文件,然后使用ImageIO.read()方法将其读入内存中,并将其设置为JLabel的图像。我们还添加了一个“Next”按钮,用于在图片库中选择下一张图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值