回答編集履歴

1

サンプル提供

2016/01/14 14:49

投稿

ipadcaron
ipadcaron

スコア1693

test CHANGED
@@ -7,3 +7,251 @@
7
7
  今より倍くらい長いソースになっちゃうけど、処理単位が纏まるのと同じような記述がへるので
8
8
 
9
9
  読みやすくなります。
10
+
11
+ ーーーーーーーー追記
12
+
13
+ ![イメージ説明](4d1a02540802e1b8921cc30655fc30f8.png)
14
+
15
+
16
+
17
+ ```java
18
+
19
+ package com.test.swing.util;
20
+
21
+
22
+
23
+ import java.awt.BorderLayout;
24
+
25
+ import java.awt.Graphics;
26
+
27
+ import java.awt.GridLayout;
28
+
29
+ import java.awt.event.ActionEvent;
30
+
31
+ import java.awt.event.ActionListener;
32
+
33
+ import java.io.File;
34
+
35
+ import java.util.ArrayList;
36
+
37
+ import java.util.List;
38
+
39
+ import java.util.Random;
40
+
41
+
42
+
43
+ import javax.swing.ImageIcon;
44
+
45
+ import javax.swing.JButton;
46
+
47
+ import javax.swing.JFrame;
48
+
49
+ import javax.swing.JPanel;
50
+
51
+
52
+
53
+ /**
54
+
55
+ * 画像ダウンロード先
56
+
57
+ *  http://www.irasutoya.com/2012/05/asia.html
58
+
59
+ *
60
+
61
+ * eclipseLuna 4.4 Java8 で動作確認済み
62
+
63
+ *
64
+
65
+ * images/ フォルダを作りそこに上記無料画像のHPからダウンロードした画像を格納してください。
66
+
67
+ *
68
+
69
+ * プログラムはimages/ フォルダにある画像ファイル分のボタンを生成、
70
+
71
+ * ボタン表示名に画像ファイル名を短縮した名称、ツールチップにフルパス、
72
+
73
+ * ボタンクリックすると画面中央にその国旗を表示します。
74
+
75
+ */
76
+
77
+ public class FlagFrame extends JFrame implements ActionListener {
78
+
79
+
80
+
81
+ /**
82
+
83
+ *
84
+
85
+ */
86
+
87
+ private static final long serialVersionUID = -5353814936444576118L;
88
+
89
+
90
+
91
+ public static void main(String[] args) {
92
+
93
+ new FlagFrame(new File("images"));
94
+
95
+ }
96
+
97
+
98
+
99
+ public FlagFrame(File folderPath) {
100
+
101
+
102
+
103
+ initLayout(folderPath);
104
+
105
+ }
106
+
107
+
108
+
109
+ private String cmd;
110
+
111
+
112
+
113
+ private void initLayout(File path) {
114
+
115
+
116
+
117
+ super.setLayout(new BorderLayout());
118
+
119
+
120
+
121
+ List<File> files = new ArrayList<File>();
122
+
123
+
124
+
125
+ for (File f : path.listFiles()) {
126
+
127
+ files.add(f);
128
+
129
+ }
130
+
131
+
132
+
133
+ final int BUTON_NUM = 8;
134
+
135
+ JPanel panel = new JPanel(new GridLayout((int)(files.size() / BUTON_NUM) + 1, BUTON_NUM));
136
+
137
+ Random random = new Random();
138
+
139
+
140
+
141
+ int max = files.size();
142
+
143
+ for (int i=0;i < max;i++) {
144
+
145
+ int r = random.nextInt(files.size());
146
+
147
+ File oFile = files.get(r);
148
+
149
+
150
+
151
+ String fileName = oFile.getName();
152
+
153
+ if (fileName.length() > 5) {
154
+
155
+ fileName = fileName.substring(0, 3) + "..." + fileName.substring(fileName.length() - 5);
156
+
157
+ }
158
+
159
+ JButton button = new JButton(fileName);
160
+
161
+ button.addActionListener(this);
162
+
163
+ button.setActionCommand(oFile.getAbsolutePath());
164
+
165
+ // button.setIgnoreRepaint(true);
166
+
167
+ button.setToolTipText(oFile.getAbsolutePath());
168
+
169
+ panel.add(button);
170
+
171
+ System.out.println(oFile.toString());
172
+
173
+
174
+
175
+ files.remove(r);
176
+
177
+ }
178
+
179
+ super.add(panel, BorderLayout.NORTH);
180
+
181
+
182
+
183
+ JPanel renderPanel = new JPanel() {
184
+
185
+
186
+
187
+ private static final long serialVersionUID = -3107296751372342247L;
188
+
189
+
190
+
191
+ @Override
192
+
193
+ public void paint(Graphics g) {
194
+
195
+
196
+
197
+ if (cmd != null) {
198
+
199
+ ImageIcon imageIcon = new ImageIcon(cmd);
200
+
201
+
202
+
203
+ System.out.println("render: " + cmd);
204
+
205
+
206
+
207
+ g.drawImage(imageIcon.getImage(), 100, 100, null);
208
+
209
+
210
+
211
+ cmd = null;
212
+
213
+
214
+
215
+ }
216
+
217
+ }
218
+
219
+ };
220
+
221
+ super.add(renderPanel, BorderLayout.CENTER);
222
+
223
+
224
+
225
+ super.setDefaultCloseOperation(EXIT_ON_CLOSE);
226
+
227
+ super.setSize(800, 600);
228
+
229
+ super.setVisible(true);
230
+
231
+ }
232
+
233
+
234
+
235
+ @Override
236
+
237
+ public void actionPerformed(ActionEvent e) {
238
+
239
+ JButton button = (JButton) e.getSource();
240
+
241
+ cmd = button.getActionCommand();
242
+
243
+ System.out.println("repaint target: " + cmd);
244
+
245
+ super.repaint();
246
+
247
+ }
248
+
249
+ }
250
+
251
+
252
+
253
+ ```
254
+
255
+
256
+
257
+ ソース見難いじゃねーの、とか言っておきながら自分のも見難いですね;;;反省。