我正在尝试做类似的事情:Swing - Get new component under mouseReleased
在一个窗口中,我有一些按钮,每个按钮上都有一个数字。例如2 8 5 6.这将显示4个JButton。如果我在2上按鼠标,在8上拖动和释放...我希望firstcomponent
包含JButton 2和lastcomponent
以包含JButton 8。
public class Bouton extends JButton implements MouseListener {
String name;
Component firstEntered;
Component lastEntered;
public Bouton(String str) {
super(str);
this.name = str;
this.addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent event) { }
@Override
public void mouseEntered(MouseEvent event) {
lastEntered = event.getComponent();
String name2 = ((JButton) lastEntered).getName();
System.out.println("entered : " + name2);
}
@Override
public void mouseExited(MouseEvent event) {
firstEntered = event.getComponent();
}
@Override
public void mousePressed(MouseEvent event) {
// I tried this didn't work...
//firstEntered = event.getComponent();
}
@Override
public void mouseReleased(MouseEvent event) {
firstEntered.setBackground(Color.RED);
lastEntered.setBackground(Color.BLUE);
String name = ((JButton) firstEntered).getName();
String name2 = ((JButton) lastEntered).getName();
System.out.println("Release ... first: " + name);
System.out.println("Release ... last entered: " + name2);
}
@Override
public String getName() {
return name;
}
}
输出结果为:
entered : 2
entered : 8
Release ... first: 2
Release ... last entered: 2
我的问题是我不明白为什么“释放...最后输入:2”显示2.为什么它不是8?
由于
答案 0 :(得分:1)
您添加的每个Bouton
都拥有自己的firstEntered
和lastEntered
,因此它只能正确打印出当前按钮(this
)。我的建议是创建只有MouseListener
的一个新实例,我认为不需要创建自己的JButton
:( mousePressed()
方法现在也可正常使用)
import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel p = new JPanel();
MyMouseListener ml = new MyMouseListener(); // Only one MouseListener
for (int i = 0; i < 8; i++) {
JButton b = new JButton(String.valueOf(i));
b.addMouseListener(ml); // Add the same MouseListener to every
// button
p.add(b);
}
frame.setContentPane(p);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
public static class MyMouseListener implements MouseListener {
Component firstEntered;
Component lastEntered;
@Override
public void mouseClicked(MouseEvent event) {
}
@Override
public void mouseEntered(MouseEvent event) {
lastEntered = event.getComponent();
System.out.println("entered : " + ((JButton) lastEntered).getText());
}
@Override
public void mouseExited(MouseEvent event) {
}
@Override
public void mousePressed(MouseEvent event) {
firstEntered = event.getComponent();
System.out.println("pressed : " + ((JButton) event.getComponent()).getText());
}
@Override
public void mouseReleased(MouseEvent event) {
firstEntered.setBackground(Color.RED);
lastEntered.setBackground(Color.BLUE);
System.out.println("Release ... first: " + ((JButton) firstEntered).getText());
System.out.println("Release ... last entered: " + ((JButton) lastEntered).getText());
}
}
}