gpt4 book ai didi

java - jComboBox 只能输入字符(不能输入数字)

转载 作者:行者123 更新时间:2023-12-01 20:18:00 26 4
gpt4 key购买 nike

我正在尝试制作一个允许除数字之外的所有输入的 jComoBox。但是当我用 jComoBox 尝试它时,它不起作用。

我使用 jTextFiled 成功完成了此操作(但相反的站点 - 没有数字):

i_borow jTextFiled 的 TimeKeyTyped 事件代码:

private void i_borowTimeKeyTyped(java.awt.event.KeyEvent evt) {                                     
char c = evt.getKeyChar();
if(!( Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_DELETE)) {
evt.consume();
l_msg2.setForeground(Color.red);
} else {
l_msg2.setForeground(Color.black);
}
}

我尝试对 jComoBox (c_title) 执行相同的操作:

private void c_titleKeyTyped(java.awt.event.KeyEvent evt) {                                 
System.out.println("ssss");
char c = evt.getKeyChar();
System.out.println(c);
if(Character.isDigit(c)){
evt.consume();
l_noNum.setForeground(Color.red);
} else {
l_noNum.setForeground(Color.black);
}
}

该代码不起作用。此外,这并没有打印“ssss”字符串。为什么它在 jComboBox 上不起作用?谢谢。

最佳答案

以下是如何提供组合框所需的检查的示例。您也可以将相同的方法用于文本字段(它比使用监听器更好)。当用户将文本粘贴到组合框中时,此方法也适用。

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.plaf.basic.BasicComboBoxEditor;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class FilterTryout {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
JFrame frm = new JFrame("Combo test");
JComboBox<String> combo = new JComboBox<>(new String[] {"One", "Two", "Three"});
combo.setEditor(new ComboEditor());
combo.setEditable(true);
frm.add(combo);
frm.pack();
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
});
}

private static class ComboEditor extends BasicComboBoxEditor {
@Override
protected JTextField createEditorComponent() {
JTextField fld = super.createEditorComponent();
((AbstractDocument) fld.getDocument()).setDocumentFilter(new NoDigitsFilter());
return fld;
}
}

private static class NoDigitsFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
if (isNoDigits(string)) {
super.insertString(fb, offset, string, attr);
}
}

@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if (isNoDigits(text)) {
super.replace(fb, offset, length, text, attrs);
}
}

private boolean isNoDigits(String text) {
boolean noDigits = true;
for (int i = 0; i < text.length() && noDigits; i++) {
noDigits = !Character.isDigit(text.charAt(i));
}
return noDigits;
}
}
}

关于java - jComboBox 只能输入字符(不能输入数字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45431798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com