Java 访问JFrame或JPanel中的子元素
我目前正在写一个小程序。在程序中,我有一个JPanel,它包含400个文本框,以20 x 20的网格排列。Java 访问JFrame或JPanel中的子元素,java,Java,我目前正在写一个小程序。在程序中,我有一个JPanel,它包含400个文本框,以20 x 20的网格排列。 程序的一部分工作是为变量指定颜色。然后,当用户单击其中一个文本框时,背景颜色将更改。 这是在Netbeans中编写的,所有可视项都是使用设计管理器设置的(另外还要修改布局管理器以适应) 我对设计、将颜色分配给变量,甚至编写使用鼠标单击事件处理程序为颜色变量设置背景颜色的单独代码都没有异议 这个问题的原因是目前,我需要为所有400个文本框编写代码以使其工作。是否有一种方法可以知道单击了哪个文
程序的一部分工作是为变量指定颜色。然后,当用户单击其中一个文本框时,背景颜色将更改。 这是在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);
}
}
FocusListener FocusListener=newmyfocus();
setLayout(新的GridLayout(行数,行数,1,1));
for(int row=0;row
整个可测试的事情:
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.awt.Color;
导入java.awt.GridLayout;
导入java.awt.event.FocusEvent;
导入java.awt.event.FocusListener;
导入javax.swing.*;
@抑制警告(“串行”)
公共类FocusExample扩展了JPanel{
私有静态最终整数行计数=20;
专用静态最终int COLS=5;
受保护的静态最终颜色活动\u颜色=Color.PINK;
受保护的静态最终颜色不活动\u Color=Color.WHITE;
私有JTextField[][]字段=新JTextField[行计数][行计数];
公共焦点示例(){
FocusListener FocusListener=newmyfocus();
setLayout(新的GridLayout(行数,行数,1,1));
for(int row=0;rowcreateAndShowGui());
}
}
简单:使用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);
}
}
FocusListener FocusListener=newmyfocus();
setLayout(新的GridLayout(行数,行数,1,1));
for(int row=0;row
整个可测试的事情:
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.awt.Color;
导入java.awt.GridLayout;
导入java.awt.event.FocusEvent;
导入java.awt.event.FocusListener;
导入javax.swing.*;
@抑制警告(“串行”)
公共类FocusExample扩展了JPanel{
私有静态最终整数行计数=20;
专用静态最终int COLS=5;
受保护的静态最终颜色活动\u颜色=Color.PINK;
受保护的静态最终颜色不活动\u Color=Color.WHITE;
私有JTextField[][]字段=新JTextField[行计数][行计数];
公共焦点示例(){
FocusListener FocusListener=newmyfocus();
setLayout(新的GridLayout(行数,行数,1,1));
for(int row=0;row