gpt4 book ai didi

java - java中的mouseevent没有被触发

转载 作者:行者123 更新时间:2023-12-02 06:24:30 25 4
gpt4 key购买 nike

我正在使用 Java7 设计一架虚拟钢琴。但是,我在事件调用方面遇到问题,但单击鼠标。 JFrame (Piano) 包含 JPanel (PianoKeyboard),而 JPanel (PianoKeyboard) 又包含多个钢琴键作为 JComponent(PianoKey)。我希望 Piano 类(jframe)知道按下相应图标时按下了哪个键。

因此我做了以下事情PianoKey 有一个 MouseListener,其方法在 PianoKeyboard 中实现。 PianoKeyboard 有一个 KeyListener,其方法在 Piano 中实现。当从钢琴键接收到事件时,将触发按键监听器事件。

但是问题是,当单击图标时,钢琴键盘内的任何鼠标事件监听器方法都不会被触发。

钢琴课:

public class Piano extends JFrame implements KeyListener {

public Piano() {
super();
this.setTitle("Piano");
this.setName("Piano");
this.setSize(850,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
PianoKeyboard s = new PianoKeyboard(0);
s.addListener(this);
this.getContentPane().add(s);
this.setVisible(true);
}
}

钢琴键盘类:

public class PianoKeyboard extends JPanel implements MouseListener{
ArrayList<KeyListener> listeners = new ArrayList<KeyListener>();
public PianoKeyboard(int tempo){
super();
this.tempo = tempo;
this.setBackground(Color.WHITE);
this.setEnabled(true);
this.setSize(Piano.FRAME_SIZE);
this.setVisible(true);
this.setLayout(new OverlayLayout(this));
createKeyboard();
}
public void addListener(KeyListener listener){
listeners.add(listener);
}

private void createKeyboard() {
........ code ......
for (int i = 0; i < 25; i++) {
PianoKey k = new PianoKey(i+1, tempo);
k.addMouseListener(this);
..... code .....
keys.add(k);
}
}

/** the followign methods are also available:
mouseClicked, mouseExited, mousePressed, mouseReleased
which contain the following code when neeed:
for(KeyListener k : listeners){
k.keyPressed();
}
*/

public void paint(Graphics g){
for(int i = 0; i < keys.size(); i++){
g.drawImage(keys.get(i).getImage(),positions.get(i),0,54,150,null);
}
}
}

钢琴键类:

public class PianoKey extends JComponent {
public PianoKey(int pitch,int tempo) {
super();
....code....
this.setImage(ImageIO.read(cl.getResource(IMAGE_DIRECTORY + "/key/white.png")));
}

主要听众:

public interface KeyListener {
public void keyPressed();
}

最佳答案

我认为发生的事情是您实际上没有将按键添加到任何 GUI 来注册鼠标单击。我看到您将它们添加到似乎某种列表中,但仅此而已。它们“出现”是因为面板正在从它们绘制图像,但这与实际上是界面的可见部分不同。

class PianoKey
extends JComponent {

PianoKey(/* args */) {

BufferedImage key = ImageIO.read(
cl.getResource(IMAGE_DIRECTORY + "/key/white.png")
);

final Dimension sz = new Dimension(key.getWidth(), key.getHeight());
setPreferredSize(sz);
setMinimumSize(sz);
setMaximumSize(sz);

setImage(key);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

BufferedImage key = getImage();

g.drawImage(key, 0, 0, key.getWidth(), key.getHeight(), null);
}
}

class PianoKeyBoard
extends JPanel
implements MouseListener {

PianoKeyBoard(/* args */) {

setLayout(new GridLayout(25, 0));

for( int i = 1; i <= 25; i++ ) {
PianoKey key = new PianoKey(/* args */);

key.addMouseListener(this);

add(key);
}
}

@Override
public void mousePressed(MouseEvent me) {
System.out.println("pressed");
}
@Override
public void mouseClicked(MouseEvent me) {
System.out.println("clicked");
}
@Override
public void mouseReleased(MouseEvent me) {
System.out.println("released");
}
@Override
public void mouseEntered(MouseEvent me) {
System.out.println("entered");
}
@Override
public void mouseExited(MouseEvent me) {
System.out.println("exited");
}
}

class Piano
extends JFrame {

Piano() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setContentPane(new PianoKeyBoard(/* args */));

pack();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Piano().setVisible(true);
}
});
}
}

类似的东西会起作用。在这里,PianoKey 绘制图像,而 PianoKeyBoard 只是将它们添加到自身。另外,作为旁注,请确保您是 overriding paintComponent in Swing ,不是油漆。

关于java - java中的mouseevent没有被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20686535/

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