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

質問編集履歴

3

一部ソース修正しました。

2020/12/16 09:27

投稿

Jhon_McClane
Jhon_McClane

スコア48

title CHANGED
File without changes
body CHANGED
@@ -111,52 +111,7 @@
111
111
 
112
112
  }
113
113
  ```
114
- ```ここに言語を入力
115
- package boundary;
116
114
 
117
- import java.awt.BorderLayout;
118
- import java.awt.Color;
119
- import java.awt.Dimension;
120
-
121
- import javax.swing.JFrame;
122
- import javax.swing.JPanel;
123
- import javax.swing.JScrollPane;
124
- import javax.swing.JViewport;
125
-
126
- public class ChatBoundary extends JFrame{
127
- Message[] message = new Message[3];
128
- //Message message = null;
129
- public static void main(String[] args) {
130
- ChatBoundary chatBoundary = new ChatBoundary("ChatBoundary");
131
- chatBoundary.setSize(new Dimension(2000,1400));
132
- //chatBoundary.setResizable(false);
133
- chatBoundary.setVisible(true);
134
- }
135
-
136
- ChatBoundary(String title){
137
- setTitle(title);
138
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
139
- //Container contentPane = getContentPane();
140
- message[0] = new Message("naruto.jpg");
141
- message[1] = new Message("コナン.jpg");
142
- JScrollPane scrollpane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
143
- scrollpane.setPreferredSize(new Dimension(1000,1200));
144
- scrollpane.setViewportView(message[0]);
145
- JViewport view = scrollpane.getViewport();
146
- view.setView(message[1]);
147
-      view.setViewPosition(new Point(100,1000));
148
-          //送信欄の枠
149
- JPanel jp = new JPanel();
150
- jp.setBackground(Color.blue);
151
- jp.setPreferredSize(new Dimension(100,130));
152
- add(scrollpane, BorderLayout.NORTH);
153
- add(jp, BorderLayout.SOUTH);
154
- pack();//フレームに入れた部品の大きさなどを考慮してフレームの適切なサイズを計算
155
- }
156
-
157
- }
158
-
159
- ```
160
115
  ```ここに言語を入力
161
116
  package boundary;
162
117
 

2

回答いただいたものを参考にソースを書き直しました。また完成図を追加しました。

2020/12/16 09:27

投稿

Jhon_McClane
Jhon_McClane

スコア48

title CHANGED
File without changes
body CHANGED
@@ -12,58 +12,89 @@
12
12
  ![イメージ説明](232808c7871cc4d77b0621bbd0355c5d.jpeg)
13
13
 
14
14
  ### 発生している問題
15
- パネルに後からパネル貼り付けて拡張るようなイメージで記述のですが,上手くきませんした.
15
+ 現段階では送信ボタンと,ッセージを出すようにているのですが,その描画が反映されな状況す。
16
- そもそも作方として合っているのか分からないため,なかなかほし情報がヒットしく手が止まっちゃっています.
16
+ また,送信ボタン周の操作クラスが再描画によってつぶれてしま,適切領域が割り当てられてなくスクロールもない状態にってしまっています
17
+ ChatBounddaryの継承をJPanelに変更したことで,add(scrollPane, BorderLayout.CENTER);を書く適切な位置がわからなくなってしまいました.
17
18
 
18
19
  ### 該当のソースコード
19
20
 
