(1)Random类
Random类中实现的随机算法是伪随机,也就是有规则的随机。在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要的随机数字。
包含两个构造方法
a、public Random()
该构造方法使用一个和当前系统时间对应的相对时间有关的数字作为种子数,然后使用这个种子数构造Random对象。
b、public Random(long seed)
该构造方法可以通过制定一个种子数进行创建。
(2)一个简单的按钮监听
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestGUI1 { public static void main(String[] args){ JFrame f=new JFrame("MouseClick"); f.setBounds(500,200,400,300); f.setLayout(null); final JLabel l=new JLabel(); ImageIcon i=new ImageIcon("D:/小小下载/shana.png"); l.setIcon(i); l.setBounds(50,50,i.getIconWidth(),i.getIconHeight()); JButton b=new JButton("隐藏图片"); b.setBounds(150,200,100,30); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ l.setVisible(false); } }); f.add(l); f.add(b); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }
(2)MouseListener 鼠标监听器 mouseReleased 鼠标释放 mousePressed 鼠标按下
mouseExited 鼠标退出 mouseEntered 鼠标进入 mouseClicked 鼠标点击
mouseExited 鼠标退出 mouseEntered 鼠标进入 mouseClicked 鼠标点击
package gui; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class TestGUI { public static void main(String[] args) { final JFrame f = new JFrame("鼠标监听"); f.setSize(800, 600); f.setLocationRelativeTo(null);//则此窗口将置于屏幕的中央。 f.setLayout(null); final JLabel l = new JLabel(); ImageIcon i = new ImageIcon("d:/小小下载/j2se/shana"); l.setIcon(i); l.setBounds(375, 275, i.getIconWidth(), i.getIconHeight()); f.add(l); l.addMouseListener(new MouseListener() { // 释放鼠标 public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } // 按下鼠标 public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } // 鼠标退出 public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } // 鼠标进入 public void mouseEntered(MouseEvent e) { Random r = new Random(); int x = r.nextInt(f.getWidth() - l.getWidth()); int y = r.nextInt(f.getHeight() - l.getHeight()); l.setLocation(x, y); } // 按下释放组合动作为点击鼠标 public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }