由於我以前的帖子很亂,我決定重新發布它,但希望這次更清潔。mouseLIstener在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光標變爲手形光標,但事實並非如此。 我錯過了什麼?
*「因爲我以前的帖子是一個爛攤子,我決定ŧ o轉貼「*不要那麼做!相反*編輯*原始帖子。 –
@AndrewThompson我已經刪除它,雖然 – user3026693
嗯,希望倒票會提醒你今後不要這樣做。與你之前在你編輯之前的陳述形成對比的是,*是一個答案,即使它不是特別相關。評論中還有一些有用的建議。你可以選擇刪除一個問題,但你也可以選擇**恢復它。** –