20
21
  ```ここに言語名を入力
21
22
  package boundary;
22
23
 
24
+ import java.awt.BorderLayout;
23
25
  import java.awt.Color;
24
26
  import java.awt.Dimension;
27
+ import java.awt.FlowLayout;
25
28
  import java.awt.Graphics;
29
+ import java.awt.event.ActionEvent;
30
+ import java.awt.event.ActionListener;
26
31
  import java.awt.event.MouseEvent;
27
32
  import java.awt.event.MouseListener;
33
+ import java.util.ArrayList;
28
34
 
29
35
  import javax.swing.ImageIcon;
30
36
  import javax.swing.JButton;
31
37
  import javax.swing.JLabel;
32
38
  import javax.swing.JPanel;
33
39
 
34
- public class Message extends JPanel implements MouseListener {
40
+ public class Message extends JPanel implements MouseListener, ActionListener {
35
- private JButton[] eBtn = new JButton[4];
41
+ private EmotionButton[] eb = new EmotionButton[4];
36
42
  private ImageIcon[] icon = new ImageIcon[4];
37
- private static final int WIDTH = 600;
43
+ //private static final int WIDTH = 900;
38
- private static final int HEIGHT = 900;
44
+ //private static final int HEIGHT = 900;
39
-
45
+ private JButton saveBtn;
46
+
47
+
40
- public Message(String imageName) {
48
+ public Message(String imageName,FlowLayout layout) {
41
- setPreferredSize(new Dimension(WIDTH, HEIGHT));
49
+ setLayout(layout);
42
- setBackground(Color.black);
50
+ setBackground(Color.WHITE);
51
+ //setPreferredSize(new Dimension(WIDTH, HEIGHT));
52
+ //setBackground(Color.black);
43
53
  ImageIcon image = new ImageIcon(imageName);
44
- JLabel jl = new JLabel(image);
54
+ JLabel jl = new JLabel(image,JLabel.CENTER);
45
55
  add(jl);
46
56
  for (int i = 0; i < 4; i++) {
47
57
  icon[i] = new ImageIcon("emotion" + i + ".png");
48
- eBtn[i] = new JButton("0", icon[i]);
58
+ eb[i] = new EmotionButton(icon[i]);
49
- eBtn[i].setPreferredSize(new Dimension(100, 100));
59
+ eb[i].setPreferredSize(new Dimension(100, 100));
50
- eBtn[i].addMouseListener(this);
60
+ eb[i].addMouseListener(this);
51
- add(eBtn[i]);
61
+ add(eb[i]);
52
62
  }
63
+ saveBtn = new JButton("保存");
64
+ saveBtn.addActionListener(this);
65
+ add(saveBtn, BorderLayout.EAST);
53
66
 
54
-
55
67
  }
56
68
 
57
69
  public void paintComponent(Graphics g) {
58
70
  super.paintComponent(g);
59
71
  }
60
72
 
73
+ public void actionPerformed(ActionEvent e) {
74
+ InformDialog.displayDialogText("保存します");
75
+ }
76
+
61
77
  public void mouseClicked(MouseEvent e) {
78
+ EmotionButton eBtn = (EmotionButton) e.getSource();
79
+ switch (e.getButton()) {
80
+ case MouseEvent.BUTTON1:
81
+ eBtn.setText(String.valueOf(eBtn.editBtn("wataru")));
82
+ break;
83
+ case MouseEvent.BUTTON3:
84
+ ArrayList<String> arrUN = eBtn.getUserName();
85
+ for (int i = 0; i < arrUN.size(); i++) {
86
+ System.out.println(arrUN.get(i));
87
+ }
88
+ if (arrUN.size() != 0) {
62
- InformDialog.displayDialogText("クリックしました。");
89
+ InformDialog.displayDialogText(arrUN.get(0));
90
+ }
91
+ break;
92
+ }
93
+
63
94
  }
64
95
 
65
96
  public void mouseEntered(MouseEvent e) {
66
- InformDialog.displayDialogText("ボタンに触れましました。");
97
+ //InformDialog.displayDialogText("ボタンに触れましました。");
67
98
  }
68
99
 
69
100
  public void mouseExited(MouseEvent e) {
@@ -126,11 +157,144 @@
126
157
  }
127
158
 
128
159
  ```
