这个星期的任务是做一个JAVA猜猜看游戏
目前正在研究阶段,还不是很有头绪,先把随机数以及java链接数据库弄懂先吧。
这天我没考虑太多的问题,就在老师给的源代码上加点盐加点醋,很粗糙的把图片显示成三张
首先,要运用到随机数,定义三个不同的随机数,分别把他们投射到Image上,再把Image投射到相应的显示图片的标签上
然后,还是看代码吧。。比较不会说,也不知道怎么解释,唉,其实我也看不懂的。多百度吧。
下面是随机显示三张图片的代码
import java.awt.EventQueue;
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.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
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.util.Random;
public class testtest extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField tfDir;
private JTextField tfClass;
String strPath = "";
String strFileName = "";
File[] fileArray;
int NUM_IMG = 0;
int index = 0;
/**
* 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(" 猜猜看游戏FV0.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);
final JButton btnDir = new JButton("\u9009\u62E9\u76EE\u5F55");
btnDir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnDir){
JFileChooser jfc=new JFileChooser();
//jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
if(file.isDirectory()){
System.out.println("文件夹:"+file.getAbsolutePath());
}else if(file.isFile()){
System.out.println("文件:"+file.getAbsolutePath());
}
System.out.println(jfc.getSelectedFile().getName());
// 把文件路径显示在文本框中
tfDir.setText(file.getAbsolutePath());
//jlbImg.setIcon(new ImageIcon(file.getAbsolutePath()));
// 获取文件路径 与文件名
strPath = file.getAbsolutePath();
strFileName = jfc.getSelectedFile().getName();
if(file!=null && file.isDirectory());
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("\u9009\u62E9\u73ED\u7EA7");
btnClass.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
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("\u56FE\u72471");
lblImg1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
}
});
lblImg1.setBounds(26, 151, 183, 178);
contentPane.add(lblImg1);
// 标签,显示第二个学生相片
final JLabel lblImg2 = new JLabel("\u56FE\u72472");
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("\u56FE\u72473");
lblImg3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
lblImg3.setBounds(434, 155, 185, 172);
contentPane.add(lblImg3);
// 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名
final JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21");
btnGuessAgain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnGuessAgain){ //如果是next按钮
String[] imgSrcs = {"D:/photo/1.jpg","D:/photo/2.jpg","D:/photo/3.jpg","D:/photo/4.jpg","D:/photo/5.jpg","D:/photo/6.jpg","D:/photo/7.jpg","D:/photo/8.jpg","D:/photo/9.jpg","D:/photo/10.jpg","D:/photo/11.jpg","D:/photo/12.jpg","D:/photo/13.jpg","D:/photo/14.jpg","D:/photo/15.jpg","D:/photo/16.jpg","D:/photo/17.jpg","D:/photo/18.jpg","D:/photo/19.jpg","D:/photo/20.jpg","D:/photo/21.jpg","D:/photo/22.jpg","D:/photo/23.jpg","D:/photo/24.jpg"};
<span style="font-family: Arial, Helvetica, sans-serif;">Random r = new Random();</span>
String imageSrc = imgSrcs[r.nextInt(24)];
String imageSrc1 = imgSrcs[r.nextInt(24)];
String imageSrc2 = imgSrcs[r.nextInt(24)];
lblImg1.setIcon(new ImageIcon(imageSrc));
lblImg2.setIcon(new ImageIcon(imageSrc1));
lblImg3.setIcon(new ImageIcon(imageSrc2));
}
}
});
btnGuessAgain.setBounds(223, 337, 93, 23);
contentPane.add(btnGuessAgain);
}
}