访问JFrame或JPanel中的子元素

时间:2019-01-28 13:59:59

标签: java

我目前正在编写一个小程序。在该程序中,我有一个JPanel,其中包含以20 x 20网格排列的400个文本框。
该程序的一部分工作是为变量分配颜色。然后,当用户单击其中一个文本框时,背景色将更改。 这是用Netbeans编写的,并且所有可视项都使用设计管理器列出(并更改布局管理器以适合)。

我在设计上没有问题,没有将颜色分配给变量,甚至没有编写使用鼠标单击事件处理程序将背景色设置为颜色变量的单独代码。

该问题的原因是目前,我需要为所有400个文本框编写代码才能使其正常工作。是否有一种方法可以知道单击哪个文本框并分配颜色,而无需通过父(JPanel)编写所有400个文本框的代码?

2 个答案:

答案 0 :(得分:2)

简单:使用FocusListener,将其添加到每个JTextField中。例如,如果您有这样的JTextFields:

private JTextField[][] fields = new JTextField[ROW_COUNT][ROW_COUNT];

并说您有一个这样的FocusListener:

private class MyFocus implements FocusListener {
    @Override
    public void focusLost(FocusEvent e) {
        // get JTextField that lost focus
        JTextField textField = (JTextField) e.getSource();

        // set color back to white
        textField.setBackground(INACTIVE_COLOR);
    }

    @Override
    public void focusGained(FocusEvent e) {
        // get JTextField that is gaining focus
        JTextField textField = (JTextField) e.getSource();

        // set color to the active background
        textField.setBackground(ACTIVE_COLOR);
    }
}

您可以创建并添加您的侦听器

    FocusListener focusListener = new MyFocus();        
    setLayout(new GridLayout(ROW_COUNT, ROW_COUNT, 1, 1));
    for (int row = 0; row < fields.length; row++) {
        for (int col = 0; col < fields[row].length; col++) {
            JTextField field = new JTextField(COLS);
            field.addFocusListener(focusListener);
            add(field);
        }
    }

整个可测试的东西:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*;

@SuppressWarnings("serial")
public class FocusExample extends JPanel {
    private static final int ROW_COUNT = 20;
    private static final int COLS = 5;
    protected static final Color ACTIVE_COLOR = Color.PINK;
    protected static final Color INACTIVE_COLOR = Color.WHITE;
    private JTextField[][] fields = new JTextField[ROW_COUNT][ROW_COUNT];

    public FocusExample() {
        FocusListener focusListener = new MyFocus();        
        setLayout(new GridLayout(ROW_COUNT, ROW_COUNT, 1, 1));
        for (int row = 0; row < fields.length; row++) {
            for (int col = 0; col < fields[row].length; col++) {
                JTextField field = new JTextField(COLS);
                field.addFocusListener(focusListener);
                add(field);
            }
        }
    }

    private class MyFocus implements FocusListener {
        @Override
        public void focusLost(FocusEvent e) {
            // get JTextField that lost focus
            JTextField textField = (JTextField) e.getSource();

            // set color back to white
            textField.setBackground(INACTIVE_COLOR);
        }

        @Override
        public void focusGained(FocusEvent e) {
            // get JTextField that is gaining focus
            JTextField textField = (JTextField) e.getSource();

            // set color to the active background
            textField.setBackground(ACTIVE_COLOR);
        }
    }

    private static void createAndShowGui() {
        FocusExample mainPanel = new FocusExample();

        JFrame frame = new JFrame("FocusExample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

答案 1 :(得分:0)

您不需要面板。只需这样做:

声明此鼠标适配器:

MouseAdapter ma = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JTextField tf = (JTextField) e.getSource();
                tf.setBackground(Color.BLACK);
            }
        };

在创建文本字段的循环中执行以下操作:

textField.addMouseListener(ma);

MouseEvent知道单击了哪个JTextField,因此您可以访问它并更改其颜色。将此鼠标侦听器添加到每个文本字段,这应该可以工作。