问题
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?
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(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()));
}
回答1:
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) {
}
}
回答2:
Maybe add a mouseListener and create areas over the 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.
来源:https://stackoverflow.com/questions/20409024/java-determine-which-textfield-has-focus