回答編集履歴

3

コードバグ修正、ついでにスライダーにラベル追加

2021/10/31 11:47

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -60,9 +60,9 @@
60
60
 
61
61
  @Override
62
62
 
63
- public void onChange(int red, int blue, int green) {
63
+ public void onChange(int red, int green, int blue) {
64
-
64
+
65
- rgbTextField.setText("red="+red+", blue="+blue+", green="+green);
65
+ rgbTextField.setText("red="+red+", green="+green+", blue="+blue);
66
66
 
67
67
  }
68
68
 
@@ -88,13 +88,13 @@
88
88
 
89
89
  interface Listener {
90
90
 
91
- void onChange(int red, int blue, int green);
91
+ void onChange(int red, int green, int blue);
92
92
 
93
93
  }
94
94
 
95
95
  private Listener listener;
96
96
 
97
- private int red, blue, green;
97
+ private int red, green, blue;
98
98
 
99
99
 
100
100
 
@@ -106,6 +106,8 @@
106
106
 
107
107
 
108
108
 
109
+ JLabel redLabel = new JLabel("R");
110
+
109
111
  JSlider redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
110
112
 
111
113
  redSlider.addChangeListener(new ChangeListener() {
@@ -116,13 +118,15 @@
116
118
 
117
119
  red = ((JSlider)e.getSource()).getValue();
118
120
 
119
- if(listener != null) listener.onChange(red, blue, green);
121
+ if(listener != null) listener.onChange(red, green, blue);
120
-
122
+
121
- }
123
+ }
122
-
124
+
123
- });
125
+ });
126
+
127
+
128
+
124
-
129
+ JLabel greenLabel = new JLabel("G");
125
-
126
130
 
127
131
  JSlider greenSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
128
132
 
@@ -132,27 +136,29 @@
132
136
 
133
137
  public void stateChanged(ChangeEvent e) {
134
138
 
139
+ green = ((JSlider)e.getSource()).getValue();
140
+
141
+ if(listener != null) listener.onChange(red, green, blue);
142
+
143
+ }
144
+
145
+ });
146
+
147
+
148
+
149
+ JLabel blueLabel = new JLabel("B");
150
+
151
+ JSlider blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
152
+
153
+ blueSlider.addChangeListener(new ChangeListener() {
154
+
155
+ @Override
156
+
157
+ public void stateChanged(ChangeEvent e) {
158
+
135
159
  blue = ((JSlider)e.getSource()).getValue();
136
160
 
137
- if(listener != null) listener.onChange(red, blue, green);
161
+ if(listener != null) listener.onChange(red, green, blue);
138
-
139
- }
140
-
141
- });
142
-
143
-
144
-
145
- JSlider blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
146
-
147
- blueSlider.addChangeListener(new ChangeListener() {
148
-
149
- @Override
150
-
151
- public void stateChanged(ChangeEvent e) {
152
-
153
- green = ((JSlider)e.getSource()).getValue();
154
-
155
- if(listener != null) listener.onChange(red, blue, green);
156
162
 
157
163
  }
158
164
 
@@ -164,16 +170,50 @@
164
170
 
165
171
  gbc.gridx = 0;
166
172
 
173
+ gbc.gridy = 0;
174
+
175
+ add(redLabel, gbc);
176
+
177
+ gbc.gridx ++;
178
+
179
+ add(Box.createHorizontalStrut(5), gbc);
180
+
181
+ gbc.gridx ++;
182
+
167
183
  add(redSlider, gbc);
168
184
 
185
+ gbc.gridx = 0;
186
+
187
+ gbc.gridy ++;
188
+
169
189
  add(Box.createVerticalStrut(10), gbc);
170
190
 
191
+ gbc.gridx = 0;
192
+
193
+ gbc.gridy ++;
194
+
195
+ add(greenLabel, gbc);
196
+
197
+ gbc.gridx = 2;
198
+
199
+ add(greenSlider, gbc);
200
+
201
+ gbc.gridx = 0;
202
+
203
+ gbc.gridy ++;
204
+
205
+ add(Box.createVerticalStrut(10), gbc);
206
+
207
+ gbc.gridx = 0;
208
+
209
+ gbc.gridy ++;
210
+
211
+ add(blueLabel, gbc);
212
+
213
+ gbc.gridx = 2;
214
+
171
215
  add(blueSlider, gbc);
172
216
 
173
- add(Box.createVerticalStrut(10), gbc);
174
-
175
- add(greenSlider, gbc);
176
-
177
217
  }
178
218
 
179
219
 
@@ -182,7 +222,7 @@
182
222
 
183
223
  this.listener = listener;
184
224
 
185
- if(listener != null) listener.onChange(red, green, blue); //初期表示用
225
+ if(listener!= null) listener.onChange(red, green, blue); //初期表示用
186
226
 
187
227
  }
