Java确定哪个文本字段具有焦点

时间:2013-12-05 19:38:42

标签: java swing jbutton jtextfield calculator

我创建了按钮1-9以及4个文本字段。我试图让用户点击任意数字1-9输入到每个文本字段。但是,我无法确定用户被单击的字段。目前,1-9按钮仅将文本输入到amt文本字段。如何查看单击的字段并在其中输入输入?

新问题

 public void actionPerformed(ActionEvent e) {

    String iamt, ii, iterm,ipay;
    iamt = amt.getText();
    ii = interest.getText();
    iterm = term.getText();
    ipay = payment.getText();

有没有办法缩短这个if语句所以我没有按钮1-10的冗余代码?

    if(e.getSource()==btn1) {
        if(previouslyFocusedTextBox.equals(amt)){
            amt.setText("1");
            amt_number+=amt.getText();
            amt.setText(amt_number); 
        }
        else if (previouslyFocusedTextBox.equals(interest)){
            interest.setText("1");
            int_number+=interest.getText();
            interest.setText(int_number);
        }
        else if (previouslyFocusedTextBox.equals(term)){
            term.setText("1");
            t_number+=term.getText();
            term.setText(t_number);
        }
        else if (previouslyFocusedTextBox.equals(payment)){
            payment.setText("1");
            p_number+=payment.getText();
            payment.setText(p_number);
        }
    }

    if(e.getSource()==btnPay){
        Loan = new LoanforCalc(Double.parseDouble(iamt),Double.parseDouble(ii),Integer.parseInt(iterm));
            payment.setText(Double.toString(Loan.getPayment())); 
    }

2 个答案:

答案 0 :(得分:3)

我弄清楚了OP的含义,并快速解决了问题:

基本上,单击按钮后文本框会失去焦点,因此我们必须先跟踪拥有焦点的内容。

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.io.FileNotFoundException;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;

    public class FocusAwareWindow extends JFrame implements ActionListener, FocusListener {
        public static void main(String[] args) throws FileNotFoundException {
            FocusAwareWindow c = new FocusAwareWindow();
        }
        private JTextField textFieldA, textFieldB;
        private JButton buttonA, buttonB;
        private JTextField previouslyFocusedTextBox = textFieldA;
        public FocusAwareWindow(){
            super();
            this.setLayout(new FlowLayout());

            textFieldA = new JTextField();
            textFieldA.setText("Field A");
            textFieldA.setFocusable(true);
            textFieldA.addFocusListener(this);
            this.add(textFieldA);

            textFieldB = new JTextField();
            textFieldB.setText("Field B");
            textFieldB.setFocusable(true);
            textFieldB.addFocusListener(this);
            this.add(textFieldB);

            buttonA = new JButton("Which is focused?");
            buttonA.addActionListener(this);
            this.add(buttonA);

            this.pack();
            this.setVisible(true);;
        }
        public void actionPerformed(ActionEvent ev) {
            if(previouslyFocusedTextBox.equals(textFieldA)){
                System.out.println("Text Field A");
            } else if(previouslyFocusedTextBox.equals(textFieldB)){
                System.out.println("Text Field B");
            }
        }
        public void focusGained(FocusEvent ev) {
            if(ev.getSource() instanceof JTextField) {
                previouslyFocusedTextBox = (JTextField) ev.getSource();
            }
        }
        public void focusLost(FocusEvent ev) {
        }
    }

答案 1 :(得分:0)

也许添加一个mouseListener并在textFields上创建区域?只是在这里猜测,但它可能会奏效。如果可以,那么你需要的只是一个变量来跟踪你点击的区域,然后检查你想要的textField的变量。