-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaoju.java
250 lines (230 loc) · 11.4 KB
/
Daoju.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
package UI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Daoju {
public static void jj(){
JFrame f2 = new JFrame("道具店");
f2.setSize(new Dimension(1000, 200));
f2.setLocation(800,500);
JPanel p1 = new JPanel();
ImageIcon icon = new ImageIcon("src/images/复活草.png");
Image i = icon.getImage(); // 获取原始图片对象
Image s = i.getScaledInstance(100, 100, Image.SCALE_SMOOTH); // 缩放图片
ImageIcon I = new ImageIcon(s); // 创建经过缩放的图片对象
JLabel l1 = new JLabel(I); // 创建JLabel,并传入经过缩放的图片对象
JButton bt1 = new JButton("复活草:有一次复活机会");
ImageIcon icon1 = new ImageIcon("src/images/元气碎片.png");
Image i1 = icon1.getImage();
Image s1 = i1.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
ImageIcon I1 = new ImageIcon(s1);
JLabel l2 = new JLabel(I1);
JButton bt2 = new JButton("元气碎片:复活,但是只有一半的生命值");
ImageIcon icon2 = new ImageIcon("src/images/伤药.png");
Image i2 = icon2.getImage();
Image s2 = i2.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
ImageIcon I2 = new ImageIcon(s2);
JLabel l3 = new JLabel(I2);
JButton bt3 = new JButton("伤药:生命值加30");
p1.add(bt1);p1.add(l1);
bt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jjj1();}});
p1.add(bt2);p1.add(l2);
bt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jjj2();}});
p1.add(bt3);p1.add(l3);
bt3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jjj3();}});
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 jjj1(){
JDialog j1 = new JDialog();
j1.setTitle("价格");
j1.setLayout(new BorderLayout()); // 修改布局方式为边框布局
j1.setLocation(1100,400);
j1.setSize(new Dimension(200, 100));
j1.setModal(true);
JLabel ll1 = new JLabel("2000");
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, "没钱了,购买失败!");}}
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 jjj2(){
JDialog j1 = new JDialog();
j1.setTitle("价格");
j1.setLayout(new BorderLayout()); // 修改布局方式为边框布局
j1.setLocation(1200,400);
j1.setSize(new Dimension(200, 100));
j1.setModal(true);
JLabel ll1 = new JLabel("1000");
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, "没钱了,购买失败!");}}
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 jjj3(){
JDialog j1 = new JDialog();
j1.setTitle("价格");
j1.setLayout(new BorderLayout()); // 修改布局方式为边框布局
j1.setLocation(1300,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, "没钱了,购买失败!");}}
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);
}
}