質問編集履歴

2

ソースコードを追記

2018/09/23 11:47

投稿

puroguraminng
puroguraminng

スコア21

test CHANGED
File without changes
test CHANGED
@@ -8,4 +8,304 @@
8
8
 
9
9
  setTextを試してみたのですがコンソールこのような文字列が表示されました。![イメージ説明](21d8f718e5c755261666b091f2d4a199.png)
10
10
 
11
+ ソースコードは...
12
+
13
+ part18.java
14
+
15
+ ```java
16
+
17
+ public class part18
18
+
19
+ {
20
+
21
+ public static void main(String[] args) throws java.io.IOException
22
+
23
+ {
24
+
25
+ new MyFrame();
26
+
27
+ }
28
+
29
+ }
30
+
31
+ ```
32
+
33
+
34
+
35
+ MyFrame.java
36
+
37
+ ```java
38
+
39
+ import java.awt.Color;
40
+
41
+ import java.awt.Font;
42
+
43
+ import java.awt.Image;
44
+
45
+ import java.awt.image.BufferedImage;
46
+
47
+
48
+
49
+ import javax.swing.ImageIcon;
50
+
51
+ import javax.swing.JFrame;
52
+
53
+ import javax.swing.JLabel;
54
+
55
+ import javax.swing.JPanel;
56
+
57
+
58
+
59
+ public class MyFrame extends JFrame{
60
+
61
+ JLabel jl;
62
+
63
+ public MyFrame() throws java.io.IOException{
64
+
65
+ setBounds( 975, 150, 940, 540); // フレームサイズを設定
66
+
67
+ setDefaultCloseOperation( EXIT_ON_CLOSE); // 閉じるボタン有効化
68
+
69
+ add( createBasePanel() ); // 親パネルを作成し、フレームに貼り付け
70
+
71
+ setVisible(true); // フレームを表示状態へ
72
+
73
+
74
+
75
+ enableEvents( java.awt.AWTEvent.KEY_EVENT_MASK );
76
+
77
+ }
78
+
79
+
80
+
81
+ protected void processKeyEvent( java.awt.event.KeyEvent e ) {
82
+
83
+ if( e.getID() == java.awt.event.KeyEvent.KEY_PRESSED) {
84
+
85
+ System.out.println( "キー" + e.getKeyCode() + "が、押されたよ");
86
+
87
+ jl.setText("キーがおされた");
88
+
89
+ if( e.getKeyCode() == java.awt.event.KeyEvent.VK_SPACE) {
90
+
91
+ System.out.println("スペースキーですね");
92
+
93
+ }
94
+
95
+ }
96
+
97
+
98
+
99
+ }
100
+
101
+ /**
102
+
103
+ * 基盤パネルを作成
104
+
105
+ */
106
+
107
+ JPanel createBasePanel() throws java.io.IOException{
108
+
109
+ JPanel jp = createPanel(Color.BLACK );
110
+
111
+ // フォントを作成
112
+
113
+ Font f = new Font( Font.MONOSPACED,
114
+
115
+ Font.BOLD,
116
+
117
+ 50 );
118
+
119
+
120
+
121
+ // レイアウト1行目
122
+
123
+ // パネルを作成し、親パネルに貼り付け
124
+
125
+ JPanel jpChild = createPanel(Color.BLUE );
126
+
127
+ jp.add( jpChild );
128
+
129
+ // ラベルを作成し、子パネルに貼り付け
130
+
131
+ JLabel jlChild = new JLabel( Player.getStatusString() );
132
+
133
+ jlChild.setFont( f );
134
+
135
+ jlChild.setForeground( Color.WHITE );
136
+
137
+ jpChild.add( jlChild );
138
+
139
+
140
+
141
+ // 画像つきラベルを作成し、親パネルに貼り付け
142
+
143
+ //java.awt.image.BufferedImage ii
144
+
145
+ //= new javax.swing.ImageIcon("Character.jpg");
146
+
147
+
148
+
149
+ // レイアウト2行目
150
+
151
+ // パネルを作成し、親パネルに貼り付け
152
+
153
+ JPanel jpChild2 = createPanel(Color.CYAN );
154
+
155
+ jp.add( jpChild2 );
156
+
157
+ // 選択肢をパネルに貼り付け
158
+
159
+ JLabel jlMain = new JLabel("<html>1.魔王を倒しに行く<br>2.修行する<br>3.宿屋に泊まる");
160
+
161
+ jlMain.setFont( f );
162
+
163
+ jpChild2.add( jlMain );
164
+
165
+ /*
166
+
167
+ // モンスターをパネルに貼り付け
168
+
169
+ File file = new File("Character.jpg"); // ファイルを開く
170
+
171
+ BufferedImage ba = ImageIO.read( file );// 画像全体を読み込む
172
+
173
+
174
+
175
+ int cw = 48; // セルサイズ・幅
176
+
177
+ int ch = 48; // セルサイズ・高さ
178
+
179
+ int sw = cw*4; // 表示サイズ・幅
180
+
181
+ int sh = ch*4; // 表示サイズ・高さ
182
+
183
+
184
+
185
+ putMonster( jpChild2, ba, 3, 0, cw, ch, sw, sh); // モンスター 3-0 表示
186
+
187
+ putMonster( jpChild2, ba, 9, 0, cw, ch, sw, sh); // モンスター 9-0 表示
188
+
189
+ putMonster( jpChild2, ba, 0, 0, cw, ch, sw, sh); // モンスター 0-0 表示
190
+
191
+ putMonster( jpChild2, ba, 0, 4, cw, ch, sw, sh); // モンスター 0-4 表示
192
+
193
+ */
194
+
195
+
196
+
197
+ // レイアウト3行目
198
+
199
+ // ラベルを作成し、親パネルに貼り付け
200
+
201
+ JLabel jl = new JLabel();
202
+
203
+ jl.setText("魔王が世界を滅ぼそうとしています");
204
+
205
+ jl.setFont( f );
206
+
207
+ jl.setForeground( new Color( 255, 128, 0) );
208
+
209
+ jp.add( jl );
210
+
211
+
212
+
213
+ return( jp );
214
+
215
+ }
216
+
217
+
218
+
219
+ /**
220
+
221
+ * パネルを作成し、背景色を設定します。
222
+
223
+ */
224
+
225
+ static JPanel createPanel ( Color c ){
226
+
227
+ JPanel jp = new JPanel();
228
+
229
+ jp.setBackground( c );
230
+
231
+ return( jp );
232
+
233
+ }
234
+
235
+ /**
236
+
237
+ * モンスターを表示します。
238
+
239
+ *
240
+
241
+ * @param jpChild2 表示対象パネル
242
+
243
+ * @param ba 画像全体
244
+
245
+ * @param x 表示するセル座標X
246
+
247
+ * @param y 表示するセル座標Y
248
+
249
+ * @param cx セルの大きさ・幅
250
+
251
+ * @param cy セルの大きさ・高さ
252
+
253
+ * @param sw 表示サイズ・幅
254
+
255
+ * @param sh 表示サイズ・高さ
256
+
257
+ */
258
+
259
+ static void putMonster( JPanel jpChild2, BufferedImage ba, int x, int y, int cw, int ch, int sw, int sh)
260
+
261
+ {
262
+
263
+ BufferedImage bs = ba.getSubimage( cw*x, ch*y, cw, ch );
264
+
265
+ Image im = bs.getScaledInstance( sw, sh, Image.SCALE_DEFAULT);
266
+
267
+ ImageIcon ii = new ImageIcon( im ); // JLabel用に型変換
268
+
269
+ JLabel jlImage = new JLabel( ii ); // 画像部品を作成
270
+
271
+ jlImage.setSize(cw, ch); // 画像部品サイズを設定
272
+
273
+ jpChild2.add( jlImage ); // 画像を貼り付け
274
+
275
+ }
276
+
277
+ }
278
+
279
+ ```
280
+
281
+ Player.java
282
+
283
+ ```java
284
+
285
+
286
+
287
+ public class Player {
288
+
289
+ static String name = "すけさん";
290
+
291
+ static int lv = 30; //プレイヤーのレベル
292
+
293
+ static int hp = 30; //プレイヤーのHP
294
+
295
+ static double gold=50.0; //プレイヤーの所持金
296
+
297
+
298
+
299
+ public static String getStatusString()
300
+
301
+ {
302
+
303
+ return(" "+name + " LV" + lv + " HP" + hp + " "+gold+"G");
304
+
305
+ }
306
+
307
+ }
308
+
309
+ ```
310
+
11
311
  環境はjava8 windows8.1です。

1

環境について追記しました

2018/09/23 11:47

投稿

puroguraminng
puroguraminng

スコア21

test CHANGED
File without changes
test CHANGED
@@ -7,3 +7,5 @@
7
7
  JLabel作ってJPanelにaddすれば、設定できるのはわかるんですが、addしておいて書き換える方法を知りたいです。
8
8
 
9
9
  setTextを試してみたのですがコンソールこのような文字列が表示されました。![イメージ説明](21d8f718e5c755261666b091f2d4a199.png)
10
+
11
+ 環境はjava8 windows8.1です。