-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wuqidian.java
306 lines (286 loc) · 14.4 KB
/
Wuqidian.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package UI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Wuqidian {
public static void wuqi(){
JFrame f2 = new JFrame("武器店");
f2.setSize(new Dimension(700, 200));
f2.setLocation(500,500);
JPanel p1 = new JPanel();
JLabel l1 = new JLabel(new ImageIcon("src/images/天顶剑.png"));
JButton bt1 = new JButton("天顶剑 atk+200");
JLabel l2 = new JLabel(new ImageIcon("src/images/泰拉剑.png"));
JButton bt2 = new JButton("泰拉剑 atk+150");
JLabel l3 = new JLabel(new ImageIcon("src/images/波涌之刃.png"));
JButton bt3 = new JButton("波涌之刃 atk+100");
p1.add(bt1);p1.add(l1);
bt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
www1();}});
p1.add(bt2);p1.add(l2);
bt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
www2();}});
p1.add(bt3);p1.add(l3);
bt3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
www3();}});
f2.add(p1);
f2.setVisible(true);
// 创建退出按钮,并添加到面板中
JButton exit = new JButton("退出");
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 处理退出按钮的逻辑
f2.dispose(); // 关闭JFrame窗口
}
});
f2.add(exit,BorderLayout.PAGE_END);
}
public static void www1(){
JDialog j1 = new JDialog();
j1.setTitle("价格");
j1.setLayout(new BorderLayout()); // 修改布局方式为边框布局
j1.setLocation(230,400);
j1.setSize(new Dimension(200, 100));
j1.setModal(true);
JLabel ll1 = new JLabel("200");
ll1.setPreferredSize(new Dimension(180, 30)); // 设置首选尺寸
JPanel buttonPanel = new JPanel(new GridLayout(1,2)); // 创建一个放置按钮的面板
JButton buy = new JButton("购买");
JButton exit = new JButton("退出");
buy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int price = Integer.parseInt(ll1.getText()); // 获取ll1中的数量
if (price > 0) {
try {// 连接数据库
Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
String url = "jdbc:mysql://localhost:3306/final?serverTimezone=GMT%2B8&useSSL=false";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
// 查询钱包的钱
ResultSet rs = stmt.executeQuery("SELECT monkey FROM monkey");
if (rs.next()) {
int stock = rs.getInt("monkey");
if (price < stock) {
// 更新数据库中的库存
stock -= price;
stmt.executeUpdate("UPDATE monkey SET monkey = " + stock);
JOptionPane.showMessageDialog(null, "购买成功!");
j1.dispose();
} else {
JOptionPane.showMessageDialog(null, "没钱了,购买失败!");}}
// 读取 pikaqiu 表的 atk 列的当前值
ResultSet rss = stmt.executeQuery("SELECT atk FROM pikaqiu");
int atk = 0;
if (rss.next()) {
atk = rss.getInt("atk");
}
atk += price;
stmt = conn.createStatement();
PreparedStatement pstmt = conn.prepareStatement("UPDATE pikaqiu SET atk = ?");
pstmt.setInt(1, atk);
pstmt.executeUpdate();
rss.close();
rs.close();
stmt.close();
conn.close();// 关闭JDialog窗口
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}}});
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
j1.dispose(); // 关闭JDialog窗口
}
});
buttonPanel.add(buy);
buttonPanel.add(exit);
j1.add(ll1, BorderLayout.CENTER); // 将标签添加到JDialog的中心区域
j1.add(buttonPanel, BorderLayout.SOUTH); // 将按钮添加到JDialog的底部
j1.setVisible(true);
}
public static void www2(){
JDialog j1 = new JDialog();
j1.setTitle("价格");
j1.setLayout(new BorderLayout()); // 修改布局方式为边框布局
j1.setLocation(230,400);
j1.setSize(new Dimension(200, 100));
j1.setModal(true);
JLabel ll1 = new JLabel("150");
ll1.setPreferredSize(new Dimension(180, 30)); // 设置首选尺寸
JPanel buttonPanel = new JPanel(new GridLayout(1,2)); // 创建一个放置按钮的面板
JButton buy = new JButton("购买");
buy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int price = Integer.parseInt(ll1.getText()); // 获取ll1中的数量
if (price > 0) {
try {// 连接数据库
Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
String url = "jdbc:mysql://localhost:3306/final?serverTimezone=GMT%2B8&useSSL=false";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
// 查询钱包的钱
ResultSet rs = stmt.executeQuery("SELECT monkey FROM monkey");
if (rs.next()) {
int stock = rs.getInt("monkey");
if (price < stock) {
// 更新数据库中的库存
stock -= price;
stmt.executeUpdate("UPDATE monkey SET monkey = " + stock);
JOptionPane.showMessageDialog(null, "购买成功!");
j1.dispose();
} else {
JOptionPane.showMessageDialog(null, "没钱了,购买失败!");}}
rs.close();
stmt.close();
conn.close();// 关闭JDialog窗口
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}}});
JButton exit = new JButton("退出");
buy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int price = Integer.parseInt(ll1.getText()); // 获取ll1中的数量
if (price > 0) {
try {// 连接数据库
Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
String url = "jdbc:mysql://localhost:3306/final?serverTimezone=GMT%2B8&useSSL=false";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
// 查询钱包的钱
ResultSet rs = stmt.executeQuery("SELECT monkey FROM monkey");
if (rs.next()) {
int stock = rs.getInt("monkey");
if (price < stock) {
// 更新数据库中的库存
stock -= price;
stmt.executeUpdate("UPDATE monkey SET monkey = " + stock);
JOptionPane.showMessageDialog(null, "购买成功!");
j1.dispose();
} else {
JOptionPane.showMessageDialog(null, "没钱了,购买失败!");}}
ResultSet rss = stmt.executeQuery("SELECT atk FROM pikaqiu");
int atk = 0;
if (rss.next()) {
atk = rss.getInt("atk");
}
atk += price;
stmt = conn.createStatement();
PreparedStatement pstmt = conn.prepareStatement("UPDATE pikaqiu SET atk = ?");
pstmt.setInt(1, atk);
pstmt.executeUpdate();
rss.close();
rs.close();
stmt.close();
conn.close();// 关闭JDialog窗口
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}}});
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
j1.dispose(); // 关闭JDialog窗口
}
});
buttonPanel.add(buy);
buttonPanel.add(exit);
j1.add(ll1, BorderLayout.CENTER); // 将标签添加到JDialog的中心区域
j1.add(buttonPanel, BorderLayout.SOUTH); // 将按钮添加到JDialog的底部
j1.setVisible(true);
}
public static void www3(){
JDialog j1 = new JDialog();
j1.setTitle("价格");
j1.setLayout(new BorderLayout()); // 修改布局方式为边框布局
j1.setLocation(230,400);
j1.setSize(new Dimension(200, 100));
j1.setModal(true);
JLabel ll1 = new JLabel("100");
ll1.setPreferredSize(new Dimension(180, 30)); // 设置首选尺寸
JPanel buttonPanel = new JPanel(new GridLayout(1,2)); // 创建一个放置按钮的面板
JButton buy = new JButton("购买");
JButton exit = new JButton("退出");
buy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int price = Integer.parseInt(ll1.getText()); // 获取ll1中的数量
if (price > 0) {
try {// 连接数据库
Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
String url = "jdbc:mysql://localhost:3306/final?serverTimezone=GMT%2B8&useSSL=false";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
// 查询钱包的钱
ResultSet rs = stmt.executeQuery("SELECT monkey FROM monkey");
if (rs.next()) {
int stock = rs.getInt("monkey");
if (price < stock) {
// 更新数据库中的库存
stock -= price;
stmt.executeUpdate("UPDATE monkey SET monkey = " + stock);
JOptionPane.showMessageDialog(null, "购买成功!");
j1.dispose();
} else {
JOptionPane.showMessageDialog(null, "没钱了,购买失败!");}}
// 读取 pikaqiu 表的 atk 列的当前值
ResultSet rss = stmt.executeQuery("SELECT atk FROM pikaqiu");
int atk = 0;
if (rss.next()) {
atk = rss.getInt("atk");
}
atk += price;
stmt = conn.createStatement();
PreparedStatement pstmt = conn.prepareStatement("UPDATE pikaqiu SET atk = ?");
pstmt.setInt(1, atk);
pstmt.executeUpdate();
rss.close();
rs.close();
stmt.close();
conn.close();// 关闭JDialog窗口
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}}});
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
j1.dispose(); // 关闭JDialog窗口
}
});
buttonPanel.add(buy);
buttonPanel.add(exit);
j1.add(ll1, BorderLayout.CENTER); // 将标签添加到JDialog的中心区域
j1.add(buttonPanel, BorderLayout.SOUTH); // 将按钮添加到JDialog的底部
j1.setVisible(true);
}
}