Java确定哪个文本字段具有焦点 [英] Java Determine which textfield has Focus

查看:82
本文介绍了Java确定哪个文本字段具有焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了按钮1-9和4个文本字段.我试图允许用户单击任何数字1-9,以输入到每个文本字段中.但是,我无法确定用户被单击的字段.当前1-9按钮仅将文本输入到amt文本字段.如何检查单击了哪个字段并在其中输入输入?

I have created buttons 1-9 as well as 4 text fields. I am trying to allow the user to click on any number 1-9 to input into each text field. However, I cannot determine which field the user is clicked on. Currently the 1-9 buttons only input text to the amt text field. How can I check which field is clicked and enter input into there?

新问题

 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的冗余代码?

Is there a way to shorten this if statement so I do not have redundant code for buttons 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())); 
    }

推荐答案

我弄清楚了OP的含义,并提出了一个快速解决方案:

I figured out what the OP meant, and made a quick solution:

基本上,一旦单击按钮,文本框就会失去焦点,因此我们必须跟踪以前焦点的内容.

Essentially, the textboxes lose focus as soon as the button is clicked, so we have to keep track of what had the focus before.

    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) {
        }
    }

这篇关于Java确定哪个文本字段具有焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