日志四:数据库连接与随机数的使用

来源:互联网 发布:淘宝店铺信誉级别分类 编辑:程序博客网 时间:2024/05/29 03:16

【导语】
敲了一个星期的代码,“猜猜看”这个游戏还是没有一点成效,对于如何实现这个游戏,我到现在还不是很懂它的功能,其实老师每一次的实验都是“猜猜看”游戏的拆分,但是我对于老师的每一小步都还不是很懂,我这些天也每天都在做一些东西,但是感觉收获一天比一天少,现在我开始停在那里,不知道怎么继续了,我不知道怎么去实现三张不同的图片随机显示,我今天在实验室坐了一个早上,什么都感觉没做到一样,我还是停在那里,前进不了,我不知道怎么做,我现在尽量去把我所收获到的记录下来。


【目的】
1.了解随机数的生成,及图片的显示。
2.了解Java连接数据库的步骤与方法,以及MySQL数据库的安装与使用。


【目标】
1. 随机相片显示
2. 数据库初步


【方法与步骤】
1. 了解随机数的生成
要产生随机数,可以使用Java api中java.lang包中的Math类.Math类以静态方法的方式提供常用的数学方法,
其中Math.random()方法是一个可以产生[0.0,1.0]区间内的一个双精度浮点数的方法
如:产生一个100以内的整数:int x=(int)(Math.random()*100);
又如:产生一个1-50之间的随机数:int x=1+(int)(Math.random()*50)


2. 图片的显示:
1).首先是applet应用小程序的图像导入
2).再看在Application中利用swing包,构建带有图片的窗体
3).也可以自己写一个窗口类,然后重载其中的paint方法

3. 数据库的安装
数据库的安装我就不详细写了,之前上课的时候我就有安装过了,安装的具体步骤我也详细的写在了博客里了,这是那个安装过程的博客地址:http://blog.csdn.net/u014029090/article/details/46050419,这是我之前的上课的时候要做的作业来的,我都已经在我自己的电脑那里安装好了。


4. 结合WB进行简单编程
     这节课呢?我做的东西并不是那么多,虽然是各种想法,但是没有几个是可以成功的,我遗漏了老师给我们的模板了,快下课的时候,我去老师的空间那里看了看,我发现老师有给我们一些提示,我居然就这样错过了,现在这个时候才发现自己平时关注得太少了,我复制了老师的模板,发现老师的模板可以实现一张图片的随机,于是我根据我自己的想法,我慢慢的修改,最后实现了3张相同图片图片显示,这对于我来说已经是很好的一步了,我相信,我不断的去思考,总会想出办法来的。加油!
 

【实验代码】

<pre class="java" name="code">package test;import java.awt.BorderLayout;//文件过滤器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 class Guess extends JFrame implements ActionListener{JButton button = null;JButton button_1 = null;JButton  button_2 = null;JLabel  label = null;JLabel  label_1 = null;JLabel  lblNewLabel = null;private JPanel contentPane;private JTextField textField;private JTextField textField_1;    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 {Guess frame = new Guess();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/** * Create the frame. */public Guess() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);//创建“选择目录”按钮button = new JButton("\u9009\u62E9\u76EE\u5F55");button.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {            if(e.getSource()==button){    //如果是open按钮                    JFileChooser jfc=new JFileChooser();                    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());                    // 把文件路径显示在文本框中                    textField.setText(file.getAbsolutePath());                                        strPath = file.getAbsolutePath();                    strFileName = jfc.getSelectedFile().getName();                                                            fileArray = file.listFiles();                    NUM_IMG = fileArray.length;                }                             }        });button.setBounds(10, 20, 93, 23);contentPane.add(button);//创建“选择班级”按钮button_1 = new JButton("\u9009\u62E9\u73ED\u7EA7");button_1.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {            if(e.getSource()==button_1){    //如果是open1按钮                    JFileChooser jfc1=new JFileChooser();                        jfc1.showDialog(new JLabel(), "选择");                        File file1=jfc1.getSelectedFile();                    if(file1.isDirectory()){                        System.out.println("文件夹:"+file1.getAbsolutePath());                                            }else if(file1.isFile()){                        System.out.println("文件:"+file1.getAbsolutePath());                    }                    System.out.println(jfc1.getSelectedFile().getName());                                            // 把文件路径显示在文本框中                    textField_1.setText(file1.getAbsolutePath());                    //jlbImg.setIcon(new ImageIcon(file.getAbsolutePath()));                                        // 获取文件路径 与文件名                    strPath = file1.getAbsolutePath();                    strFileName = jfc1.getSelectedFile().getName();                                        if(file1!=null && file1.isDirectory()){                        // 参考: java中File.listFiles(FileFilter) FileFilter的使用                         //  http://zhouzaibao.iteye.com/blog/347557 ;                    }            }            }        });button_1.setBounds(10, 53, 93, 23);contentPane.add(button_1);//添加“选择目录”的文本框textField = new JTextField();textField.setBounds(111, 21, 297, 21);contentPane.add(textField);textField.setColumns(10);//添加“选择班级”的文本框textField_1 = new JTextField();textField_1.setBounds(113, 54, 295, 21);contentPane.add(textField_1);textField_1.setColumns(10);//添加显示第一张图片的JLabel控件label = new JLabel("\u56FE\u72471");label.setBounds(10, 104, 121, 132);contentPane.add(label);label.addMouseListener(new MouseAdapter() {            public void mouseClicked(MouseEvent e,Component p) {            JOptionPane.showMessageDialog(p,"恭喜你猜对了,是否要继续呢?","提示",1);                            }});//添加显示第二张图片的JLabel控件label_1 = new JLabel("\u56FE\u72473");label_1.setBounds(296, 104, 121, 132);contentPane.add(label_1);label_1.addMouseListener(new MouseAdapter() {            public void mouseClicked(MouseEvent e,Component p) {            JOptionPane.showMessageDialog(p,"恭喜你猜对了,是否要继续呢?","提示",1);                            }});//添加显示第三张图片的JLabel控件lblNewLabel = new JLabel("\u56FE\u72472");lblNewLabel.setBounds(154, 104, 121, 132);contentPane.add(lblNewLabel);lblNewLabel.addMouseListener(new MouseAdapter() {            public void mouseClicked(MouseEvent e, Component p) {            JOptionPane.showMessageDialog(p,"恭喜你猜对了,是否要继续呢?","提示",1);                            }});//添加显示“姓名”的JLabel控件JLabel label_2 = new JLabel("\u59D3\u540D");label_2.setBounds(154, 85, 103, 15);contentPane.add(label_2);//创建“再猜一次”按钮button_2 = new JButton("\u518D\u731C\u4E00\u6B21");button_2.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                if(e.getSource()==button_2){ //如果是next按钮                    String strTmp = fileArray[index].toString();//strTmp是绝对路径                    index++;                    if(index==NUM_IMG)                        index = 0;                    label.setIcon(new ImageIcon(strTmp));                    label_1.setIcon(new ImageIcon(strTmp));                    lblNewLabel.setIcon(new ImageIcon(strTmp));                }                                   }        });button_2.setBounds(164, 240, 93, 20);contentPane.add(button_2);               }@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stub}}



【实验结果】

  

0 0
原创粉丝点击