简体   繁体   English

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

[英]Java Determine which textfield has Focus

I have created buttons 1-9 as well as 4 text fields. 我创建了按钮1-9以及4个文本字段。 I am trying to allow the user to click on any number 1-9 to input into each text field. 我试图允许用户单击任何数字1-9以输入到每个文本字段中。 However, I cannot determine which field the user is clicked on. 但是,我无法确定用户被单击的字段。 Currently the 1-9 buttons only input text to the amt text field. 当前1-9按钮仅将文本输入到amt文本字段。 How can I check which field is clicked and enter input into there? 如何检查单击了哪个字段并在其中输入输入?

New Question 新问题

 public void actionPerformed(ActionEvent e) {

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

Is there a way to shorten this if statement so I do not have redundant code for buttons 1-10? 有没有一种方法可以缩短此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())); 
    }

I figured out what the OP meant, and made a quick solution: 我弄清楚了OP的含义,并提出了一个快速的解决方案:

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

Maybe add a mouseListener and create areas over the textFields? 也许添加mouseListener并在textFields上创建区域? Just guessing here but it might work. 只是在这里猜测,但它可能会起作用。 If this is possible then all you need is a variable to keep track of which area you clicked on and then check the variable for the textField you want. 如果可以,那么您只需要一个变量来跟踪您单击的区域,然后检查该变量中所需的textField。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM