gpt4 book ai didi

java - 让 JSpinner 只读取数字但也检测退格

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:31 25 4
gpt4 key购买 nike

我正在尝试制作一个只接受数字但我也希望它读取/响应退格键的 JSpinner。

public class test {
JFrame frame;
JPanel panel;
JSpinner spinner;

public test()
{
frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(200,200));

panel = new JPanel();

SpinnerNumberModel numSpin = new SpinnerNumberModel(10, 0,1000,1);
spinner = new JSpinner(numSpin);
JFormattedTextField txt = ((JSpinner.NumberEditor) spinner.getEditor()).getTextField();
((NumberFormatter) txt.getFormatter()).setAllowsInvalid(false);
panel.add(spinner);

frame.setContentPane(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

}
public static void main(String[] args)

{
test test = new test();
}
}

上面的代码只能生成数字,但这不允许我退格。我在这个网站上找到了一些示例,但它们是为 C 编写的。

最佳答案

  • 你是对的 JFormattedTextField 没有正确地实现到 JSpinner,你已经实现了 DocumentFilter 来过滤不需要的 从键盘输入或从剪贴板粘贴的字符,(感谢@StanislavL)

  • 您已经通过 focusGained() 上的 selectAll() 自行解决了问题,并将其包装到 invokeLater() 中,

例子

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.text.*;

public class TestDigitsOnlySpinner {

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

@Override
public void run() {
JFrame frame = new JFrame("enter digit");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSpinner jspinner = makeDigitsOnlySpinnerUsingDocumentFilter();
frame.getContentPane().add(jspinner, BorderLayout.CENTER);
frame.getContentPane().add(new JButton("just another widget"), BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}

private JSpinner makeDigitsOnlySpinner_BasicAttempt() {
JSpinner spinner = new JSpinner(new SpinnerNumberModel());
return spinner;
}

private JSpinner makeDigitsOnlySpinnerUsingDocumentFilter() {
JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 20, 1));
JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor();
final Document jsDoc = jsEditor.getTextField().getDocument();
if (jsDoc instanceof PlainDocument) {
AbstractDocument doc = new PlainDocument() {

private static final long serialVersionUID = 1L;

@Override
public void setDocumentFilter(DocumentFilter filter) {
if (filter instanceof MyDocumentFilter) {
super.setDocumentFilter(filter);
}
}
};
doc.setDocumentFilter(new MyDocumentFilter());
jsEditor.getTextField().setDocument(doc);
}
return spinner;
}
});
}

private static class MyDocumentFilter extends DocumentFilter {

@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
if (stringContainsOnlyDigits(string)) {
super.insertString(fb, offset, string, attr);
}
}

@Override
public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
super.remove(fb, offset, length);
}

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

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

private TestDigitsOnlySpinner() {
}
}

关于java - 让 JSpinner 只读取数字但也检测退格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9778958/

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