188
228
 

2

コード修正

2021/10/31 11:47

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -182,7 +182,7 @@
182
182
 
183
183
  this.listener = listener;
184
184
 
185
- listener.onChange(red, green, blue); //初期表示用
185
+ if(listener != null) listener.onChange(red, green, blue); //初期表示用
186
186
 
187
187
  }
188
188
 

1

コード追加

2021/10/31 11:38

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -3,3 +3,189 @@
3
3
  「コンテナにウィンドウを追加する」
4
4
 
5
5
  by Google翻訳
6
+
7
+
8
+
9
+ ----
10
+
11
+
12
+
13
+ スライダー3つでRGBを選ぶパネルなら、こんな感じでしょうか。
14
+
15
+ ```java
16
+
17
+ package teratail_java.q367113;
18
+
19
+
20
+
21
+ import java.awt.*;
22
+
23
+
24
+
25
+ import javax.swing.*;
26
+
27
+ import javax.swing.event.ChangeEvent;
28
+
29
+ import javax.swing.event.ChangeListener;
30
+
31
+
32
+
33
+ public class MainFrame extends JFrame {
34
+
35
+ public static void main(String[] args) {
36
+
37
+ new MainFrame().setVisible(true);
38
+
39
+ }
40
+
41
+
42
+
43
+ MainFrame() {
44
+
45
+ super();
46
+
47
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48
+
49
+
50
+
51
+ JTextField rgbTextField = new JTextField();
52
+
53
+ rgbTextField.setEditable(false);
54
+
55
+
56
+
57
+ RGBSliderPanel rgbSliderPanel = new RGBSliderPanel();
58
+
59
+ rgbSliderPanel.setChangeListener(new RGBSliderPanel.Listener() {
60
+
61
+ @Override
62
+
63
+ public void onChange(int red, int blue, int green) {
64
+
65
+ rgbTextField.setText("red="+red+", blue="+blue+", green="+green);
66
+
67
+ }
68
+
69
+ });
70
+
71
+
72
+
73
+ add(rgbTextField, BorderLayout.NORTH);
74
+
75
+ add(rgbSliderPanel, BorderLayout.CENTER);
76
+
77
+
78
+
79
+ pack();
80
+
81
+ }
82
+
83
+ }
84
+
85
+
86
+
87
+ class RGBSliderPanel extends JPanel {
88
+
89
+ interface Listener {
90
+
91
+ void onChange(int red, int blue, int green);
92
+
93
+ }
94
+
95
+ private Listener listener;
96
+
97
+ private int red, blue, green;
98
+
99
+
100
+
101
+ RGBSliderPanel() {
102
+
103
+ super(new GridBagLayout());
104
+
105
+ setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
106
+
107
+
108
+
109
+ JSlider redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
110
+
111
+ redSlider.addChangeListener(new ChangeListener() {
112
+
113
+ @Override
114
+
115
+ public void stateChanged(ChangeEvent e) {
116
+
117
+ red = ((JSlider)e.getSource()).getValue();
118
+
119
+ if(listener != null) listener.onChange(red, blue, green);
120
+
121
+ }
122
+
123
+ });
124
+
125
+
126
+
127
+ JSlider greenSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
128
+
129
+ greenSlider.addChangeListener(new ChangeListener() {
130
+
131
+ @Override
132
+
133
+ public void stateChanged(ChangeEvent e) {
134
+
135
+ blue = ((JSlider)e.getSource()).getValue();
136
+
137
+ if(listener != null) listener.onChange(red, blue, green);
138
+
139
+ }
140
+
141
+ });
142
+
143
+
144
+
145
+ JSlider blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
146
+
147
+ blueSlider.addChangeListener(new ChangeListener() {
148
+
149
+ @Override
150
+
151
+ public void stateChanged(ChangeEvent e) {
152
+
153
+ green = ((JSlider)e.getSource()).getValue();
154
+
155
+ if(listener != null) listener.onChange(red, blue, green);
156
+
157
+ }
158
+
159
+ });
160
+
161
+
162
+
163
+ GridBagConstraints gbc = new GridBagConstraints();
164
+
165
+ gbc.gridx = 0;
166
+
167
+ add(redSlider, gbc);
168
+
169
+ add(Box.createVerticalStrut(10), gbc);
170
+
171
+ add(blueSlider, gbc);
172
+
173
+ add(Box.createVerticalStrut(10), gbc);
174
+
175
+ add(greenSlider, gbc);
176
+
177
+ }
178
+
179
+
180
+
181
+ void setChangeListener(Listener listener) {
182
+
183
+ this.listener = listener;
184
+
185
+ listener.onChange(red, green, blue); //初期表示用
186
+
187
+ }
188
+
189
+ }
190
+
191
+ ```