为了账号安全,请及时绑定邮箱和手机立即绑定

访问 JFrame 或 JPanel 中的子元素

访问 JFrame 或 JPanel 中的子元素

慕无忌1623718 2022-05-25 16:28:26
我目前正在编写一个小程序。在程序中,我有一个 JPanel,其中包含 400 个文本框,设置在 20 x 20 网格中。他程序的一部分致力于为变量分配颜色。然后当用户单击其中一个文本框时,背景颜色会发生变化。这是在 Netbeans 中编写的,所有可视项目都使用设计管理器设置(加上更改布局管理器以适应)。我对设计、将颜色分配给变量甚至编写使用鼠标单击事件处理程序将背景颜色设置为颜色变量的单独代码都没有问题。问题的原因是目前,我需要为所有 400 个文本框编写代码才能使其工作。有没有办法知道单击了哪个文本框并分配颜色,而无需为所有 400 个文本框编写代码,可能通过父级(JPanel)?
查看完整描述

2 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

很简单:使用一个 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());

    }

}


查看完整回答
反对 回复 2022-05-25
?
长风秋雁

TA贡献1757条经验 获得超7个赞

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


声明这个鼠标适配器:


MouseAdapter ma = new MouseAdapter() {

            @Override

            public void mouseClicked(MouseEvent e) {

                JTextField tf = (JTextField) e.getSource();

                tf.setBackground(Color.BLACK);

            }

        };

在创建文本字段的循环内执行此操作:


textField.addMouseListener(ma);

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


查看完整回答
反对 回复 2022-05-25
  • 2 回答
  • 0 关注
  • 310 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号