回答編集履歴

2

コードのゴミを削除

2021/09/22 07:26

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -190,7 +190,7 @@
190
190
 
191
191
 
192
192
 
193
- private JLabel messageLabel = new JLabel("数値を入力してください");;
193
+ private JLabel messageLabel = new JLabel("数値を入力してください");
194
194
 
195
195
  private ChangeListener listener;
196
196
 

1

コード追加

2021/09/22 07:26

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -11,3 +11,349 @@
11
11
  java でウインドウを使ってどうこうするモノを作ることは、 System.out.println や Scanner を使ったコマンド型のプログラムよりも難易度が格段に違います。
12
12
 
13
13
  大変失礼ですが、このエラーで戸惑われているようでは『まだ早いのでは』と思わざるを得ません。
14
+
15
+
16
+
17
+ ----
18
+
19
+
20
+
21
+ GUI で完結するように作ってみました。
22
+
23
+
24
+
25
+ LineGraph.java
26
+
27
+ ```java
28
+
29
+ package teratail_java.q360628;
30
+
31
+
32
+
33
+ import java.awt.*;
34
+
35
+ import java.awt.event.*;
36
+
37
+
38
+
39
+ import javax.swing.*;
40
+
41
+ import javax.swing.event.DocumentEvent;
42
+
43
+ import javax.swing.event.DocumentListener;
44
+
45
+
46
+
47
+ public class LineGraph extends JFrame {
48
+
49
+
50
+
51
+ public static void main(String args[]) {
52
+
53
+ new LineGraph().setVisible(true);
54
+
55
+ }
56
+
57
+
58
+
59
+ private static final int DATASIZE = 6;
60
+
61
+
62
+
63
+ LineGraph() {
64
+
65
+ super("LineGraph");
66
+
67
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
68
+
69
+
70
+
71
+ LineGraphPanel graphPanel = new LineGraphPanel(DATASIZE);
72
+
73
+ DataPanel dataPanel = new DataPanel(DATASIZE, new DataPanel.ChangeListener() {
74
+
75
+ @Override
76
+
77
+ public void changed(int index, int value) {
78
+
79
+ graphPanel.setData(index, value);
80
+
81
+ }
82
+
83
+ });
84
+
85
+
86
+
87
+ add(graphPanel, BorderLayout.CENTER);
88
+
89
+ add(dataPanel, BorderLayout.NORTH);
90
+
91
+ pack();
92
+
93
+ }
94
+
95
+
96
+
97
+ private static class LineGraphPanel extends JPanel {
98
+
99
+ private int data[];
100
+
101
+
102
+
103
+ LineGraphPanel(int datasize) {
104
+
105
+ setPreferredSize(new Dimension(300, 300));
106
+
107
+ setMinimumSize(getPreferredSize());
108
+
109
+
110
+
111
+ data = new int[datasize];
112
+
113
+ }
114
+
115
+
116
+
117
+ void setData(int index, int value) {
118
+
119
+ data[index] = value;
120
+
121
+ repaint();
122
+
123
+ }
124
+
125
+
126
+
127
+ public void paint(Graphics g) {
128
+
129
+ super.paint(g);
130
+
131
+
132
+
133
+ //Xスケール
134
+
135
+ g.drawLine( 50, 250, 250, 250);
136
+
137
+ for (int i=0; i<data.length; i++) {
138
+
139
+ String s = String.format("%3d", i);
140
+
141
+ g.drawString(s, i * 20 + 40, 270);
142
+
143
+ }
144
+
145
+
146
+
147
+ //Yスケール
148
+
149
+ g.drawLine( 50, 40, 50, 250);
150
+
151
+ for (int i=0; i<=10; i++) {
152
+
153
+ String s = String.format("%3d", i * 10);
154
+
155
+ g.drawString(s, 28, 250 - i * 20);
156
+
157
+ }
158
+
159
+
160
+
161
+ //グラフ
162
+
163
+ for (int i=0; i<data.length-1; i++) {
164
+
165
+ int x1 = i * 20 + 50;
166
+
167
+ int x2 = (i + 1) * 20 + 50;
168
+
169
+ int y1 = 250 - data[i] * 2;
170
+
171
+ int y2 = 250 - data[i+1] * 2;
172
+
173
+ g.drawLine( x1, y1, x2, y2);
174
+
175
+ }
176
+
177
+ }
178
+
179
+ }
180
+
181
+
182
+
183
+ private static class DataPanel extends JPanel {
184
+
185
+ interface ChangeListener {
186
+
187
+ void changed(int index, int value);
188
+
189
+ }
190
+
191
+
192
+
193
+ private JLabel messageLabel = new JLabel("数値を入力してください");;
194
+
195
+ private ChangeListener listener;
196
+
197
+
198
+
199
+ DataPanel(int size, ChangeListener listener) {
200
+
201
+ super(new BorderLayout());
202
+
203
+ this.listener = listener;
204
+
205
+
206
+
207
+ add(messageLabel, BorderLayout.NORTH);
208
+
209
+
210
+
211
+ JPanel inputPanel = new JPanel(new FlowLayout());
212
+
213
+ for(int i=0; i<size; i++) {
214
+
215
+ inputPanel.add(new LabelAndInputField(i, 0));
216
+
217
+ }
218
+
219
+ add(inputPanel, BorderLayout.CENTER);
220
+
221
+
222
+
223
+ setNormalState();
224
+
225
+ }
226
+
227
+
228
+
229
+ private void setNormalState() {
230
+
231
+ messageLabel.setForeground(Color.BLACK);
232
+
233
+ }
234
+
235
+
236
+
237
+ private void setErrorState() {
238
+
239
+ messageLabel.setForeground(Color.RED);
240
+
241
+ }
242
+
243
+
244
+
245
+ private class LabelAndInputField extends JPanel implements ActionListener, DocumentListener, FocusListener {
246
+
247
+ private int number;
248
+
249
+ private JLabel label;
250
+
251
+ private JTextField field;
252
+
253
+
254
+
255
+ LabelAndInputField(int number, int initialValue) {
256
+
257
+ super(new BorderLayout());
258
+
259
+ this.number = number;
260
+
261
+
262
+
263
+ field = new JTextField(""+initialValue, 3);
264
+
265
+ field.addActionListener(this);
266
+
267
+ field.getDocument().addDocumentListener(this);
268
+
269
+ field.addFocusListener(this);
270
+
271
+
272
+
273
+ label = new JLabel(number+":");
274
+
275
+
276
+
277
+ add(label, BorderLayout.WEST);
278
+
279
+ add(field, BorderLayout.CENTER);
280
+
281
+
282
+
283
+ setNormalState();
284
+
285
+ }
286
+
287
+
288
+
289
+ private void setNormalState() {
290
+
291
+ label.setForeground(Color.BLACK);
292
+
293
+ DataPanel.this.setNormalState();
294
+
295
+ }
296
+
297
+
298
+
299
+ private void setErrorState() {
300
+
301
+ label.setForeground(Color.RED);
302
+
303
+ DataPanel.this.setErrorState();
304
+
305
+ }
306
+
307
+
308
+
309
+ @Override
310
+
311
+ public void actionPerformed(ActionEvent ae) {
312
+
313
+ try {
314
+
315
+ int data = Integer.parseInt(field.getText());
316
+
317
+ listener.changed(number, data);
318
+
319
+ } catch(NumberFormatException e) {
320
+
321
+ setErrorState();
322
+
323
+ field.requestFocus();
324
+
325
+ }
326
+
327
+ }
328
+
329
+
330
+
331
+ @Override
332
+
333
+ public void removeUpdate(DocumentEvent e) { setNormalState(); }
334
+
335
+ @Override
336
+
337
+ public void insertUpdate(DocumentEvent e) { setNormalState(); }
338
+
339
+ @Override
340
+
341
+ public void changedUpdate(DocumentEvent e) { setNormalState(); }
342
+
343
+
344
+
345
+ @Override
346
+
347
+ public void focusLost(FocusEvent e) { actionPerformed(null); }
348
+
349
+ @Override
350
+
351
+ public void focusGained(FocusEvent e) {} //ignore
352
+
353
+ }
354
+
355
+ }
356
+
357
+ }
358
+
359
+ ```