如何添加一个对不同的JTextField执行相同操作的动作侦听器?

时间:2015-07-06 08:47:47

标签: java swing

目前我有两个问题

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

    }

}

1 个答案:

答案 0 :(得分:1)

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

For 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);
    }
}