回答編集履歴

3

IDEに言われるまま変更したが意味が異なる

2024/08/02 04:03

投稿

TN8001
TN8001

スコア9807

test CHANGED
@@ -85,7 +85,7 @@
85
85
  var pp = SwingUtilities.convertPoint(this, e.getPoint(), child);
86
86
 
87
87
  // 捏造したイベントを送る
88
- child.dispatchEvent(new MouseEvent(child, e.getID(), e.getWhen(), e.getModifiersEx(),
88
+ child.dispatchEvent(new MouseEvent(child, e.getID(), e.getWhen(), e.getModifiers(),
89
89
  pp.x, pp.y, e.getClickCount(), e.isPopupTrigger()));
90
90
  }
91
91
  }

2

複数ラベル

2024/08/01 10:37

投稿

TN8001
TN8001

スコア9807

test CHANGED
@@ -6,15 +6,14 @@
6
6
  ```java
7
7
  import javax.swing.*;
8
8
  import java.awt.*;
9
- import java.awt.event.MouseEvent;
9
+ import java.awt.event.*;
10
- import java.awt.event.MouseListener;
11
10
 
12
11
  public class Test {
13
12
  public static void main(String[] args) {
14
13
  JFrame window = new JFrame("test");
14
+ window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15
15
  window.setSize(300, 200);
16
16
  window.setLocationRelativeTo(null);
17
- window.setVisible(true);
18
17
 
19
18
  JPanel panel_1 = new JPanel(null); // nullレイアウトじゃないとラベルのsetBoundsが意味ない
20
19
  panel_1.setBackground(Color.GRAY);
@@ -24,12 +23,21 @@
24
23
  label_1.setBounds(0, 0, 300, 200);
25
24
  panel_1.add(label_1);
26
25
 
27
- ClickLabel label_2 = new ClickLabel();
26
+ ClickLabel label_2 = new ClickLabel("Click!!");
28
27
  label_2.setBounds(90, 55, 100, 50);
29
28
  panel_1.add(label_2);
30
29
 
30
+ for (int i = 0; i < 5; i++) {
31
+ ClickLabel label = new ClickLabel("" + i);
32
+ label.setBounds((i % 3) * 80 + 35, (i / 3) * 80 + 35, 50, 50);
33
+ label.setBackground(Color.BLUE);
34
+ panel_1.add(label);
35
+ }
36
+
31
37
  JLabelThread thread = new JLabelThread(label_1);
32
38
  thread.start();
39
+
40
+ window.setVisible(true);
33
41
  }
34
42
  }
35
43
 
@@ -88,20 +96,16 @@
88
96
  @Override public void mouseExited(MouseEvent e) { }
89
97
  }
90
98
 
91
- class ClickLabel extends JLabel implements MouseListener {
99
+ class ClickLabel extends JLabel {
92
- ClickLabel() {
100
+ ClickLabel(String text) {
93
- addMouseListener(this);
101
+ addMouseListener(new MouseAdapter() {
102
+ @Override public void mouseClicked(MouseEvent e) {
103
+ System.out.println(text);
104
+ }
105
+ });
94
106
  setOpaque(true);
95
107
  setBackground(Color.RED);
96
108
  }
97
-
98
- @Override public void mouseClicked(MouseEvent e) {
99
- System.out.println("Click!!");
100
- }
101
- @Override public void mousePressed(MouseEvent e) { }
102
- @Override public void mouseReleased(MouseEvent e) { }
103
- @Override public void mouseEntered(MouseEvent e) { }
104
- @Override public void mouseExited(MouseEvent e) { }
105
109
  }
106
110
 
107
111
  class JLabelThread extends Thread {
@@ -123,6 +127,7 @@
123
127
  [SwingUtilities#convertPoint (Java Platform SE 8 )](https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/SwingUtilities.html#convertPoint-java.awt.Component-java.awt.Point-java.awt.Component-)
124
128
  [Component#dispatchEvent (Java Platform SE 8 )](https://docs.oracle.com/javase/jp/8/docs/api/java/awt/Component.html#dispatchEvent-java.awt.AWTEvent-)
125
129
  [MouseEvent (Java Platform SE 8 )](https://docs.oracle.com/javase/jp/8/docs/api/index.html?javax/swing/JPanel.html)
130
+ ![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2024-08-01/52ca6ef8-19db-4df0-b592-4aeaf8879b41.gif)
126
131
 
127
132
  > いい方法はないでしょうか?
128
133
 

1

メソッド順

2024/07/29 15:48

投稿

TN8001
TN8001

スコア9807

test CHANGED
@@ -42,6 +42,20 @@
42
42
  addMouseListener(this);
43
43
  }
44
44
 
45
+ @Override protected void paintComponent(Graphics g) {
46
+ super.paintComponent(g);
47
+
48
+ if (click) {
49
+ g.setColor(new Color(255, 255, 255, count));
50
+ g.fillRect(point_x, point_y, 20, 20);
51
+ count -= 5;
52
+ if (count < 0) {
53
+ click = false;
54
+ count = 255;
55
+ }
56
+ }
57
+ }
58
+
45
59
  @Override public void mouseClicked(MouseEvent e) {
46
60
  point_x = e.getX() - 10;
47
61
  point_y = e.getY() - 10;
@@ -65,20 +79,6 @@
65
79
  // 捏造したイベントを送る
66
80
  child.dispatchEvent(new MouseEvent(child, e.getID(), e.getWhen(), e.getModifiersEx(),
67
81
  pp.x, pp.y, e.getClickCount(), e.isPopupTrigger()));
68
- }
69
- }
70
- }
71
-
72
- @Override protected void paintComponent(Graphics g) {
73
- super.paintComponent(g);
74
-
75
- if (click) {
76
- g.setColor(new Color(255, 255, 255, count));
77
- g.fillRect(point_x, point_y, 20, 20);
78
- count -= 5;
79
- if (count < 0) {
80
- click = false;
81
- count = 255;
82
82
  }
83
83
  }
84
84
  }