質問編集履歴

1

PlanAndGameクラスの追加

2016/12/14 11:37

投稿

mercredi
mercredi

スコア26

test CHANGED
File without changes
test CHANGED
@@ -109,3 +109,477 @@
109
109
  Javaの列の型をStringにすると挿入できます。
110
110
 
111
111
  日付入力は'DD-MON-RR'フォーマット以外にもいろいろな型を試してみました。
112
+
113
+
114
+
115
+ ```PlayAndGameクラスのコード
116
+
117
+ class PlayerAndGame extends JFrame {
118
+
119
+
120
+
121
+ private JPanel mainPanel = new JPanel();
122
+
123
+ private JPanel bottomLeft = new JPanel();
124
+
125
+ private JPanel topPanel = new JPanel();
126
+
127
+ private JPanel middlePanel = new JPanel();
128
+
129
+ private JPanel bottomRight = new JPanel();
130
+
131
+ private JLabel lLabel = new JLabel();
132
+
133
+ private JLabel rLabel = new JLabel();
134
+
135
+ private JLabel lblScore = new JLabel();
136
+
137
+ private JLabel lblDate = new JLabel();
138
+
139
+ private DefaultTableModel aModel = new DefaultTableModel();
140
+
141
+ private DefaultTableModel bModel=new DefaultTableModel();
142
+
143
+ private JTable lTable = new JTable(aModel);
144
+
145
+ private JTable rTable = new JTable(bModel);
146
+
147
+ private JTextField txtScore = new JTextField(15);
148
+
149
+ private JTextField txtDate = new JTextField(15);
150
+
151
+ private JScrollPane scroll1 = new JScrollPane(lTable);
152
+
153
+ private JScrollPane scroll2 = new JScrollPane(rTable);
154
+
155
+ DatabaseMainTest db = new DatabaseMainTest();
156
+
157
+ private Vector rows = new Vector();
158
+
159
+ private Vector columns = new Vector();
160
+
161
+ private Vector p_rows = new Vector();
162
+
163
+ private Vector p_columns = new Vector();
164
+
165
+ String selectedData = null;
166
+
167
+ String selectedData2 = null;
168
+
169
+
170
+
171
+ private JButton btnInsert = new JButton();
172
+
173
+ private JButton playerButton = new JButton();
174
+
175
+ private JButton gameButton = new JButton();
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+ public JTable getlTable() {
184
+
185
+ return lTable;
186
+
187
+ }
188
+
189
+
190
+
191
+ public JTable getrTable() {
192
+
193
+ return rTable;
194
+
195
+ }
196
+
197
+
198
+
199
+ public JTextField getTxtScore() {
200
+
201
+ return txtScore;
202
+
203
+ }
204
+
205
+
206
+
207
+ public JTextField getTxtDate() {
208
+
209
+ return txtDate;
210
+
211
+ }
212
+
213
+
214
+
215
+ public String getSelectedData() {
216
+
217
+ return selectedData;
218
+
219
+ }
220
+
221
+ public String getSelectedData2() {
222
+
223
+ return selectedData;
224
+
225
+ }
226
+
227
+
228
+
229
+ public PlayerAndGame() throws SQLException {
230
+
231
+ this.setSize(700, 700);
232
+
233
+ this.setTitle("Game Information");
234
+
235
+
236
+
237
+ mainPanel.setLayout(new GridLayout(3, 2, 5, 5));
238
+
239
+ bottomLeft.setLayout(new GridLayout(3, 1, 2, 2));
240
+
241
+ bottomRight.setLayout(new GridLayout(2, 1, 2, 2));
242
+
243
+ topPanel.setLayout(new GridLayout(1, 2));
244
+
245
+ middlePanel.setLayout(new GridLayout(1, 2));
246
+
247
+ mainPanel.add(lLabel);
248
+
249
+ mainPanel.add(rLabel);
250
+
251
+ mainPanel.add(scroll1);
252
+
253
+ mainPanel.add(scroll2);
254
+
255
+ topPanel.add(lblScore);
256
+
257
+ topPanel.add(txtScore);
258
+
259
+ middlePanel.add(lblDate);
260
+
261
+ middlePanel.add(txtDate);
262
+
263
+ bottomLeft.add(topPanel);
264
+
265
+ bottomLeft.add(middlePanel);
266
+
267
+ bottomLeft.add(btnInsert);
268
+
269
+ bottomRight.add(playerButton);
270
+
271
+ mainPanel.add(bottomLeft);
272
+
273
+ bottomRight.add(gameButton);
274
+
275
+ mainPanel.add(bottomRight);
276
+
277
+
278
+
279
+
280
+
281
+
282
+
283
+ add(mainPanel);
284
+
285
+
286
+
287
+ // System.out.print(txtScore.getText().toString());
288
+
289
+ // System.out.print(txtDate.getText().toString());
290
+
291
+
292
+
293
+ ResultSet gameRS = db.getTableInfo("Game");
294
+
295
+ ResultSetMetaData gameMD = gameRS.getMetaData();
296
+
297
+ // create columns headers
298
+
299
+ for (int i = 1; i <= gameMD.getColumnCount(); i++) {
300
+
301
+ columns.addElement(gameMD.getColumnName(i));
302
+
303
+ }
304
+
305
+ //
306
+
307
+ int row = 0;
308
+
309
+ while (gameRS.next())// loop for rows
310
+
311
+ {
312
+
313
+ Vector vRow = new Vector(); // to store the current row
314
+
315
+ for (int i = 1; i <= gameMD.getColumnCount(); i++)// loop for columns
316
+
317
+ {
318
+
319
+
320
+
321
+ Object columnValue = gameRS.getObject(i);
322
+
323
+ vRow.addElement(columnValue.toString());
324
+
325
+
326
+
327
+ }
328
+
329
+ row += 1;
330
+
331
+ rows.addElement(vRow);// add individual row vectors to rows
332
+
333
+
334
+
335
+ }
336
+
337
+
338
+
339
+ aModel.setDataVector(rows, columns);
340
+
341
+ gameRS.close();//end of display the game table
342
+
343
+
344
+
345
+ ///////////////////////////////////////////////////////////
346
+
347
+ ResultSet playerRS = db.getTableInfo("Player");
348
+
349
+ ResultSetMetaData playerMD = playerRS.getMetaData();
350
+
351
+ // create columns headers
352
+
353
+ for (int i = 1; i <= playerMD.getColumnCount(); i++) {
354
+
355
+ p_columns.addElement(playerMD.getColumnName(i));
356
+
357
+ }
358
+
359
+ //
360
+
361
+ int playerRow = 0;
362
+
363
+ while (playerRS.next())// loop for rows
364
+
365
+ {
366
+
367
+ Vector vRow = new Vector(); // to store the current row
368
+
369
+ for (int i = 1; i <= playerMD.getColumnCount(); i++)// loop for columns
370
+
371
+ {
372
+
373
+
374
+
375
+ Object columnValue = playerRS.getObject(i);
376
+
377
+ vRow.addElement(columnValue.toString());
378
+
379
+
380
+
381
+ }
382
+
383
+ row += 1;
384
+
385
+ p_rows.addElement(vRow);// add individual row vectors to rows
386
+
387
+
388
+
389
+ }
390
+
391
+
392
+
393
+ bModel.setDataVector(p_rows, p_columns);
394
+
395
+ playerRS.close();//end of display the player table
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+ ///////////////////////////////////////////
404
+
405
+
406
+
407
+ lTable.setCellSelectionEnabled(true);
408
+
409
+ ListSelectionModel cellSelectionModel = lTable.getSelectionModel();
410
+
411
+ cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
412
+
413
+
414
+
415
+ cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
416
+
417
+ public void valueChanged(ListSelectionEvent e) {
418
+
419
+
420
+
421
+
422
+
423
+ int[] selectedRow = lTable.getSelectedRows();
424
+
425
+ int[] selectedColumns = lTable.getSelectedColumns();
426
+
427
+
428
+
429
+ for (int i = 0; i < selectedRow.length; i++) {
430
+
431
+ for (int j = 0; j < selectedColumns.length; j++) {
432
+
433
+ selectedData = (String) lTable.getValueAt(selectedRow[i], selectedColumns[j]);
434
+
435
+ }
436
+
437
+ }
438
+
439
+ System.out.println("Selected: " + selectedData);
440
+
441
+ }
442
+
443
+
444
+
445
+ });// end of selection lister argument for lTable
446
+
447
+
448
+
449
+ rTable.setCellSelectionEnabled(true);
450
+
451
+ cellSelectionModel = rTable.getSelectionModel();
452
+
453
+ cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
454
+
455
+
456
+
457
+ cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
458
+
459
+ public void valueChanged(ListSelectionEvent e) {
460
+
461
+
462
+
463
+ int[] selectedRow = rTable.getSelectedRows();
464
+
465
+ int[] selectedColumns = rTable.getSelectedColumns();
466
+
467
+
468
+
469
+ for (int i = 0; i < selectedRow.length; i++) {
470
+
471
+ for (int j = 0; j < selectedColumns.length; j++) {
472
+
473
+ selectedData2 = (String) rTable.getValueAt(selectedRow[i], selectedColumns[j]);
474
+
475
+ }
476
+
477
+ }
478
+
479
+ System.out.println("Selected: " + selectedData2);
480
+
481
+ }
482
+
483
+
484
+
485
+ });// end of selection lister argument for rTable
486
+
487
+ btnInsert.addActionListener(new ActionListener() // anonymous inner class
488
+
489
+ {
490
+
491
+ @Override
492
+
493
+ public void actionPerformed(ActionEvent arg0) {
494
+
495
+ // PreparedStatement pst=db.insertTable();
496
+
497
+ lblScore.setText(txtScore.getText());
498
+
499
+ lblDate.setText(txtDate.getText());
500
+
501
+ try {
502
+
503
+ db.insertTable();
504
+
505
+ } catch (SQLException e) {
506
+
507
+ // TODO Auto-generated catch block
508
+
509
+ e.printStackTrace();
510
+
511
+ } catch (ParseException e) {
512
+
513
+ // TODO Auto-generated catch block
514
+
515
+ e.printStackTrace();
516
+
517
+ }
518
+
519
+ }
520
+
521
+
522
+
523
+ } // end of insert button event handler
524
+
525
+ );
526
+
527
+ }// end of constructor
528
+
529
+
530
+
531
+ public static void main(String[] args) throws SQLException {
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+ DatabaseMainTest db = new DatabaseMainTest();
540
+
541
+ // System.out.println(db.getTableInfo());
542
+
543
+ PlayerAndGame pAndGame = new PlayerAndGame();
544
+
545
+ pAndGame.setFont(new Font("Arial", Font.BOLD, 24));
546
+
547
+ // Validate frames that have preset sizes
548
+
549
+ // Pack frames that have useful preferred size info, e.g. from their
550
+
551
+ // layout
552
+
553
+
554
+
555
+ // Center the window
556
+
557
+ Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
558
+
559
+ Dimension frameSize = pAndGame.getSize();
560
+
561
+ if (frameSize.height > screenSize.height) {
562
+
563
+ frameSize.height = screenSize.height;
564
+
565
+ }
566
+
567
+ if (frameSize.width > screenSize.width) {
568
+
569
+ frameSize.width = screenSize.width;
570
+
571
+ }
572
+
573
+ pAndGame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
574
+
575
+ pAndGame.setVisible(true);
576
+
577
+
578
+
579
+ }
580
+
581
+
582
+
583
+ } // end of PlayerAndGame class
584
+
585
+ ```