gpt4 book ai didi

java - 获取 mouseReleased 下的组件

转载 作者:行者123 更新时间:2023-11-30 10:57:16 27 4
gpt4 key购买 nike

我正在尝试做类似的事情:Swing - Get new component under mouseReleased

在一个窗口中,我有一些按钮,每个按钮上都有一个数字。例如 2 8 5 6。这将显示 4 个 JButton。如果我在 2 上按下鼠标,在 8 上拖动并释放...我希望 firstcomponent 包含 JButton 2,lastcomponent 包含带有 8 的 JButton。

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

我的问题是我不明白为什么“Release ... last entered: 2”显示 2。为什么不是 8?

谢谢

最佳答案

你添加的每个Bouton都有它自己的firstEnteredlastEntered,因此它只能正确打印出当前按钮(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());
}
}

}

关于java - 获取 mouseReleased 下的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32702126/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com