作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个扩展 JPanel 的类,在这个面板中我添加了很多文本字段。我想将 DocumentListener 添加到每个 textField 以检测对文本的更改并将 txtField 的 bachground 更改为红色。我做了下面的事情但还没有工作。
谁能帮忙解决一下或者有什么新的建议吗?
public class MainDetailPanel extends JPanel implements DocumentListener{
public JLabel lblS_DWLDIRPRDPIC,lblL_DWLDIRPRD,lblS_UPLDIR,lblL_UPLDIR,lblS_DWLDIRPRD;
public JLabel lblFTP_DEFDIR,lblFTP_SERVER,lblFTP_POORT,lblFTP_USER,lblFTP_PASWOORD,lblL_CFDIR;
public JLabel lblL_DWLDIRPRDPIC,lblS_CFDIR,lblS_CFNAME,lblS_DWLNAMEPRD,lblS_DWLNAMEPRDPIC;
public JTextField txtL_DWLDIRPRD,txtS_DWLDIRPRDPIC,txtL_UPLDIR,txtS_DWLNAMEPRDPIC,txtL_CFDIR;
public JTextField txtFTP_DEFDIR,txtFTP_SERVER,txtFTP_POORT,txtFTP_USER,txtFTP_PASWOORD,txtS_UPLDIR;
public JTextField txtL_DWLDIRPRDPIC,txtS_CFDIR,txtS_CFNAME,txtS_DWLNAMEPRD,txtS_DWLDIRPRD;
public MainDetailPanel() {
createComponents();
layoutComponents();
}
private void createComponents() {
txtL_DWLDIRPRD = new JTextField(30);
txtL_DWLDIRPRD.getDocument().addDocumentListener(this);
txtS_DWLDIRPRDPIC = new JTextField(30);
txtL_UPLDIR = new JTextField(30);
txtS_DWLNAMEPRDPIC = new JTextField(30);
txtFTP_DEFDIR = new JTextField(30);
txtFTP_SERVER = new JTextField(30);
txtFTP_POORT = new JTextField(30);
txtFTP_USER = new JTextField(30);
txtFTP_PASWOORD = new JTextField(30);
txtL_CFDIR = new JTextField(30);
txtL_DWLDIRPRDPIC = new JTextField(30);
txtS_UPLDIR = new JTextField(30);
txtS_CFDIR = new JTextField(30);
txtS_CFNAME = new JTextField(30);
txtS_DWLNAMEPRD = new JTextField(30);
txtS_DWLDIRPRD = new JTextField(30);
lblFTP_DEFDIR = new JLabel("FTP_DEFDIR");
lblFTP_SERVER = new JLabel("FTP_SERVER");
lblFTP_POORT = new JLabel("FTP_POORT");
lblFTP_USER = new JLabel("FTP_USER");
lblFTP_PASWOORD = new JLabel("FTP_PASWOORD");
lblL_CFDIR = new JLabel("L_CFDIR");
lblL_UPLDIR = new JLabel("L_UPLDIR");
lblL_DWLDIRPRDPIC = new JLabel("L_DWLDIRPRDPIC");
lblS_CFDIR = new JLabel("S_CFDIR");
lblS_CFNAME = new JLabel("S_CFNAME");
lblS_DWLNAMEPRD = new JLabel("S_DWLNAMEPRD");
lblS_DWLDIRPRD = new JLabel("S_DWLDIRPRD");
lblS_DWLNAMEPRDPIC = new JLabel("S_DWLNAMEPRDPIC");
lblS_UPLDIR = new JLabel("S_UPLDIR");
lblS_DWLDIRPRDPIC = new JLabel("lblS_DWLDIRPRDPIC");
lblL_DWLDIRPRD = new JLabel("lblL_DWLDIRPRD");
}
private void layoutComponents() {
setLayout(new ParagraphLayout());
add(lblFTP_DEFDIR, ParagraphLayout.NEW_PARAGRAPH);
add(txtFTP_DEFDIR);
add(lblFTP_SERVER, ParagraphLayout.NEW_PARAGRAPH);
add(txtFTP_SERVER);
add(lblFTP_POORT, ParagraphLayout.NEW_PARAGRAPH);
add(txtFTP_POORT);
add(lblFTP_USER, ParagraphLayout.NEW_PARAGRAPH);
add(txtFTP_USER);
add(lblFTP_PASWOORD, ParagraphLayout.NEW_PARAGRAPH);
add(txtFTP_PASWOORD);
add(lblL_CFDIR, ParagraphLayout.NEW_PARAGRAPH);
add(txtL_CFDIR);
add(lblL_UPLDIR, ParagraphLayout.NEW_PARAGRAPH);
add(txtL_UPLDIR);
add(lblL_DWLDIRPRD, ParagraphLayout.NEW_PARAGRAPH);
add(txtL_DWLDIRPRD);
add(lblL_DWLDIRPRDPIC, ParagraphLayout.NEW_PARAGRAPH);
add(txtL_DWLDIRPRDPIC);
add(lblS_UPLDIR, ParagraphLayout.NEW_PARAGRAPH);
add(txtS_UPLDIR);
add(lblS_CFDIR, ParagraphLayout.NEW_PARAGRAPH);
add(txtS_CFDIR);
add(lblS_CFNAME, ParagraphLayout.NEW_PARAGRAPH);
add(txtS_CFNAME);
add(lblS_DWLNAMEPRD, ParagraphLayout.NEW_PARAGRAPH);
add(txtS_DWLNAMEPRD);
add(lblS_DWLDIRPRD, ParagraphLayout.NEW_PARAGRAPH);
add(txtS_DWLDIRPRD);
add(lblS_DWLNAMEPRDPIC, ParagraphLayout.NEW_PARAGRAPH);
add(txtS_DWLNAMEPRDPIC);
add(lblS_DWLDIRPRDPIC, ParagraphLayout.NEW_PARAGRAPH);
add(txtS_DWLDIRPRDPIC);
}
public void insertUpdate(DocumentEvent e) {
((JTextField)e.getDocument()).setBackground(Color.red);
}
public void removeUpdate(DocumentEvent e) {
((JTextField)e.getDocument()).setBackground(Color.red);
}
public void changedUpdate(DocumentEvent e) {
((JTextField)e.getDocument()).setBackground(Color.red);
}
}
最佳答案
您不能将 Document 转换为 JTextField。 JTextField 有一个文档。但是 JTextField 不是
文档。
据我所知,您必须为每个 JTextField 使用单独的监听器,并将 JTextField 的引用提供给您的监听器:
private static class BecomingRedDocumentListener implements DocumentListener {
private JTextField textField;
public BecomingRedDocumentListener(JTextField textField) {
this.textField = textField;
}
@Override
public void insertUpdate(DocumentEvent e) {
textField.setBackground(Color.red);
}
@Override
public void removeUpdate(DocumentEvent e) {
textField.setBackground(Color.red);
}
@Override
public void changedUpdate(DocumentEvent e) {
textField.setBackground(Color.red);
}
}
并且您应该使用工厂方法来创建您的文本字段:
private JTextField createTextFieldBecomingRed() {
JTextField tf = new JTextField(30);
tf.addDocumentListener(new BecomingRedDocumentListener(tf));
return tf;
}
关于java - 如何将文档监听器添加到面板内的 JTextFields?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10717623/
我是一名优秀的程序员,十分优秀!