public class PlayMouse extends JFrame implements Runnable {
public JLabel bgLabel;//背景图片
public JLabel[] mouses;//老鼠
public JLabel jtf; //分数
public int score;
public Random random = new Random();
public PlayMouse() {
//锤子
Cursor myCursor = null;
try {
myCursor = Toolkit.getDefaultToolkit().createCustomCursor(ImageIO.read(new File("hand.jpg")),new Point(0,0),"");
} catch (IOException e) {
e.printStackTrace();
}
setCursor(myCursor);
//得分
jtf = new JLabel();
jtf.setBounds(360,10,235,50);
jtf.setFont(new Font("",Font.BOLD,25));
jtf.setForeground(Color.blue);
jtf.setText("您的得分是: 分");
add(jtf);
bgLabel = new JLabel(); //背景图片
bgLabel.setIcon(new ImageIcon("bgimage.jpg"));
bgLabel.setBounds(0,0,1000,800);
setResizable(false);//不能修改窗体大小
setTitle("打地鼠");
setBounds(300,100,1000,800);
//连接数据库
Connection con=null;
Statement sql;
ResultSet rs;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加载Access数据库连接器
}
catch(Exception e){ }
try{
con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1435;DatabaseName=caoyao","sa","1227mq");
}
catch(SQLException e){
System.out.println(e);
}
mouses = new JLabel[17];
for (int i = 0; i < 17; i++) {
mouses[i] = new JLabel();
mouses[i].setSize(110,110);
try{
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs =sql.executeQuery("SELECT * FROM caoyao");//sql调用.executeQuery方法查询bookList表中的全部记录
rs.last();
int max = rs.getRow();
int num=(int)(Math.random()*max+1);
rs.absolute(num);//将rs的游标游标移到第i行
String path=rs.getString(3);
String name=rs.getString(2);
mouses[i].setIcon(new ImageIcon(path));
mouses[i].setVisible(false);
//监听器
mouses[i].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(name=="清热药")
score++;
else
score--;
jtf.setText("您的得分是: "+score+"分");
repaint();
}
});
add(mouses[i]);
con.close();
}
catch(SQLException e) { }
}
mouses[0].setLocation(120,520);
mouses[1].setLocation(480,520);
mouses[2].setLocation(850,520);
mouses[3].setLocation(50,420);
mouses[4].setLocation(350,420);
mouses[5].setLocation(650,420);
mouses[6].setLocation(900,420);
mouses[7].setLocation(210,333);
mouses[8].setLocation(500,333);
mouses[9].setLocation(750,333);
mouses[10].setLocation(150,250);
mouses[11].setLocation(370,250);
mouses[12].setLocation(625,250);
mouses[13].setLocation(850,250);
mouses[14].setLocation(260,150);
mouses[15].setLocation(480,150);
mouses[16].setLocation(680,150);
add(bgLabel);
setVisible(true);
new Thread(this).start();
}
public static void main(String[] args) {
PlayMouse game = new PlayMouse();
}
@Override
public void run() {
while(true){
try {
Thread.sleep(1200);
} catch (InterruptedException e) {
e.printStackTrace();
}
int pos = random.nextInt(17);//0-16
if(!mouses[pos].isVisible()){
mouses[pos].setVisible(true);
try {
Thread.sleep(1200);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(mouses[pos].isVisible()){
mouses[pos].setVisible(false);
}
}
}
}
}
为什么运行出来只有第一个位置出现图片?还有点击鼠标总是减分,明明数据库那一列都是清热药。求解答
看了你的代码 这是可能的原因
你使用的随机数生成器random的种子被定死了,所以每次生成的随机数pos都是0,导致只有第一个位置的老鼠图片出现
线程睡眠时间sleep(1200)太长,导致第二个老鼠还没有显示,第一个老鼠就已经 disappears 了,给人的感觉只有第一个位置出现图片。线程while循环体内只产生一个随机位置pos,只显示和隐藏一个老鼠图片,而没有继续生成下一个随机位置,导致只有第一个老鼠图片在显示和隐藏。
可以一起讨论
**
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
public class DButilspool implements DataSource {
private static List<Connection> list= new LinkedList<>();
static {
try {
Class.forName("com.mysql.jdbc.Driver");
for (int i = 0; i <10 ; i++) {
Connection connection= DriverManager.getConnection("jdbc:mysql:///book",
"root","123456");
list.add(connection);
}
System.out.println("创建了"+list.size()+"个连接");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
@Override
public Connection getConnection() throws SQLException {
return null;
}
@Override
public Connection getConnection(String user,String password) throws SQLException {
if (list==null) {
for (int i = 0; i <10 ; i++) {
Connection connection= DriverManager.getConnection("jdbc:mysql:///book",user,password);
list.add(connection);
}
}
Connection co=list.remove(0);
System.out.println("取出了一个连接,还有"+list.size()+"个连接");
return co;
}
public void close(Connection conn){
try {
if (conn!=null&&!conn.isClosed()){
list.add(conn);
System.out.println("归还了一个连接,还有"+list.size()+"个连接");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
}
@Override
public int getLoginTimeout() throws SQLException {
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
import java.sql.*;
public class TestDB {
public static void main(String[] args) {
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
DButilspool dButilspool = new DButilspool();
try {
connection=dButilspool.getConnection("root","123456");
preparedStatement= connection.prepareStatement("select * from user ");
resultSet=preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getString(2)+"------"+resultSet.getString(3)+"\n");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
dButilspool.close(connection);
}
}
}
第一次写,有很多不足,单纯只为记笔记,越努力,越幸运.加油!
已解决,把con.close()删掉就好啦