是否可以在键入JTextField时计算它们并将结果设置为JLabel?
例如:
int iCalc = 0;
JTextField x = new JTextField();
JTextField y = new JTextField();
JTextField z = new JTextField();
JLabel lCalc = new JLabel("Your total is: " + calc);
在我输入时,会计算出计算结果。我输入了x“12”,显示了12。 并且在y中我把“10”22自动输入等等。
澄清“自动”我不想使用键监听器(比如按下Tab键以获取下一个TextField)(除非绝对必要)并且我不想按一个按钮来进行计算
就像在字符串中输入Hello World一样:因为我输入标签会显示:“H”,“e”,“l” 等,等。
希望我澄清了
答案 0 :(得分:4)
您可以使用DocumentListener为此目的,将其添加到相关Document
的{{1}}中,并在JTextField
insertUpdate(...) and removeUpdate(...)
方法中执行加法运算{1}}界面。
以下是一个工作示例,以便进一步了解:
DocumentListener
您可能还需要向import java.awt.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class DocumentListenerExample {
private JPanel contentPane;
private JPanel compPanel;
private JTextField firstField;
private JTextField secondField;
private JTextField thirdField;
private JLabel resultLabel;
private static final String RESULT = "Result : ";
private int result;
private void displayGUI() {
JFrame frame = new JFrame("Document Listener Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel(new GridBagLayout());
compPanel = new JPanel(new BorderLayout(5, 5));
JPanel fieldPanel = new JPanel(new GridLayout(3, 3, 5, 5));
JLabel firstLabel = new JLabel("First Number : ", JLabel.CENTER);
firstField = new JTextField(10);
firstField.getDocument().addDocumentListener(
new MyDocumentListener());
JLabel secondLabel = new JLabel("Second Number : ", JLabel.CENTER);
secondField = new JTextField(10);
secondField.getDocument().addDocumentListener(
new MyDocumentListener());
JLabel thirdLabel = new JLabel("Third Number : ", JLabel.CENTER);
thirdField = new JTextField(10);
thirdField.getDocument().addDocumentListener(
new MyDocumentListener());
fieldPanel.add(firstLabel);
fieldPanel.add(firstField);
fieldPanel.add(secondLabel);
fieldPanel.add(secondField);
fieldPanel.add(thirdLabel);
fieldPanel.add(thirdField);
JPanel footerPanel = new JPanel();
resultLabel = new JLabel((RESULT + "0"), JLabel.CENTER);
footerPanel.add(resultLabel);
compPanel.add(fieldPanel, BorderLayout.CENTER);
compPanel.add(footerPanel, BorderLayout.PAGE_END);
contentPane.add(compPanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private class MyDocumentListener implements DocumentListener {
@Override
public void changedUpdate(DocumentEvent de) {}
@Override
public void insertUpdate(DocumentEvent de) {
calculateSum();
}
@Override
public void removeUpdate(DocumentEvent de) {
calculateSum();
}
private void calculateSum() {
int first = 0, second = 0, third = 0;
if (firstField.getDocument().getLength() > 0)
first = Integer.parseInt(firstField.getText());
if (secondField.getDocument().getLength() > 0)
second = Integer.parseInt(secondField.getText());
if (thirdField.getDocument().getLength() > 0)
third = Integer.parseInt(thirdField.getText());
result = first + second + third;
resultLabel.setText(RESULT + result);
}
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new DocumentListenerExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
添加一个DocumentFilter,只允许将Document
放在Digit
内,而不是任意随机字符。这是一个相关的example
答案 1 :(得分:1)
您需要在java教程中阅读有关侦听器的内容,并实现一个侦听器,它将键入的字符串转换为数字,然后更新标签的文本。