160
+ ```ここに言語を入力
161
+ package boundary;
129
162
 
130
- ### 試したこと
131
- panel以外でチャットの枠を構成できないか調べ,JListを使って出来るのかと今ネットを探っている段階です.
163
+ import javax.swing.JFrame;
132
164
 
165
+ public class Main extends JFrame{
166
+ public static void main(String[] args) {
167
+ Main frame = new Main();
168
+ frame.setTitle("画面遷移テスト");
169
+ frame.setSize(2200, 1400);
170
+ frame.setLocationRelativeTo(null);
171
+ ChatBoundary chatBoundary = new ChatBoundary();
172
+ frame.getContentPane().add(chatBoundary);
173
+ frame.setVisible(true);
174
+ }
175
+ }
176
+ ```
177
+ ```ここに言語を入力
178
+ package boundary;
133
179
 
180
+ import java.awt.BorderLayout;
181
+ import java.awt.Color;
182
+ import java.awt.Dimension;
183
+ import java.awt.FlowLayout;
184
+ import java.awt.event.AdjustmentEvent;
185
+ import java.awt.event.AdjustmentListener;
186
+
187
+ import javax.swing.BorderFactory;
188
+ import javax.swing.Box;
189
+ import javax.swing.JButton;
190
+ import javax.swing.JPanel;
191
+ import javax.swing.JScrollPane;
192
+ import javax.swing.JTextField;
193
+
194
+ public class ChatBoundary extends JPanel{
195
+ private static final FlowLayout right = new FlowLayout(FlowLayout.RIGHT);
196
+ private static final FlowLayout left = new FlowLayout(FlowLayout.LEFT);
197
+
198
+
199
+ public ChatBoundary(){
200
+ setLayout(new BorderLayout());
201
+ setPreferredSize(new Dimension(2200, 1200));
202
+ setBackground(Color.black);
203
+ // メッセージが追加されていく本体
204
+ Box inner = Box.createVerticalBox();
205
+ inner.setBorder(BorderFactory.createLineBorder(Color.RED, 5));
206
+
207
+ //JPanel outer = new JPanel(new BorderLayout());
208
+ //outer.add(inner, BorderLayout.NORTH);
209
+ //outer.setBorder(BorderFactory.createLineBorder(Color.GREEN, 5));
210
+
211
+ add(inner, BorderLayout.NORTH);
212
+ setBorder(BorderFactory.createLineBorder(Color.GREEN, 5));
213
+
214
+ //JScrollPane scrollPane = new JScrollPane(outer);
215
+
216
+ JScrollPane scrollPane = new JScrollPane(this);
217
+
218
+
219
+ //add(scrollPane, BorderLayout.CENTER);
220
+ scrollPane.setBorder(BorderFactory.createLineBorder(Color.BLUE, 5));
221
+
222
+ scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
223
+ private int max = scrollPane.getVerticalScrollBar().getMaximum();
224
+
225
+ public void adjustmentValueChanged(AdjustmentEvent e) {
226
+ if (max - e.getAdjustable().getMaximum() == 0) return;
227
+
228
+ e.getAdjustable().setValue(e.getAdjustable().getMaximum());
229
+ max = scrollPane.getVerticalScrollBar().getMaximum();
230
+ }
231
+ });
232
+
233
+ JPanel controls = new JPanel(); // 操作エリア
234
+ add(controls, BorderLayout.SOUTH);
235
+
236
+ JTextField textField = new JTextField("メッセージ", 20);
237
+ controls.add(textField);
238
+ textField.selectAll(); // 全選択
239
+
240
+ JButton button = new JButton("送信");
241
+ controls.add(button);
242
+ button.addActionListener(e -> {
243
+ inner.add(new Message("naruto.jpg", right));
244
+ inner.add(Box.createVerticalStrut(10)); // 隙間
245
+ inner.add(new Message("naruto.jpg", left));
246
+ inner.add(Box.createVerticalStrut(10)); // 隙間
247
+
248
+ scrollPane.revalidate(); // 再描画
249
+
250
+ textField.selectAll(); // 全選択
251
+ textField.requestFocus(); // フォーカス
252
+ });
253
+
254
+ textField.addActionListener(e -> button.doClick());
255
+
256
+
257
+ }
258
+ }
259
+
260
+ ```
261
+ ```ここに言語を入力
262
+ package boundary;
263
+
264
+ import java.util.ArrayList;
265
+
266
+ import javax.swing.ImageIcon;
267
+ import javax.swing.JButton;
268
+
269
+ //メッセージのエモーションにかかわるボタンの処理クラス
270
+ class EmotionButton extends JButton {
271
+ private ArrayList<String> arrL = new ArrayList<String>();
272
+
273
+ public EmotionButton(ImageIcon icon) {
274
+ super("0", icon);
275
+ }
276
+
277
+ public int editBtn(String userName) {
278
+ if(arrL.contains(userName)) {
279
+ arrL.remove(arrL.indexOf(userName));
280
+ return arrL.size();
281
+ }
282
+ else {
283
+ arrL.add(userName);
284
+ return arrL.size();
285
+ }
286
+ }
287
+
288
+ public ArrayList<String> getUserName() {
289
+ return arrL;
290
+ }
291
+
292
+ }
293
+
294
+ ```
295
+
296
+
297
+
134
298
  ### 補足情報(FW/ツールのバージョンなど)
135
299
  eclipse 2020/12/9にダウンロードしたため最新だと思います.
136
300
  java8です.

1

完成したイメージの図を新たに付け加えた。

2020/12/16 09:25

投稿

Jhon_McClane
Jhon_McClane

スコア48

title CHANGED
File without changes
body CHANGED
@@ -8,6 +8,8 @@
8
8
  JPanelに画像とボタン数種類を付加したものを一つのメッセージとしてパネル単位でメッセージ枠を作り、JScrollPaneクラスで実現しようと考えています.
9
9
  今のところ,「JScrollPaneクラスは単独で用いるものではなく他のコンポーネントにスクロール機能を提供するもの」ということは分かりました.
10
10
 
11
+ 完成イメージです.
12
+ ![イメージ説明](232808c7871cc4d77b0621bbd0355c5d.jpeg)
11
13
 
12
14
  ### 発生している問題
13
15
  パネルに後からパネルを貼り付けて拡張するようなイメージで記述したのですが,上手くいきませんでした.