mouseLIstener在JPanel / JDialog中“不工作”(mouseLIstener “not working” in JPanel/JDialog)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
mouseLIstener在JPanel / JDialog中“不工作”(mouseLIstener “not working” in JPanel/JDialog)

由于我上一篇文章很乱,我决定重新发布它,但希望这次更干净。

所以这是我正在尝试使用的代码:

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class sample extends JFrame implements ActionListener, MouseListener { JButton b1, b2; JPanel panel1; JDialog dialog; public sample() { dialog = new JDialog(); dialog.setBounds (0,0,200,200); panel1 = new JPanel(); panel1.setLayout (new FlowLayout()); b1 = new JButton("B1"); add(b1); b1.addActionListener (this); b1.addMouseListener (this); b2 = new JButton ("B2"); panel1.add(b2); b2.addMouseListener (this); dialog.add(panel1); /* I tried this but it didn't work as well: dialog.addMouseListener(this); panel1.addMouseListener(this); */ } public void actionPerformed (ActionEvent e) { if (e.getSource () == b1) { dialog.setVisible (true); } } public void mouseClicked (MouseEvent e) { } public void mouseEntered (MouseEvent e) { setCursor (new Cursor (Cursor.HAND_CURSOR)); } public void mouseExited (MouseEvent e) { setCursor (new Cursor(Cursor.DEFAULT_CURSOR)); } public void mousePressed (MouseEvent e) { } public void mouseReleased (MouseEvent e) { } public static void main (String[] args) { sample s = new sample(); s.setVisible (true); s.setBounds (0,0,200,200); } }

我的目标是当用户将鼠标悬停在B2上时光标变为手形光标,但事实并非如此。 我错过了什么?

Since my previous post was a mess, I decided to repost it but hopefully much cleaner this time.

So here is the code I'm trying to work with:

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class sample extends JFrame implements ActionListener, MouseListener { JButton b1, b2; JPanel panel1; JDialog dialog; public sample() { dialog = new JDialog(); dialog.setBounds (0,0,200,200); panel1 = new JPanel(); panel1.setLayout (new FlowLayout()); b1 = new JButton("B1"); add(b1); b1.addActionListener (this); b1.addMouseListener (this); b2 = new JButton ("B2"); panel1.add(b2); b2.addMouseListener (this); dialog.add(panel1); /* I tried this but it didn't work as well: dialog.addMouseListener(this); panel1.addMouseListener(this); */ } public void actionPerformed (ActionEvent e) { if (e.getSource () == b1) { dialog.setVisible (true); } } public void mouseClicked (MouseEvent e) { } public void mouseEntered (MouseEvent e) { setCursor (new Cursor (Cursor.HAND_CURSOR)); } public void mouseExited (MouseEvent e) { setCursor (new Cursor(Cursor.DEFAULT_CURSOR)); } public void mousePressed (MouseEvent e) { } public void mouseReleased (MouseEvent e) { } public static void main (String[] args) { sample s = new sample(); s.setVisible (true); s.setBounds (0,0,200,200); } }

My goal is for the cursor to change to the hand cursor when the user hovers over B2, but it doesn't. What am I missing?

最满意答案

你下一个问题:

您将Cursor设置为sample实例( JFrame ),而不是JButton ,用于在按钮更改setCursor (new Cursor (Cursor.HAND_CURSOR));上设置光标setCursor (new Cursor (Cursor.HAND_CURSOR)); to ((JComponent)e.getSource()).setCursor (new Cursor (Cursor.HAND_CURSOR));

此外,为此您不必使用MouseListener您可以使用:

b1.setCursor(new Cursor (Cursor.HAND_CURSOR));

Your problem in next:

You set Cursor to sample instance(JFrame), not to JButton, for setting cursor on button change setCursor (new Cursor (Cursor.HAND_CURSOR)); to ((JComponent)e.getSource()).setCursor (new Cursor (Cursor.HAND_CURSOR));

Also for that purposes you needn't to use MouseListener you can just use:

b1.setCursor(new Cursor (Cursor.HAND_CURSOR));

更多推荐

本文发布于:2023-04-17 08:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/399b758b75f419c94eac1b73932b14a6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   JPanel   mouseLIstener   working   JDialog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!