猿问

访问 JFrame 或 JPanel 中的子元素

我目前正在编写一个小程序。在程序中,我有一个 JPanel,其中包含 400 个文本框,设置在 20 x 20 网格中。
他程序的一部分致力于为变量分配颜色。然后当用户单击其中一个文本框时,背景颜色会发生变化。这是在 Netbeans 中编写的,所有可视项目都使用设计管理器设置(加上更改布局管理器以适应)。

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

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


慕无忌1623718
浏览 311回答 2
2回答

慕森卡

很简单:使用一个 FocusListener,一个添加到每个 JTextField。例如,如果您有这样的 JTextFields:private JTextField[][] fields = new JTextField[ROW_COUNT][ROW_COUNT];假设你有一个像这样的 FocusListener:private class MyFocus implements FocusListener {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void focusLost(FocusEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; // get JTextField that lost focus&nbsp; &nbsp; &nbsp; &nbsp; JTextField textField = (JTextField) e.getSource();&nbsp; &nbsp; &nbsp; &nbsp; // set color back to white&nbsp; &nbsp; &nbsp; &nbsp; textField.setBackground(INACTIVE_COLOR);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void focusGained(FocusEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; // get JTextField that is gaining focus&nbsp; &nbsp; &nbsp; &nbsp; JTextField textField = (JTextField) e.getSource();&nbsp; &nbsp; &nbsp; &nbsp; // set color to the active background&nbsp; &nbsp; &nbsp; &nbsp; textField.setBackground(ACTIVE_COLOR);&nbsp; &nbsp; }}您可以创建和添加您的侦听器&nbsp; &nbsp; FocusListener focusListener = new MyFocus();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; setLayout(new GridLayout(ROW_COUNT, ROW_COUNT, 1, 1));&nbsp; &nbsp; for (int row = 0; row < fields.length; row++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int col = 0; col < fields[row].length; col++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JTextField field = new JTextField(COLS);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.addFocusListener(focusListener);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add(field);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }整个可测试的东西: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 {&nbsp; &nbsp; private static final int ROW_COUNT = 20;&nbsp; &nbsp; private static final int COLS = 5;&nbsp; &nbsp; protected static final Color ACTIVE_COLOR = Color.PINK;&nbsp; &nbsp; protected static final Color INACTIVE_COLOR = Color.WHITE;&nbsp; &nbsp; private JTextField[][] fields = new JTextField[ROW_COUNT][ROW_COUNT];&nbsp; &nbsp; public FocusExample() {&nbsp; &nbsp; &nbsp; &nbsp; FocusListener focusListener = new MyFocus();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; setLayout(new GridLayout(ROW_COUNT, ROW_COUNT, 1, 1));&nbsp; &nbsp; &nbsp; &nbsp; for (int row = 0; row < fields.length; row++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int col = 0; col < fields[row].length; col++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JTextField field = new JTextField(COLS);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.addFocusListener(focusListener);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add(field);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private class MyFocus implements FocusListener {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void focusLost(FocusEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get JTextField that lost focus&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JTextField textField = (JTextField) e.getSource();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set color back to white&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textField.setBackground(INACTIVE_COLOR);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void focusGained(FocusEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get JTextField that is gaining focus&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JTextField textField = (JTextField) e.getSource();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set color to the active background&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textField.setBackground(ACTIVE_COLOR);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static void createAndShowGui() {&nbsp; &nbsp; &nbsp; &nbsp; FocusExample mainPanel = new FocusExample();&nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("FocusExample");&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; frame.getContentPane().add(mainPanel);&nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SwingUtilities.invokeLater(() -> createAndShowGui());&nbsp; &nbsp; }}

长风秋雁

你不需要面板。只需这样做:声明这个鼠标适配器:MouseAdapter ma = new MouseAdapter() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void mouseClicked(MouseEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JTextField tf = (JTextField) e.getSource();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tf.setBackground(Color.BLACK);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };在创建文本字段的循环内执行此操作:textField.addMouseListener(ma);MouseEvent 知道单击了哪个 JTextField,因此您可以访问它并更改它的颜色。将此鼠标侦听器添加到每个文本字段,这应该可以工作。
随时随地看视频慕课网APP

相关分类

Java
我要回答