好吧,讓每個文本字段成爲動作偵聽器。我做了很多測試,發現添加動作監聽器不是問題。問題出現在下面的代碼中,因爲對於前四個文本字段,hello窗口顯示,但不是ok。但在最下面的窗口中,確定的窗口和hello窗口彈出。我做錯了什麼?需要幫助在java中製作合適的動作偵聽器
public class handler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == text)
{
JOptionPane.showMessageDialog(null, "ok");
}
else if (event.getSource() == text1)
{
JOptionPane.showMessageDialog(null, "ok");
}
else if (event.getSource() == text2)
{
JOptionPane.showMessageDialog(null, "ok");
}
else if (event.getSource() == text3)
{
JOptionPane.showMessageDialog(null, "ok");
}
else if (event.getSource() == text4)
{
JOptionPane.showMessageDialog(null, "ok");
}
JOptionPane.showMessageDialog(null, "hello");
}
}
嘗試使用'.equals(...)'而不是'==',以及預期的行爲究竟是什麼? –