-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool.java
More file actions
318 lines (270 loc) · 8 KB
/
Copy pathtool.java
File metadata and controls
318 lines (270 loc) · 8 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
package pane;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class tool {
static int page=0;
static int pages=10;
final static HeroTableModel htm=new HeroTableModel();
final static JTable t=new JTable(htm);
static JButton bHome=new JButton("首页");
static JButton bPre=new JButton("上一页");
static JButton bNe=new JButton("下一页");
static JButton bEnd=new JButton("末页");
static JPanel p=new JPanel();
static JFrame f=new JFrame("lol");
static JPanel p2=new JPanel();
static JComboBox jc;
public static void main(String[] args) {
f.setSize(400, 325);
f.setLocation(200, 200);
f.setLayout(null);
t.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
t.getSelectionModel().setSelectionInterval(0, 0);
String[] comboxItems=new String[page()];
for(int i=0;i<page();i++) {
comboxItems[i]=Integer.toString(i);
}
jc=new JComboBox(comboxItems);
p.setBounds(90, 190, 200, 40);
JButton bAdd=new JButton("增加");
JButton bDelect=new JButton("删除");
JButton bEdit=new JButton("编辑");
p2.setBounds(20,230,350,40);
p.add(bAdd);p.add(bDelect);p.add(bEdit);
p2.add(bHome);p2.add(bPre);p2.add(jc);
p2.add(bNe);p2.add(bEnd);
updateButtonStatus();
List[] ppp=splitList(htm.heros);
htm.heros=ppp[page];
jc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int g=Integer.valueOf(jc.getSelectedItem().toString());
page=g*pages;
updateTable();
updateButtonStatus();
}
});
bHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
page=0;
updateTable();
updateButtonStatus();
}
});
bNe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
page+=pages;
updateTable();
updateButtonStatus();
}
});
bPre.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
page-=pages;
updateTable();
updateButtonStatus();
}
});
bEnd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
page=last();
updateTable();
updateButtonStatus();
}
});
JScrollPane sp=new JScrollPane(t);
sp.setBounds(0,0,400,185);
bEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog jd=new JDialog();
jd.setSize(300,200);
jd.setLocation(250,250);
jd.setLayout(null);
jd.setVisible(true);
JLabel name=new JLabel("名称");name.setBounds(50,0,70,70);
JTextField tName=new JTextField();tName.setBounds(100,25,100,20);
JLabel hp=new JLabel("血量");hp.setBounds(50,50,70,70);
JTextField tHp=new JTextField();tHp.setBounds(100,75,100,20);
JButton jb=new JButton("提交");jb.setBounds(110,110,75,30);
jd.add(jb);
jd.add(name);jd.add(tName);jd.add(hp);jd.add(tHp);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name=tName.getText();
String hp=tHp.getText().trim();
if(name.length()==0) {
JOptionPane.showMessageDialog(jd, "名称不能为空");
}else if(hp.isEmpty()){
JOptionPane.showMessageDialog(jd, "血量不能为空");
}else {
HeroDao hd=new HeroDao();
Hero hero=htm.heros.get(t.getSelectedRow());
hero.name=name;hero.hp=Float.parseFloat(hp);
hd.update(hero);
htm.heros=hd.list();
updateTable();
updateButtonStatus();
jd.setVisible(false);
}
}
});
}
});
bDelect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i=JOptionPane.showConfirmDialog(f, "确认要删除?");
if(i==0) {
HeroDao hd=new HeroDao();
Hero h=new Hero();
int id=t.getSelectedRow();
h=htm.heros.get(id);
hd.delete(h.id);
htm.heros=hd.list();
updateTable();
updateButtonStatus();
}
}
});
bAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog jd=new JDialog();
jd.setSize(300,200);
jd.setLocation(255,260);
jd.setLayout(null);
JLabel lName=new JLabel("名称");lName.setBounds(50,0,70,70);
JTextField jtName=new JTextField("");jtName.setBounds(100,25,100,20);
JLabel lHp=new JLabel("血量");lHp.setBounds(50,50,70,70);
JTextField jtHp=new JTextField("");jtHp.setBounds(100,75,100,20);
jd.add(lName);jd.add(jtName);jd.add(lHp);jd.add(jtHp);
jtName.setPreferredSize(new Dimension(80,30));
jtHp.setPreferredSize(new Dimension(80,30));
JButton badd=new JButton("确定");badd.setBounds(110,110,75,30);
jd.add(badd);
badd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
HeroDao dao=new HeroDao();
Hero h=new Hero();
String name=jtName.getText();
if(name.length()==0) {
JOptionPane.showMessageDialog(f, "名称不能为空");
jtName.grabFocus();
return;
}
String hp=jtHp.getText().trim();
try {
Float.parseFloat(hp);
}catch(NumberFormatException e1) {
JOptionPane.showMessageDialog(f, "血量只能是小数");
jtHp.grabFocus();
return;
}
h.name=name;
h.hp=Float.parseFloat(hp);
dao.add(h);
htm.heros=dao.list();
updateTable();
updateButtonStatus();
t.getSelectionModel().setSelectionInterval(0, 0);
JOptionPane.showMessageDialog(jd, "提交成功");
jd.setVisible(false);
}
});
jd.setVisible(true);
}
});
f.add(sp);
f.add(p);
f.add(p2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private static List[] splitList(List list) {
int pages=list.size()/10;
int lastCount=list.size()%10;
if(lastCount!=0) {
pages+=1;
}
List[] listHero=new ArrayList[pages];
for(int i=0;i<pages;i++) {
List SplitPage = new ArrayList() ;
listHero[i]=SplitPage;
}
int io=0;
int count=0;
for(int i=0;i<list.size();i++) {
if(count==10) {
io++;
count=0;
}
listHero[io].add(list.get(i));
count++;
}
return listHero;
}
public static void updateTable() {
htm.heros=new HeroDao().list(page,pages);
t.updateUI();
}
public static void updateButtonStatus() {
int last=last();
if(0!=page) {
bHome.setEnabled(true);
bPre.setEnabled(true);
}
if(0==page) {
bHome.setEnabled(false);
bPre.setEnabled(false);
}
if(page==last) {
bEnd.setEnabled(false);
bNe.setEnabled(false);
}
if(page!=last) {
bEnd.setEnabled(true);
bNe.setEnabled(true);
}
}
public static int last() {
int last=0;
int total=new HeroDao().getTotal();
if(0==total%pages) {
last=total-pages;
}else {
last=total-total%pages;
}
return last;
}
public static int page() {
int pe=0;
int total=new HeroDao().getTotal();
if(0==total%pages) {
return pe=total/pages;
}else{
return pe=(total/pages)+1;
}
}
}