我有一个Calculator类,它将使用4个文本字段中的3个输入字段,然后使用已经完成的Loan类计算另一个。

现在,我需要添加“计算器样式”按钮,用户可以使用这些按钮将这些数量输入到TextField中。这是我到目前为止所拥有的。 。 。

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class Calculator extends JFrame implements ActionListener
{
public static final int width = 400;
public static final int height = 300;
private JButton calculate;
private static JTextField amount;
private JTextField interest;
private JTextField term;
private JTextField payment;
private JLabel amountL;
private JLabel interestL;
private JLabel termL;
private JLabel paymentL;
private String i = "0";

public Calculator ()
{
    super("Loan Calculator");
    setSize(width, height);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    calculate = new JButton("payment");
    calculate.setFocusable(false);
    amount = new JTextField(10);
    interest = new JTextField(10);
    term = new JTextField(10);
    payment = new JTextField(10);

    JPanel numbButt = new JPanel();
    numbButt.setLayout(new GridLayout(4,3));
    JButton zero = new JButton("0");
    zero.setFocusable(false);
    JButton one = new JButton("1");
    one.setFocusable(false);
    JButton two = new JButton("2");
    two.setFocusable(false);
    JButton three = new JButton("3");
    three.setFocusable(false);
    JButton four = new JButton("4");
    four.setFocusable(false);
    JButton five = new JButton("5");
    five.setFocusable(false);
    JButton six = new JButton("6");
    six.setFocusable(false);
    JButton seven = new JButton("7");
    seven.setFocusable(false);
    JButton eight = new JButton("8");
    eight.setFocusable(false);
    JButton nine = new JButton("9");
    nine.setFocusable(false);
    JButton period = new JButton(".");
    period.setFocusable(false);
    numbButt.add(seven);
    seven.addActionListener(this);
    numbButt.add(eight);
    eight.addActionListener(this);
    numbButt.add(nine);
    nine. addActionListener(this);
    numbButt.add(four);
    four.addActionListener(this);
    numbButt.add(five);
    five.addActionListener(this);
    numbButt.add(six);
    six.addActionListener(this);
    numbButt.add(one);
    one.addActionListener(this);
    numbButt.add(two);
    two.addActionListener(this);
    numbButt.add(three);
    three.addActionListener(this);
    numbButt.add(zero);
    zero.addActionListener(this);
    numbButt.add(period);
    period.addActionListener(this);
    JButton enter = new JButton("enter");
    enter.setFocusable(false);
    numbButt.add(enter);
    enter.addActionListener(this);

    JPanel inputPanel = new JPanel();
    inputPanel.setLayout(new GridLayout(4,2));
    setLayout(new BorderLayout());
    amountL = new JLabel("amount");
    interestL = new JLabel("interest");
    termL = new JLabel("term");
    paymentL = new JLabel("payment");
    inputPanel.add(amountL);
    inputPanel.add(amount);
    inputPanel.add(interestL);
    inputPanel.add(interest);
    inputPanel.add(termL);
    inputPanel.add(term);
    inputPanel.add(paymentL);
    inputPanel.add(payment);
    add(inputPanel, BorderLayout.CENTER);

    calculate.addActionListener(this);
    JButton amountB = new JButton("amount");
    amountB.setFocusable(false);
    amountB.addActionListener(this);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(calculate);
    buttonPanel.add(amountB);
    add(buttonPanel, BorderLayout.SOUTH);
    add(numbButt, BorderLayout.WEST);
    interest.requestFocusInWindow();

    ActionListener actionListener = new ActionFocusMover();
    enter.addActionListener(actionListener);
}
@Override
public void actionPerformed(ActionEvent e)
{

    String iamt, ii, iterm, ipmt;
    String buttonString = e.getActionCommand();
    Component x;
    if(buttonString.equals("1"))
        i=i+1;
    else if(buttonString.equals("2"))
        i=i+2;
    else if(buttonString.equals("3"))
        i=i+3;
    else if(buttonString.equals("4"))
        i=i+4;
    else if(buttonString.equals("5"))
        i=i+5;
    else if(buttonString.equals("6"))
        i=i+6;
    else if(buttonString.equals("7"))
        i=i+7;
    else if(buttonString.equals("8"))
        i=i+8;
    else if(buttonString.equals("9"))
        i=i+9;
    else if(buttonString.equals("0"))
        i=i+0;
    else if(buttonString.equals("."))
        i=i+".";
    else if(buttonString.equals("enter"))

    else if(buttonString.equals("payment"))
    {
        iamt = amount.getText();
        ii = interest.getText();
        iterm = term.getText();

        Loan justin = new Loan(Integer.parseInt(iterm),Double.parseDouble(ii), Double.parseDouble(iamt));

        payment.setText(Double.toString(justin.getPayment()));
    }
    else
    {
        ii = interest.getText();
        iterm = term.getText();
        ipmt = payment.getText();

        Loan justin = new Loan(Double.parseDouble(ipmt), Integer.parseInt(iterm),Double.parseDouble(ii));

        amount.setText(Double.toString(justin.getAmount()));
    }

    amount.setText(i);
}

public static void setFocus()
{
    amount.requestFocusInWindow();
}
class ActionFocusMover implements ActionListener {
      public void actionPerformed(ActionEvent actionEvent) {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.focusNextComponent();
      }
    }
}


我的设置方式允许将按钮输入一个字符串,然后暂时将其放置在一个静态字段中(金额)。我需要知道的是如何进行编辑以检查哪个对象当前具有焦点并将inputbuttonString插入该对象。编辑

最佳答案

您可以使用requestFocusInWindow方法将焦点设置在组件上。确保组件是可显示的,可聚焦的和可见的。

来自How to Use the Focus Subsystem


如果您想确保某个特定组件获得关注,
首次激活窗口时,您可以调用
组件具有组件之后,在组件上使用requestFocusInWindow方法
已经实现,但是在显示框架之前。


要设置对Amount的初始关注,请在requestFocusInWindow之后调用pack()

同一教程提供了有关focus traversal policyfocus traversal keys的示例。例如,如何添加Enter作为焦点遍历键:

Set forwardKeys = getFocusTraversalKeys(
    KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
Set newForwardKeys = new HashSet(forwardKeys);
newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
    newForwardKeys);


或者,您可以通过JTextArea动作方法转移焦点,该方法对Enter按键进行反应,即:

amount.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        interest.requestFocusInWindow();
    }
});

09-07 05:46