gpt4 book ai didi

java - JFormattedTextfield.selectAll() 在格式化文本时不起作用

转载 作者:行者123 更新时间:2023-11-30 07:46:12 26 4
gpt4 key购买 nike

我有很多不同的 JFormattedTextFields 以及操作和按键监听器。每个字段都有一个按键监听器,因此当我按 Enter 键时,我将关注下一个 JFormattedTextField。问题是,对于某些 JFormattedTextFields,我的代码正在格式化输入,然后将文本设置为新的,而对于那些 selectAll() 不起作用。

JFormattedTextField a = new JFormattedTextField(someDouble);
JFormattedTextField b = new JFormattedTextField(someDouble2);
a.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
leasingfaktor1Field.selectAll();
if(...) {
//do something
a.setText(tausenderPunkt(someValue));
}
}
});
a.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
b.requestFocusInWindow();
}
}
});
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
leasingfaktor1Field.selectAll();
if(...) {
//do something
b.setText(tausenderPunkt(someValue));
}
}
});
b.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
c.requestFocusInWindow();
}
}
});

函数 tausenderPunkt():

public String tausenderPunkt(double value) {
String s = String.format("%1$,.2f", value);
return s;
}

因此,当我的光标位于字段 a 并按下 Enter 键时,光标会转到字段 b,但不会选择文本或值。当我不使用 setText() 时,我没有问题。有人有解决办法吗?

编辑:对于某些 JFormattedTextFields,解决方案是将 selectAll() 添加到 keyAdapter,但不是全部。例如:

b.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
c.requestFocusInWindow();
c.selectAll();
}
}
});

编辑2:问题似乎是当我创建 JFormattedTextFields 时。当我不使用构造函数中的值创建它们时,它会起作用。但我必须这么做。

最佳答案

在移动到下一个文本字段之前,您应该考虑处理当前关注的文本字段的所有必需条件,这当然包括提供给该字段的值或文本的格式。满足所有所需条件后,然后转到下一个文本字段。

实际上,这一切都可以通过针对您的特定情况的 keyPressed 事件来完成。任何文本字段都不需要 actionPerformed 事件,例如:

a.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
checkConditions(a, b);
}
}
});

b.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
checkConditions(b, c);
}
}
});
//---------- and so on -------------

这里有一个简单的方法,可以消除重复的代码:

private void checkConditions(JFormattedTextField fieldA, JFormattedTextField fieldB) {
// Make sure something is contained within fieldA and
// that it's actually numerical text.
if(!fieldA.getText().isEmpty() &&
fieldA.getText().matches("([-]?)\\d+([,]\\d+)?(([.]\\d+)?)")) {
// Convert the supplied text to Double and
// ensure the desired numerical formating.
String res = (String)tausenderPunkt(Double.parseDouble(fieldA.getText().replace(",","")));
fieldA.setText(res);
// Set Focus to our next text fieldB.
fieldB.requestFocusInWindow();
// Highlight the contents (if any) within the
// next text fieldB.
fieldB.selectAll();
}
// If fieldA is empty or fieldA does not contain
// numerical text then inform User and re-highlight
// the entry in fieldA.
else {
JOptionPane.showMessageDialog (null, "Please Enter Numerical Values Only!",
"Incorrect Entry", JOptionPane.WARNING_MESSAGE);
fieldA.selectAll();
}
}

如果您希望第一个文本字段的内容在焦点建立后立即突出显示(通过选项卡或单击),请考虑对该组件或您需要相同功能的任何其他组件使用 FocusGained 事件效果。

我希望这能在某种程度上有所帮助。

已编辑!

以便处理OP的特殊情况。

关于java - JFormattedTextfield.selectAll() 在格式化文本时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33919487/

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