我正試圖製作一個程序,當用戶單擊該郵箱時,郵件將清除。 (必須是6-15個字符)這個東西會被清除,當它被點擊時,我嘗試了使用鼠標偵聽器,但它不會去當我點擊它。Textfield清除字符串
這裏的代碼片段:
abc = new JTextField(" (Must be 6-15 characters)");
abc.setBounds(40,130,310,30);
abc.setFont(new Font("Lucida Grande", Font.ITALIC, 14));
abc.setForeground(Color.gray);
abc.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
abc.setText("");
}
});
mainPanel.add(abc);
它適用於我。你是否宣佈'abc'爲'final'? – Hackerdarshi