【问题标题】:How to add an action listener that does the same thing to different JTextField?如何添加对不同 JTextField 执行相同操作的操作侦听器?
【发布时间】:2015-07-06 08:47:47
【问题描述】:

目前我有两个问题

1) 如何为多个 JTextField 添加一个做同样事情的动作监听器?我知道我必须添加动作侦听器并简单地调用 textField.addMouseListener(this),但是如果我有多个 JTextField 怎么办?为简洁起见,请查看mouseClicked

2) 当 TextField 处于焦点时,将背景颜色更改为 255、255、180。当 TextField 失去焦点时,恢复为原始背景颜色。我该如何完成这项任务?

一个代码中的两个问题:

public class MainFrame implements MouseListener {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // Main Frame
        JFrame mainFrame = new JFrame("New Account Registration");

        JPanel gridPanel = new JPanel(new GridLayout(10,2));
        JPanel gridGenderPanel = new JPanel(new GridLayout(1,2, 4, 3));
        JPanel flowButton = new JPanel(new FlowLayout());

        //JLabels
        JLabel title = new JLabel("New Account Registration");

        Color newColor = new Color(255,255,180);

        // JTextFields
        JTextField nameField = new JTextField();
        nameField.addMouseListener(this);

        JTextField emailField = new JTextField();
        nameField.addMouseListener(this);

        JPasswordField passField = new JPasswordField();
        passField.addMouseListener(this);

        JPasswordField confirmPassField = new JPasswordField(); 
        confirmField.addMouseListener(this);

        JTextField addressField = new JTextField();
        addressField.addMouseListener(this);

        JTextField phoneField = new JTextField();
        phoneField.addMouseListener(this);

        gridPanel.add(title);
        gridPanel.add(nameField);
        gridPanel.add(emailField);
        gridPanel.add(passField);
        gridPanel.add(confirmPassField);
        gridPanel.add(addressField);
        gridPanel.add(countryField);


        mainFrame.add(gridPanel);   

        mainFrame.setSize(600, 700);
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if (e.getClickCount() == 1) {
            //How do i set the same thing to a different JTextField?
            setBackground(Color.BLUE);

        }

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

【问题讨论】:

  • 我知道我必须添加动作监听器并简单地调用 textField.addMouseListener(this) 不,你必须调用addActionListener

标签: java swing


【解决方案1】:

对于 1) 你可以获取另一个 TextField 并在 MouseClick 上设置你想要的属性。

对于 2) 您可以使用焦点侦听器。

focusGained(FocusEvent e)
focusLost(FocusEvent e)

public class MainFrame implements MouseListener, FocusListener {

    JTextField nameField;
    JTextField emailField

    public static void main(String[] args) {

    }


@Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if (e.getClickCount() == 1) {
           nameField.setBackground(Color.GRAY);
           emailField.setBackground(Color.GRAY);
        }

    }

     public void focusGained(FocusEvent e) {

        JTextField field = (JTextField) e.getSource();
        field.setBackground(Color.GRAY);
    }

    public void focusLost(FocusEvent e) {
        JTextField field = (JTextField) e.getSource();
        field.setBackground(Color.WHITE);
    }
}

【讨论】:

  • 能详细介绍一下吗?所以我可以接受你的回答
  • 这可能会帮助您入门。如果在其他类型的组件上使用相同的 FocusListner,则需要进行很多检查,例如检查 e.getSource 是否确实返回 JTextField。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 2016-04-10
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
相关资源
最近更新 更多