百度网盘链接: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);
}
}
这些操作用到的知识,例如界面跳转、弹框、图片显示等前面都有发过单一的。