有两个按钮只有在所有字段中都存在有效数据时才启用,即数值和没有负数。你是怎么做到的?
问问题
2473 次
4 回答
5
我会考虑使用一个 DocumentListener 来监听 JTextFields 持有的文档。这个网站上有这类事情的例子(我知道,因为我写过一两个)。
例如,
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.JTextComponent;
public class DocumentListenerFun extends JPanel {
private static final int FIELD_COUNT = 5;
private JTextField[] fields = new JTextField[FIELD_COUNT];
private JButton fooBtn = new JButton("Foo");
private JButton barBtn = new JButton("Bar");
private MyFieldListener myFieldListener = new MyFieldListener();
public DocumentListenerFun() {
fooBtn.setEnabled(false);
barBtn.setEnabled(false);
for (int i = 0; i < fields.length; i++) {
fields[i] = new JTextField(10);
add(fields[i]);
myFieldListener.addTextComponent(fields[i]);
}
myFieldListener.addComponentsToEnable(fooBtn);
myFieldListener.addComponentsToEnable(barBtn);
add(fooBtn);
add(barBtn);
}
private static void createAndShowGui() {
DocumentListenerFun mainPanel = new DocumentListenerFun();
JFrame frame = new JFrame("DocumentListenerFun");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class MyFieldListener {
private List<JTextComponent> textComponents = new ArrayList<JTextComponent>();
private List<Component> componentsToEnable = new ArrayList<Component>();
private MyDocListener myDocListener = new MyDocListener(this);
public void addTextComponent(JTextComponent textComponent) {
textComponents.add(textComponent);
textComponent.getDocument().addDocumentListener(myDocListener);
}
public void addComponentsToEnable(Component comp) {
componentsToEnable.add(comp);
}
public void docsChanged() {
boolean textVerified = true;
for (JTextComponent textComp : textComponents) {
String text = textComp.getText();
textVerified &= verifyText(text);
}
for (Component comp : componentsToEnable) {
comp.setEnabled(textVerified);
}
}
private boolean verifyText(String text) {
try {
int value = Integer.parseInt(text.trim());
if (value > 0) {
return true;
}
} catch (NumberFormatException e) {
return false;
}
return false;
}
}
class MyDocListener implements DocumentListener {
private MyFieldListener myFieldListener;
public MyDocListener(MyFieldListener myFieldListener) {
this.myFieldListener = myFieldListener;
}
@Override
public void changedUpdate(DocumentEvent e) {
myFieldListener.docsChanged();
}
@Override
public void insertUpdate(DocumentEvent e) {
myFieldListener.docsChanged();
}
@Override
public void removeUpdate(DocumentEvent e) {
myFieldListener.docsChanged();
}
}
于 2012-10-12T02:53:24.063 回答
0
在您的 JTextFields 中使用 Key 侦听器或使用 focuslost。
http://www.java2s.com/Tutorial/Java/0240__Swing/ListeningtoJTextFieldEventswithanKeyListener.htm
http://docs.oracle.com/javase/tutorial/uiswing/events/focuslistener.html
于 2012-10-12T02:56:40.910 回答
0
如果您只是不想允许负值,那么您可以使用 DocumentFilter。
/**
* REF: http://www.coderanch.com/t/345628/GUI/java/DocumentFilter
*/
public class NumericFilter extends DocumentFilter {
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String text, AttributeSet attr) throws BadLocationException {
fb.insertString(offset, text.replaceAll("\\D", ""), attr);
}
// no need to override remove(): inherited version allows all removals
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
String text, AttributeSet attr) throws BadLocationException {
fb.replace(offset, length, text.replaceAll("\\D", ""), attr);
}
}
然后使用
((AbstractDocument)(new JTextField).getDocument()).setDocumentFilter(new NumericFilter());
于 2012-10-12T02:59:43.447 回答