由于我上一篇文章很乱,我决定重新发布它,但希望这次更干净。
所以这是我正在尝试使用的代码:
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));
更多推荐
发布评论