我目前正在写一个小程序。在该程序中,我有一个 JPanel,其中包含 400 个文本框,这些文本框排列在 20 x 20 网格中。
程序的一部分致力于为变量分配颜色。当用户单击其中一个文本框时,背景颜色会发生变化。
这是在 Netbeans 中编写的,所有视觉项目都使用设计管理器进行设置(加上更改布局管理器以适应)。
我对设计、为变量分配颜色,甚至编写使用鼠标单击事件处理程序将背景颜色设置为颜色变量的单独代码都没有问题。
问题的原因是目前,我需要为所有 400 个文本框编写代码才能完成这项工作。有没有办法知道哪个文本框被单击并分配颜色,而无需为所有 400 个文本框编写代码,也许通过父级(JPanel)?
最佳答案
简单:使用 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());
}
}
关于java - 访问 JFrame 或 JPanel 中的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54403588/