由于没有好好阅读老师发表关于《猜猜看》游戏及评分标准,所以昨天及前天就没有在CSDN上发表关于课程实习的日记。
课程实习的第一、二天,我主要是将16周没有完成的作业继续完成,顺便思考了实现《猜猜看》游戏的大体思路。
虽然经过了两天的思考,有了实习游戏的思路,而且也有了行动,但是到现在还是没有将游戏的皱形给描绘出来。现在完成的部分只是限于游戏猜猜看的界面以及按钮,而且代码还是从前两周的作业及同学的代码中参考过来的。
今天的进度的结果图如下:
附上代码:
<span style="font-size:18px;">import java.awt.EventQueue;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
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.FileFilter;
import java.io.IOException;
public class Guess02 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; // 当前文件的序号
private int idImg = 1;
JLabel jlbImg1 = null;
JLabel jlbImg2 = null;
JLabel jlbImg3 = null;
/**
* Launch the application.
*/
class myFileFilter implements FileFilter{
@Override
public boolean accept(File pathname) {
String filename = pathname.getName().toLowerCase();
if(filename.contains(".jpg")){
return false;
}else{
return true;
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Guess02 frame = new Guess02();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Guess02() {
setTitle("猜猜看游戏V0.1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 645, 409);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//边界布局器,(5行5列)
setContentPane(contentPane);
contentPane.setLayout(null);
// 选择目录 按钮的处理程序
JButton btnDir = new JButton("选择目录");
btnDir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "确定");
File file=jfc.getSelectedFile();
tfDir.setText(file.getAbsolutePath());
if(file!=null && file.isDirectory()){
// 参考: java中File.listFiles(FileFilter) FileFilter的使用
// http://zhouzaibao.iteye.com/blog/347557 ;
// 获取文件夹下所有的文件
fileArray = file.listFiles();
NUM_IMG = fileArray.length;
}
}
});
btnDir.setBounds(26, 26, 93, 23);
//x组件在容器X轴上的起点;y:组件在Y轴上的起点;width:组件的长度;height:组件的高度。
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 jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "确定");
File file=jfc.getSelectedFile();
tfClass.setText(file.getAbsolutePath());
}
});
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("姓名");
lbGuessName.setBounds(259, 91, 102, 23);
contentPane.add(lbGuessName);
// 标签,显示第一个学生相片
final JLabel lblImg1 = new JLabel("图片1");
lblImg1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
}
});
lblImg1.setBounds(26, 151, 183, 172);
contentPane.add(lblImg1);
jlbImg1 = new JLabel();
jlbImg1.setBackground(Color.RED);
jlbImg1.setBounds(26, 151, 183, 172);
this.add(jlbImg1);
// 标签,显示第二个学生相片
final JLabel lblImg2 = new JLabel("图片2");
lblImg2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
lblImg2.setForeground(Color.BLACK);
lblImg2.setBackground(SystemColor.inactiveCaption);
lblImg2.setBounds(229, 151, 183, 172);
contentPane.add(lblImg2);
jlbImg2 = new JLabel();
jlbImg2.setBackground(Color.RED);
jlbImg2.setBounds(229, 151, 183, 172);
this.add(jlbImg2);
// 标签,显示第三个学生相片
final JLabel lblImg3 = new JLabel("图片3");
lblImg3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
lblImg3.setBounds(434, 151, 183, 172);
contentPane.add(lblImg3);
jlbImg3 = new JLabel();
jlbImg3.setBackground(Color.RED);
jlbImg3.setBounds(434, 151, 183, 172);
this.add(jlbImg3);
// 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名
final JButton btnGuessAgain = new JButton("继续猜猜");
btnGuessAgain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnGuessAgain ){ //如果是next按钮
String strTmp = fileArray[index].toString();
index++;
if(index==NUM_IMG)
index = 0;
jlbImg1.setIcon(new ImageIcon(strTmp));
// index++;
// jlbImg2.setIcon(new ImageIcon(strTmp));
//index++;
if(e.getSource()==btnGuessAgain ){
//如果是next按钮
} String strTmp2 = fileArray[index].toString();
index++;
jlbImg2.setIcon(new ImageIcon(strTmp2));
index++;
} String strTmp1 = fileArray[index].toString();
index++;
jlbImg3.setIcon(new ImageIcon(strTmp1));
}
});
btnGuessAgain.setBounds(223, 337, 93, 23);
contentPane.add(btnGuessAgain);
}
}</span>