使用图形化技术完成电子相册程序的开发

1.在电子相册程序界面中包含“上一张”和“下一张”,按钮和相册图片

2.点击“上一张”或者“下一张”按钮均可切换相册图片

import java.awt.BorderLayout;
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.UIManager;

public class Photo extends JFrame implements ActionListener {
JButton b1,b2,b3;//声明组件
int i = 0;//定义变量记录图片编号
String[] img = {"01.jpg","02.jpg","03.jpg","04.jpg"};//定义图片名称数组
public Photo(){
b1 = new JButton("上一张");//示例化组件

b1.addActionListener(this);
b2 = new JButton("下一张");
b2.addActionListener(this);
b3 = new JButton("",new ImageIcon("C:/"+img[i]));
this.add(b1,BorderLayout.WEST);
this.add(b2,BorderLayout.EAST);
this.add(b3);

//设置JFrame容器属性
this.setTitle("电子相册");
this.setSize(400, 300);
this.setVisible(true);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
try{

//设置记事本的风格与当前系统风格一致
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
e.printStackTrace();
}
new Photo();
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b1){//判断点击上一张按钮
i = i-1<0?3:i-1;//判断图片编号的值
b3.setIcon(new ImageIcon("C:/l"+img[i]));
}else if(e.getSource()==b2){//判断点击下一张按钮
i=i+1>3?0:i+1;//判断图片编号的值
b3.setIcon(new ImageIcon("C:/"+img[i]));
    }
  }

}

原文地址:https://www.cnblogs.com/tianyating/p/9128537.html