0

是否可以在输入一组 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);

当我打字时, calc 得到计算。我输入 x "12" 并显示 12。并在 yi 中输入“10” 22 自动输入..等等。

为了“自动”澄清,我不想使用按键监听器(比如按下 Tab 键以获取下一个 TextField)(除非绝对必要)而且我不想点击按钮进行计算

这很像在字符串中输入 Hello World:当我输入标签时,标签将显示:“H”、“e”、“l”等。

希望我说得够清楚

4

2 回答 2

4

为此,您可以使用DocumentListenerDocument ,将其添加到每个相关的,并在接口的方法中JTextField执行添加操作。insertUpdate(...) and removeUpdate(...)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以仅允许放入DigitJTextField而不是任何随机字符。这是一个相关的例子

于 2013-08-11T03:22:03.330 回答
1

您需要阅读 java 教程中的监听器并实现一个监听器,它将输入的字符串转换为数字,然后更新标签的文本。

于 2013-08-10T19:27:02.667 回答