【问题标题】:FocusListener cannot be added like other Listeners?FocusListener 不能像其他 Listeners 一样添加?
【发布时间】:2018-01-28 01:50:24
【问题描述】:

在我的代码中,当我尝试将 FocusListener 添加到 JTextField 时,它显示

ChatClient.java:30: error: incompatible types: ChatClient cannot be converted to FocusListener
    text.addFocusListener(this);

但是添加MouseListener 就可以了。为什么会这样?使用FocusListener 创建另一个类有效。我想知道添加MouseListenerFocusListener 之间的区别。有没有其他方法可以简单地添加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);
  }
  public void focusGained(FocusEvent ae){
    if(text.getText().equals(defaultMessage)){
        text.setText("");
    }
  }
  public void focusLost(FocusEvent ae){
    if(text.getText().isEmpty()){
      text.setText(defaultMessage);
    }
  }

【问题讨论】:

  • 您的 ChatClient 根本不是 FocusListener,但它可能是 ActionListener。 ChatClient是怎么声明的,大概只实现了一个接口。
  • 也就是说:为它(以及您的其他听众)编写另一个类会更干净。你了解过内部类和匿名内部类吗?
  • 1) 为了尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。 2) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
  • 我和@JBNizet 在一起。通过使您的 GUI(您的“视图”)实现侦听器接口,您还使其既是视图又是“控制器”,从而承担了过多的责任。虽然这对于非常小的玩具或演示程序可能没问题,但如果/当程序变大时,它可能会使事情变得笨拙。将职责分离到它们自己的单独类中要好得多,如果不重要,则可以使用内部类,否则甚至可以使用单独的独立类。

标签: java swing actionlistener mouselistener focuslistener


【解决方案1】:

我看到您添加了focusGainedfocusLost 方法,但是您的类是否实现了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);
            }
        }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多