FocusListener不能像其他Listener一样添加?
FocusListener cannot be added like other Listeners?
在我的代码中,当我尝试将 FocusListener
添加到 JTextField
时,它说
ChatClient.java:30: error: incompatible types: ChatClient cannot be converted to FocusListener
text.addFocusListener(this);
但是添加 MouseListener
效果很好。为什么会这样?用 FocusListener
创建另一个 class 是可行的。我想知道添加 MouseListener
和 FocusListener
之间的区别。有没有其他方法可以简单地添加 FocusListener
而无需为其编写单独的 class?
public void makeUI(){
text = new JTextField(defaultMessage);
text.setBounds(10,620,295,40);
text.addFocusListener(this);
add(text);
button = new JButton("SEND");
button.setBounds(310,620,80,40);
button.setForeground(Color.WHITE);
button.setBackground(Color.decode("#11A458"));
button.setFocusPainted(false);
button.addActionListener(this);
add(button);
setSize(400,700);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void focusGained(FocusEvent ae){
if(text.getText().equals(defaultMessage)){
text.setText("");
}
}
public void focusLost(FocusEvent ae){
if(text.getText().isEmpty()){
text.setText(defaultMessage);
}
}
我看到您添加了 focusGained
和 focusLost
方法,但是您的 class 是否实现了 FocusListener
接口?
所以,代码应该是这样的:
/**
* 1. this implements FocusListener is important
* 2. another interfaces you had here before also should be present
*/
public class YourClass implements FocusListener {
public void makeUI(){
text = new JTextField(defaultMessage);
text.setBounds(10,620,295,40);
text.addFocusListener(this);
add(text);
button = new JButton("SEND");
button.setBounds(310,620,80,40);
button.setForeground(Color.WHITE);
button.setBackground(Color.decode("#11A458"));
button.setFocusPainted(false);
button.addActionListener(this);
add(button);
setSize(400,700);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void focusGained(FocusEvent ae){
if(text.getText().equals(defaultMessage)){
text.setText("");
}
}
@Override
public void focusLost(FocusEvent ae){
if(text.getText().isEmpty()){
text.setText(defaultMessage);
}
}
}
在我的代码中,当我尝试将 FocusListener
添加到 JTextField
时,它说
ChatClient.java:30: error: incompatible types: ChatClient cannot be converted to FocusListener
text.addFocusListener(this);
但是添加 MouseListener
效果很好。为什么会这样?用 FocusListener
创建另一个 class 是可行的。我想知道添加 MouseListener
和 FocusListener
之间的区别。有没有其他方法可以简单地添加 FocusListener
而无需为其编写单独的 class?
public void makeUI(){
text = new JTextField(defaultMessage);
text.setBounds(10,620,295,40);
text.addFocusListener(this);
add(text);
button = new JButton("SEND");
button.setBounds(310,620,80,40);
button.setForeground(Color.WHITE);
button.setBackground(Color.decode("#11A458"));
button.setFocusPainted(false);
button.addActionListener(this);
add(button);
setSize(400,700);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void focusGained(FocusEvent ae){
if(text.getText().equals(defaultMessage)){
text.setText("");
}
}
public void focusLost(FocusEvent ae){
if(text.getText().isEmpty()){
text.setText(defaultMessage);
}
}
我看到您添加了 focusGained
和 focusLost
方法,但是您的 class 是否实现了 FocusListener
接口?
所以,代码应该是这样的:
/**
* 1. this implements FocusListener is important
* 2. another interfaces you had here before also should be present
*/
public class YourClass implements FocusListener {
public void makeUI(){
text = new JTextField(defaultMessage);
text.setBounds(10,620,295,40);
text.addFocusListener(this);
add(text);
button = new JButton("SEND");
button.setBounds(310,620,80,40);
button.setForeground(Color.WHITE);
button.setBackground(Color.decode("#11A458"));
button.setFocusPainted(false);
button.addActionListener(this);
add(button);
setSize(400,700);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void focusGained(FocusEvent ae){
if(text.getText().equals(defaultMessage)){
text.setText("");
}
}
@Override
public void focusLost(FocusEvent ae){
if(text.getText().isEmpty()){
text.setText(defaultMessage);
}
}
}