可以说是几经周折,今天终于有点小成就了。编写了代码将目录选择按钮可以使用,通过按钮选择图片所在目录,代码如下:
JButton btnDir = new JButton("目录");
btnDir.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
JFileChooser j=new JFileChooser();
j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置Filechooser
j.showDialog(new JLabel(), "选择目录");
File file=j.getSelectedFile();
tfDir.setText(file.getAbsolutePath()); // TODO Auto-generated method stub
if((file!=null && file.isDirectory())){
// 获取文件夹下所有的文件
fileArray = file.listFiles();
NUM_IMG = fileArray.length;
但是还不能实现显示图片功能,所以在最后的随机选择图片代码中编写运算法则,使得图片可以随机显示出来,并且通过猜一猜按钮将图片转换成另外一组,具体代码如下:
if(e.getSource()==btnNext){ //如果是next按钮 //随机数
Random random = new Random(System.currentTimeMillis()); //循环三次
ImageIcon icon;//定义icon变量
for(int i=0;i<3;i++) {
index = random.nextInt(NUM_IMG);
String strTmp = fileArray[index].toString();
try { icon = new ImageIcon(ImageIO.read(new File(strTmp)));
Image image = icon.getImage(); // 从图表中获取到图片
Image smallImage = image.getScaledInstance(150,200,Image.SCALE_FAST);// 缩放图像
icon = new ImageIcon(smallImage);//把Image文件转化为ImageIcon
if(index==NUM_IMG) index = 0;
switch(i) { case 0: lblImg1.setIcon(icon);
break;
case 1: lblImg2.setIcon(icon);
break;
case 2: lblImg3.setIcon(icon);
break;
}
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
完整的代码和猜猜看小程序运行的截图:
import java.awt.EventQueue;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Image;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class Guess01 extends JFrame {
/** * */ private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField tfDir;
private JTextField tfClass;
File[] fileArray; // 文件夹下所有文件
int NUM_IMG = 0; // 文件总数目
int index = 0; // 当前文件的序号
JLabel jlbImg = null;
JButton btnNext = null;
/** * Launch the application. */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Guess01 frame = new Guess01();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
} /** * Create the frame. */
public Guess01() {
setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 645, 409);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null); // 选择目录 按钮的处理程序
JButton btnDir = new JButton("目录");
btnDir.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
JFileChooser j=new JFileChooser();
j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置Filechooser
j.showDialog(new JLabel(), "选择目录");
File file=j.getSelectedFile();
tfDir.setText(file.getAbsolutePath()); // TODO Auto-generated method stub
if((file!=null && file.isDirectory())){
// 参考: java中File.listFiles(FileFilter) FileFilter的使用 // ;
// 获取文件夹下所有的文件
fileArray = file.listFiles();
NUM_IMG = fileArray.length;
}
}
});
btnDir.setBounds(26, 26, 93, 23);
contentPane.add(btnDir); // 文本框,显示目录
tfDir = new JTextField();
tfDir.setEditable(false);
tfDir.setBounds(125, 27, 363, 21);
contentPane.add(tfDir);
tfDir.setColumns(10); // 选择班级 按钮的处理程序
JButton btnClass = new JButton("班级");
btnClass.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser i=new JFileChooser();
i.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置Filechooser
i.showDialog(new JLabel(), "选择班级");
File file=i.getSelectedFile();
tfClass.setText(file.getAbsolutePath());
fileArray = file.listFiles();
NUM_IMG = fileArray.length;
}
});
btnClass.setBounds(26, 59, 93, 23);
contentPane.add(btnClass); // 文本框,显示班级文件
tfClass = new JTextField();
tfClass.setEditable(false);
tfClass.setBounds(125, 60, 363, 21);
contentPane.add(tfClass);
tfClass.setColumns(10); // 标签,显示带猜测学生姓名
JLabel lbGuessName = new JLabel("\u59D3\u540D");
lbGuessName.setBounds(259, 91, 102, 23);
contentPane.add(lbGuessName); // 标签,显示第一个学生相片
final JLabel lblImg1 = new JLabel("");
setBackground(Color.RED);
lblImg1.setBounds(100, 100, 200, 200);
this.add(lblImg1);
lblImg1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
}
});
lblImg1.setBounds(26, 151, 183, 178);
contentPane.add(lblImg1); // 标签,显示第二个学生相片
final JLabel lblImg2 = new JLabel("");
lblImg2.setBackground(Color.RED);
lblImg2.setBounds(100, 100, 200, 200);
this.add(lblImg2);
lblImg2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
lblImg2.setForeground(Color.BLACK);
lblImg2.setBackground(SystemColor.inactiveCaption);
lblImg2.setBounds(241, 155, 183, 172);
contentPane.add(lblImg2); // 标签,显示第三个学生相片
final JLabel lblImg3 = new JLabel("");
lblImg3.setBackground(Color.RED);
lblImg3.setBounds(100, 100, 200, 200);
this.add(lblImg3);
lblImg3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
lblImg3.setBounds(434, 155, 185, 172);
contentPane.add(lblImg3); // 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名
final JButton btnNext = new JButton("再猜一次");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnNext){ //如果是next按钮 //随机数
Random random = new Random(System.currentTimeMillis()); //循环三次
ImageIcon icon;//定义icon变量
for(int i=0;i<3;i++) {
index = random.nextInt(NUM_IMG);
String strTmp = fileArray[index].toString();
try { icon = new ImageIcon(ImageIO.read(new File(strTmp)));
Image image = icon.getImage(); // 从图表中获取到图片
Image smallImage = image.getScaledInstance(150,200,Image.SCALE_FAST);// 缩放图像
icon = new ImageIcon(smallImage);//把Image文件转化为ImageIcon
if(index==NUM_IMG) index = 0;
switch(i) { case 0: lblImg1.setIcon(icon);
break;
case 1: lblImg2.setIcon(icon);
break;
case 2: lblImg3.setIcon(icon);
break;
}
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
btnNext.setBounds(223, 337, 93, 23);
contentPane.add(btnNext); }
}