teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

サンプル提供

2016/01/14 14:49

投稿

ipadcaron
ipadcaron

スコア1693

answer CHANGED
@@ -2,4 +2,128 @@
2
2
  わたしがアドバイスするとしたら、jbutton を継承した国旗クラスを用意して処理を纏める。
3
3
  座標とか指定せずに、レイアウトマネージャーのレイアウトに委ねる書き方に改める。
4
4
  今より倍くらい長いソースになっちゃうけど、処理単位が纏まるのと同じような記述がへるので
5
- 読みやすくなります。
5
+ 読みやすくなります。
6
+ ーーーーーーーー追記
7
+ ![イメージ説明](4d1a02540802e1b8921cc30655fc30f8.png)
8
+
9
+ ```java
10
+ package com.test.swing.util;
11
+
12
+ import java.awt.BorderLayout;
13
+ import java.awt.Graphics;
14
+ import java.awt.GridLayout;
15
+ import java.awt.event.ActionEvent;
16
+ import java.awt.event.ActionListener;
17
+ import java.io.File;
18
+ import java.util.ArrayList;
19
+ import java.util.List;
20
+ import java.util.Random;
21
+
22
+ import javax.swing.ImageIcon;
23
+ import javax.swing.JButton;
24
+ import javax.swing.JFrame;
25
+ import javax.swing.JPanel;
26
+
27
+ /**
28
+ * 画像ダウンロード先
29
+ *  http://www.irasutoya.com/2012/05/asia.html
30
+ *
31
+ * eclipseLuna 4.4 Java8 で動作確認済み
32
+ *
33
+ * images/ フォルダを作りそこに上記無料画像のHPからダウンロードした画像を格納してください。
34
+ *
35
+ * プログラムはimages/ フォルダにある画像ファイル分のボタンを生成、
36
+ * ボタン表示名に画像ファイル名を短縮した名称、ツールチップにフルパス、
37
+ * ボタンクリックすると画面中央にその国旗を表示します。
38
+ */
39
+ public class FlagFrame extends JFrame implements ActionListener {
40
+
41
+ /**
42
+ *
43
+ */
44
+ private static final long serialVersionUID = -5353814936444576118L;
45
+
46
+ public static void main(String[] args) {
47
+ new FlagFrame(new File("images"));
48
+ }
49
+
50
+ public FlagFrame(File folderPath) {
51
+
52
+ initLayout(folderPath);
53
+ }
54
+
55
+ private String cmd;
56
+
57
+ private void initLayout(File path) {
58
+
59
+ super.setLayout(new BorderLayout());
60
+
61
+ List<File> files = new ArrayList<File>();
62
+
63
+ for (File f : path.listFiles()) {
64
+ files.add(f);
65
+ }
66
+
67
+ final int BUTON_NUM = 8;
68
+ JPanel panel = new JPanel(new GridLayout((int)(files.size() / BUTON_NUM) + 1, BUTON_NUM));
69
+ Random random = new Random();
70
+
71
+ int max = files.size();
72
+ for (int i=0;i < max;i++) {
73
+ int r = random.nextInt(files.size());
74
+ File oFile = files.get(r);
75
+
76
+ String fileName = oFile.getName();
77
+ if (fileName.length() > 5) {
78
+ fileName = fileName.substring(0, 3) + "..." + fileName.substring(fileName.length() - 5);
79
+ }
80
+ JButton button = new JButton(fileName);
81
+ button.addActionListener(this);
82
+ button.setActionCommand(oFile.getAbsolutePath());
83
+ // button.setIgnoreRepaint(true);
84
+ button.setToolTipText(oFile.getAbsolutePath());
85
+ panel.add(button);
86
+ System.out.println(oFile.toString());
87
+
88
+ files.remove(r);
89
+ }
90
+ super.add(panel, BorderLayout.NORTH);
91
+
92
+ JPanel renderPanel = new JPanel() {
93
+
94
+ private static final long serialVersionUID = -3107296751372342247L;
95
+
96
+ @Override
97
+ public void paint(Graphics g) {
98
+
99
+ if (cmd != null) {
100
+ ImageIcon imageIcon = new ImageIcon(cmd);
101
+
102
+ System.out.println("render: " + cmd);
103
+
104
+ g.drawImage(imageIcon.getImage(), 100, 100, null);
105
+
106
+ cmd = null;
107
+
108
+ }
109
+ }
110
+ };
111
+ super.add(renderPanel, BorderLayout.CENTER);
112
+
113
+ super.setDefaultCloseOperation(EXIT_ON_CLOSE);
114
+ super.setSize(800, 600);
115
+ super.setVisible(true);
116
+ }
117
+
118
+ @Override
119
+ public void actionPerformed(ActionEvent e) {
120
+ JButton button = (JButton) e.getSource();
121
+ cmd = button.getActionCommand();
122
+ System.out.println("repaint target: " + cmd);
123
+ super.repaint();
124
+ }
125
+ }
126
+
127
+ ```
128
+
129
+ ソース見難いじゃねーの、とか言っておきながら自分のも見難いですね;;;反省。